HTML <img>
alt
Property: Image Alternate Text
The alt
attribute of the HTML <img>
tag specifies an alternate text for an image, which is displayed when the image cannot be rendered. This attribute is crucial for web accessibility and SEO, providing a textual description of the image’s content and function.
Purpose of the alt
Attribute
The primary purposes of the alt
attribute are:
- Accessibility: Provides a text alternative for users who are visually impaired and use screen readers.
- SEO: Helps search engines understand the context and content of the image, improving SEO rankings.
- Usability: Displays a description when the image fails to load due to broken links, network issues, or browser incompatibility.
Syntax
The syntax for using the alt
attribute in the <img>
tag is:
<img src="image.jpg" alt="Alternate text describing the image">
Attribute | Value | Description |
---|---|---|
`alt` | Text | Specifies the alternate text for the image. |
Examples
Let’s explore several practical examples to understand how to effectively use the alt
attribute.
Basic Example
This example demonstrates a simple use of the alt
attribute to describe an image of a cat.
<img src="cat.jpg" alt="A cute cat sitting on a mat" />
If the cat.jpg
image fails to load, the text “A cute cat sitting on a mat” will be displayed instead.
Example with Empty alt
When an image is purely decorative and doesn’t convey essential information, use an empty alt
attribute (alt=""
) to indicate this to screen readers.
<img src="decorative-border.png" alt="" />
In this case, the screen reader will skip the image, as it is considered non-essential.
Note: Avoid omitting the alt
attribute entirely for decorative images. An absent alt
attribute may cause screen readers to announce the image file name, which is not user-friendly. ⚠️
Example with a Link
When an image is used as a link, the alt
attribute should describe the destination of the link.
<a href="https://www.example.com">
<img src="logo.png" alt="Visit Example.com" />
</a>
The alt
text “Visit Example.com” clearly informs users about the link’s destination.
Complex Example: Describing an Infographic
For complex images like infographics, provide a concise summary in the alt
attribute and consider using the aria-describedby
attribute to link to a more detailed description.
<img
src="infographic.png"
alt="Sales data for Q1 2024"
aria-describedby="infographic-desc"
/>
<div id="infographic-desc" hidden>
Detailed sales data for Q1 2024, showing a 20% increase in revenue.
</div>
The alt
attribute provides a quick summary, while the aria-describedby
attribute links to a hidden detailed description for users who need more information.
Real-World Use Case: E-commerce Product Image
In e-commerce, the alt
attribute is essential for product images.
<img
src="red-shoes.jpg"
alt="Red running shoes by Nike, size 10"
width="200"
height="150"
/>
The alt
text includes relevant details such as the product type, color, brand, and size, which can help with SEO and accessibility.
Best Practices for Writing alt
Text
- Be Descriptive: The
alt
text should accurately describe the content and function of the image. - Be Concise: Keep the
alt
text short and to the point. Aim for under 125 characters. - Be Contextual: Consider the context of the image on the page when writing the
alt
text. - Avoid Redundancy: Don’t repeat information that is already provided in the surrounding text.
- Use Keywords: Incorporate relevant keywords to improve SEO, but avoid keyword stuffing.
- Test with Screen Readers: Verify that the
alt
text provides a meaningful experience for screen reader users. - Don’t include “image of …” or “picture of …”: The screen reader already announces that it is an image.
Accessibility Considerations
The alt
attribute plays a vital role in web accessibility:
- Screen Readers: Screen readers use the
alt
text to convey the content and purpose of images to visually impaired users. - Cognitive Accessibility: Well-written
alt
text can also benefit users with cognitive disabilities by providing clear and concise information. - Assistive Technologies: Other assistive technologies, such as speech recognition software, also rely on the
alt
attribute.
SEO Benefits
Search engines use the alt
attribute to understand the content and context of images, which can significantly impact SEO:
- Image Search: Proper
alt
text can improve the visibility of images in image search results. - Overall SEO: Search engines consider the
alt
text when ranking web pages, as it provides valuable information about the page’s content. - Relevance: Using relevant keywords in the
alt
text can help search engines understand the relevance of the image to the page’s topic.
Conclusion
The alt
attribute of the HTML <img>
tag is a powerful tool for enhancing web accessibility, improving SEO, and providing a better user experience. By following the guidelines and best practices outlined in this guide, you can ensure that your images are accessible, search engine friendly, and provide meaningful information to all users. Always strive to write descriptive, concise, and contextual alt
text to maximize the benefits of this essential attribute. 🚀