The alt (alternative text) attribute in an <img> tag provides a text description of the image content.
✅ Purpose of the alt Attribute
- Accessibility:
- Screen readers use the
alttext to describe the image to visually impaired users, helping them understand the context.
- Screen readers use the
- SEO (Search Engine Optimization):
- Search engines use the
alttext to index images, improving your site’s search visibility.
- Search engines use the
- Fallback Text:
- If an image fails to load due to network issues, the browser displays the
alttext instead.
- If an image fails to load due to network issues, the browser displays the
Example:
<img src="profile.jpg" alt="Profile picture of Teekam Singh" />
If the image can’t load, users will see:
Profile picture of Teekam Singh
And screen readers will announce the same text.
🚫 Bad Example:
<img src="profile.jpg" alt="image" />
- The word “image” gives no useful context.
⚡ Tip:
- Use descriptive, concise
alttext. - For decorative images, set
alt=""to tell screen readers to ignore them.
Example for Decorative Image:
<img src="border.png" alt="" />
Summary Table
| Purpose | Description |
|---|---|
| Accessibility | Helps screen readers describe images |
| SEO | Improves image search ranking |
| Fallback | Displays text if image fails to load |
In short:
Thealtattribute ensures accessibility, SEO benefits, and a better user experience by describing images when they can’t be seen or loaded.