Writing Meaningful Alt Text: Beyond the Basics
Real-World Scenario
You're building an e-commerce product page. The page has product images, decorative banners, infographics, and charts. Each image serves a different purpose and requires a different approach to alt text.
Meeting WCAG Compliance
WCAG 2.2 requires that all non-decorative images have alternative text that serves the same purpose as the image. Decorative images should have empty alt attributes.
<img src="product.jpg" alt="Blue cotton t-shirt with round neck" />Why this works: This meets compliance because it provides a text alternative that describes the image content. Screen readers will announce 'Blue cotton t-shirt with round neck' when encountering this image.
Going Beyond Compliance
While compliance ensures basic accessibility, exceptional alt text considers context, user intent, and provides actionable information that enhances the user experience.
Context-Aware Descriptions
Consider where the image appears and what information the user needs at that moment. A product image in a listing needs different alt text than the same image on a detail page.
<!-- In product listing -->
<img src="product.jpg" alt="Blue cotton t-shirt - $29.99" />
<!-- On product detail page -->
<img src="product.jpg" alt="Blue cotton t-shirt, front view showing round neck and short sleeves" />Functional Descriptions
For interactive images (like buttons or links), describe the function, not just the appearance.
<!-- Good: Describes function -->
<a href="/cart">
<img src="cart-icon.svg" alt="View shopping cart (3 items)" />
</a>
<!-- Better: More context -->
<button aria-label="View shopping cart containing 3 items">
<img src="cart-icon.svg" alt="" aria-hidden="true" />
<span class="sr-only">3 items</span>
</button>Complex Content Handling
For charts, graphs, and infographics, provide a brief summary in alt text and link to a detailed description or data table.
<figure>
<img
src="sales-chart.png"
alt="Sales increased 25% from Q1 to Q2, with Q2 reaching $2.5M"
/>
<figcaption>
Quarterly sales comparison.
<a href="#sales-data-table">View detailed data table</a>
</figcaption>
</figure>Real-World Scenario
A news website displays article images. The compliance approach might be:
<img src="breaking-news.jpg" alt="News photo" /><img
src="breaking-news.jpg"
alt="Protesters gather outside city hall holding signs demanding climate action"
/>Impact: The better example provides context that helps users understand the article even if they can't see the image, making the content more accessible and informative.