Alternative text for product thumbnail galleries

Alternative Text for Product Thumbnail Galleries #

Description:

Below is an example of an accessible thumbnail gallery with thoughtful alt text that provides context for each image.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Accessible Product Thumbnail Gallery</title>
    <style>
        /* Basic styling for gallery layout */
        .thumbnail-gallery {
            display: flex;
            gap: 10px;
        }
        .thumbnail-gallery img {
            width: 80px;
            height: 80px;
            cursor: pointer;
            border: 2px solid transparent;
        }
        .thumbnail-gallery img:focus, .thumbnail-gallery img:hover {
            outline: 2px solid #005fcc;
            border-color: #005fcc;
        }
        .main-image {
            width: 400px;
            height: auto;
            display: block;
            margin: 20px auto;
        }
    </style>
</head>
<body>
    <h2>Product Thumbnail Gallery</h2>
    <!-- Main product image -->
    <img src="images/shoe-front.jpg" alt="Brown leather shoe, front view" id="main-image" class="main-image">
    <!-- Thumbnail Gallery -->
    <div class="thumbnail-gallery" aria-label="Product image thumbnails">
        <img src="images/shoe-front.jpg" alt="Brown leather shoe, front view" data-large="images/shoe-front.jpg">
        <img src="images/shoe-side.jpg" alt="Brown leather shoe, side view" data-large="images/shoe-side.jpg">
        <img src="images/shoe-back.jpg" alt="Brown leather shoe, back view" data-large="images/shoe-back.jpg">
        <img src="images/shoe-top.jpg" alt="Brown leather shoe, top view" data-large="images/shoe-top.jpg">
    </div>
    <script>
        // JavaScript to change the main image when a thumbnail is clicked
        const mainImage = document.getElementById('main-image');
        const thumbnails = document.querySelectorAll('.thumbnail-gallery img');
        thumbnails.forEach(thumbnail => {
            thumbnail.addEventListener('click', () => {
                mainImage.src = thumbnail.dataset.large;
                mainImage.alt = thumbnail.alt;
            });
            thumbnail.addEventListener('keydown', (e) => {
                if (e.key === 'Enter' || e.key === ' ') {
                    e.preventDefault();
                    mainImage.src = thumbnail.dataset.large;
                    mainImage.alt = thumbnail.alt;
                }
            });
        });
    </script>
</body>
</html>
Explanation
Descriptive alt Text for Each Thumbnail:
Each thumbnail image has specific alt text, such as "Brown leather shoe, front view" or "Brown leather shoe, side view". This describes the product and specifies the angle, making each thumbnail unique.
Avoid generic text like "Product thumbnail", which would not help users distinguish between different images in the gallery.
Keyboard and Mouse Accessibility:
Thumbnails are clickable and focusable, allowing users to select them with either a mouse or keyboard.
Event listeners respond to click events and Enter or Space keys, ensuring keyboard accessibility for users who do not use a mouse.
Dynamic Main Image Update:
When a user clicks on a thumbnail, the src and alt attributes of the main image update to match the selected thumbnail. This provides continuity for screen reader users by announcing the change in the main image’s content.
Updating the alt attribute on the main image ensures that screen readers can accurately describe the currently displayed view.
Focus and Hover Styles:
The thumbnails have clear focus and hover indicators, enhancing accessibility for keyboard users by showing which thumbnail is selected.
ARIA Label for Gallery:
The thumbnail container has aria-label="Product image thumbnails", helping screen reader users understand the purpose of this image group.
Key Takeaways
Descriptive and Distinctive alt Text: Each thumbnail should have unique alt text describing the specific angle or detail.
Keyboard Accessibility: Ensure thumbnails are clickable and navigable via keyboard to support non-mouse users.
Update Main Image alt Text Dynamically: Reflect the selected view in the main image’s alt attribute for screen reader clarity.
Use ARIA for Context: Label the gallery container with aria-label to provide context for screen reader users.
 

Screen reader: NVDA
Video

What are your feelings

Updated on March 6, 2025