Accessible image sliders for featured products

Description:

Accessible image sliders, especially for featured products, are a common design element in e-commerce sites, but they often lack accessibility.

Key Accessibility Features for an Image Slider #

  1. Roles and Labels: Use ARIA roles and labels to make the slider structure clear to screen readers.
  2. Keyboard Controls: Allow users to navigate through the slider using keyboard controls.
  3. Pause and Play Controls: Allow users to pause and resume automatic sliding to prevent unwanted motion.
  4. aria-live for Announcements: Use aria-live to announce changes in slides.
  5. Focus Management: Ensure focus is managed when navigating through slides to avoid disorientation

Example: Accessible Image Slider for Featured Products #

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Accessible Featured Product Slider</title>
    <style>
        /* Basic styling for the slider */
        .slider-container {
            max-width: 700px;
            margin: 0 auto;
            position: relative;
            overflow: hidden;
        }
        .slides {
            display: flex;
            transition: transform 0.5s ease-in-out;
            width: 100%;
        }
        .slide {
            min-width: 100%;
            text-align: center;
            position: relative;
        }
        .slide img {
            width: 100%;
            height: auto;
        }
        .controls {
            display: flex;
            justify-content: center;
            margin-top: 10px;
        }
        .control-button {
            background-color: #333;
            color: #fff;
            border: none;
            padding: 10px;
            font-size: 1em;
            cursor: pointer;
            margin: 5px;
            border-radius: 5px;
        }
        .control-button:focus {
            outline: 2px solid #005fcc;
        }
    </style>
</head>
<body>
    <section class="slider-container" role="region" aria-labelledby="slider-title">
        <h2 id="slider-title">Featured Products</h2>
        <!-- Slide container with aria-live for dynamic updates -->
        <div class="slides" id="slides" aria-live="polite">
            <div class="slide" id="slide1">
                <img src="images/product1.jpg" alt="Eco-friendly leather backpack, front view">
                <p>Eco-Friendly Leather Backpack - $120</p>
            </div>
            <div class="slide" id="slide2">
                <img src="images/product2.jpg" alt="Sustainable cotton t-shirt, displayed on a model">
                <p>Sustainable Cotton T-Shirt - $35</p>
            </div>
            <div class="slide" id="slide3">
                <img src="images/product3.jpg" alt="Recycled plastic water bottle, with secure cap">
                <p>Recycled Plastic Water Bottle - $15</p>
            </div>
        </div>
        <!-- Slider Navigation Controls -->
        <div class="controls">
            <button class="control-button" id="prev-button" aria-controls="slides" aria-label="Previous Slide">Previous</button>
            <button class="control-button" id="next-button" aria-controls="slides" aria-label="Next Slide">Next</button>
            <button class="control-button" id="play-pause-button" aria-pressed="false" aria-label="Pause Slide">Pause</button>
        </div>
    </section>
    <script>
        // Slide data and initial setup
        const slides = document.querySelectorAll('.slide');
        let currentIndex = 0;
        let isPlaying = true;
        const prevButton = document.getElementById('prev-button');
        const nextButton = document.getElementById('next-button');
        const playPauseButton = document.getElementById('play-pause-button');
        const slideContainer = document.getElementById('slides');
        // Update the slide position
        function updateSlidePosition() {
            const offset = -currentIndex * 100;
            slideContainer.style.transform = `translateX(${offset}%)`;
            slideContainer.setAttribute('aria-live', 'polite');
        }
        // Show the previous slide
        function showPreviousSlide() {
            currentIndex = (currentIndex === 0) ? slides.length - 1 : currentIndex - 1;
            updateSlidePosition();
        }
        // Show the next slide
        function showNextSlide() {
            currentIndex = (currentIndex === slides.length - 1) ? 0 : currentIndex + 1;
            updateSlidePosition();
        }
        // Toggle play/pause
        function togglePlayPause() {
            isPlaying = !isPlaying;
            playPauseButton.setAttribute('aria-pressed', !isPlaying);
            playPauseButton.textContent = isPlaying ? 'Pause' : 'Play';
            if (isPlaying) {
                startAutoPlay();
            } else {
                stopAutoPlay();
            }
        }
        // Auto-play function
        let autoPlayInterval;
        function startAutoPlay() {
            autoPlayInterval = setInterval(showNextSlide, 5000); // 5 seconds interval
        }
        function stopAutoPlay() {
            clearInterval(autoPlayInterval);
        }
        // Event listeners
        prevButton.addEventListener('click', showPreviousSlide);
        nextButton.addEventListener('click', showNextSlide);
        playPauseButton.addEventListener('click', togglePlayPause);
        // Keyboard navigation for the buttons
        prevButton.addEventListener('keydown', (e) => {
            if (e.key === 'ArrowLeft') showPreviousSlide();
        });
        nextButton.addEventListener('keydown', (e) => {
            if (e.key === 'ArrowRight') showNextSlide();
        });
        // Start auto-play on load
        startAutoPlay();
    </script>
</body>
</html>

Explanation #

  1. ARIA Roles and Labels:
    • The carousel container uses role="region" and aria-labelledby="slider-title" to define it as a distinct section labeled by the heading “Featured Products.”
    • Each navigation button (Previous, Next, and Play/Pause) has an aria-label describing its function to screen reader users. The play-pause-button also uses aria-pressed to indicate whether it’s currently active or paused.
  2. Keyboard Navigation:
    • Each button is keyboard-accessible, allowing users to navigate through slides with Tab, Enter, or Space.
    • The ArrowLeft and ArrowRight keys are mapped to the Previous and Next buttons, providing an intuitive experience for keyboard-only users.
  3. Automatic Slideshow with Play/Pause Control:
    • The slideshow is set to auto-play with a 5-second interval, which can be toggled on and off with the Play/Pause button.
    • The play-pause-button manages state with aria-pressed to indicate if the carousel is in play or pause mode.
  4. aria-live for Slide Updates:
    • The aria-live="polite" attribute on the slide container announces changes to the active slide without interrupting other announcements, providing dynamic updates to screen reader users.
  5. Focus Management:
    • When navigating slides, focus remains on the carousel controls, avoiding unnecessary focus shifts, which can be disorienting.

Tips for Implementing Accessible Image Sliders #

  • Provide Descriptive ARIA Labels: Use aria-label, aria-pressed, and aria-labelledby to help screen reader users understand the slider’s functionality.
  • Enable Full Keyboard Navigation: Make sure all controls are keyboard-accessible and map ArrowLeft and ArrowRight keys for intuitive slide navigation.
  • Offer Play/Pause Options: Provide users with the ability to pause the slider, especially if it auto-plays, to avoid distraction or discomfort.
  • Use aria-live for Updates: Announce slide changes dynamically to keep screen reader users informed of changes.

Key Takeaways #

  • Ensure ARIA Role Clarity: Use ARIA roles and labels to describe the carousel’s structure and functionality to screen readers.
  • Respect User Preferences: Offer play/pause options to give users control over motion.
  • Update Descriptions with aria-live: Announce slide changes in real time with aria-live.

Screen reader: NVDA
Video:

What are your feelings

Updated on March 6, 2025