Testing

Accessible carousels: the four tests to run, and the code that passes them

8 min read
Mikhail ShaymardanovWeb Quality and Accessibility Expert

Keyboard users can still reach the links of a carousel slide that has moved off screen. An accessible carousel takes those links out of the keyboard path with the inert attribute. It also passes three other tests: a pause button if it rotates on its own, a name for every button and slide, and an announcement when someone changes slides. All four are checked by hand, in the browser.

Four tests on a demo carousel, then the code


An accessible carousel can be stopped, keeps the keyboard in the slide on screen, names its buttons and slides, and announces the slide the person asks for. A carousel shows one slide at a time, or a few, and sometimes rotates on its own. Rotation gets in the way of anyone who reads slowly or finds motion distracting. With a keyboard, focus marks the element that receives keystrokes, and it sometimes lands in a hidden slide. With a screen reader, a person who asks for the next element during rotation can hear one from the following slide, without warning.

We take four rules from the W3C Carousel pattern, the design pattern for an accessible carousel:

  • Pause. If the carousel rotates on its own, a button stops and restarts it. Keyboard focus also stops it until that button is pressed, and a mouse stops it while hovering.
  • Keyboard. Tab goes through the rotation button, the arrows, then the slide on screen. It never enters a hidden slide.
  • Names. Every button has an accessible name, the text the screen reader receives. So do the carousel and each slide: a slide's title or, failing that, its position, “2 of 4”.
  • Announcement. The screen reader announces the slide the person asks for. Automatic rotation stays silent.

Our demo is the home page of Cloudburst, an umbrella maker in Cork. Its “Featured” carousel rotates four slides every 7 seconds. Two icon arrows frame it, and a hovering mouse stops it.

Screenshot: the “Featured” carousel of Cloudburst, with a photo of an umbrella printed with roses on a cobbled street, the title “Autumn collection” and the “See the collection” link, between two round arrows.Marker 1. The previous arrow, an icon with no text. Marker 2. The next arrow, an icon with no text. Marker 3. The slide on screen, “Autumn collection”, with its “See the collection” link.
On screen, the Cloudburst carousel shows only its arrowsmarker 1marker 2 and the slide on displaymarker 3; the other three wait outside the frame, links included.

A carousel is tested by hand in four moves: try to stop it, go through it with Tab, read its names, and check that a slide change is announced. The Cloudburst carousel fails all four.

This test only applies to a carousel that rotates on its own. Look for a pause button and rest the mouse on the carousel. Move it away, then press Tab until you reach the link in the slide on screen, and wait.

The demo has no pause button; only hovering stops it. In Chrome and Firefox, we put focus on “See the collection”. Within 7 seconds, the next slide had pushed that link out of the frame, focus outline and all.

The test fails, because nothing but the mouse stops this carousel. It starts on its own, runs for more than five seconds and shares the screen with the rest of the page. The W3C's accessibility guidelines, WCAG, then call for a way to pause, stop or hide it. The European standard EN 301 549 lists this rule among its requirements for web pages. Following this standard is one voluntary way to meet the European Accessibility Act. Stopping on focus doesn't count as a pause if the rotation resumes as soon as focus moves away.

From the link just before the carousel, press Tab until you leave it, counting the presses. If hovering stops the rotation, keep the mouse on the carousel while counting.

On the demo, three presses out of six land on links in hidden slides. Each time, Chrome and Firefox scroll the carousel sideways to the link, and the display jumps. The test fails, because a slide pushed out of the frame stays in the tab order, the path that Tab follows.

Click the demo, then press the key shown, or move on with “Next step”. Escape leaves the demo.
7 steps

With the carousel stopped on its first slide, three of the six Tab presses inside it reach links in hidden slides, and the view slides over to show them.

#Do the buttons and slides have names?

In Firefox, right-click each arrow, then each slide, and choose “Inspect Accessibility Properties”. The Accessibility tab then shows the name the screen reader receives. Firefox only shows this option after its developer tools have been opened once.

On the demo, both arrows are buttons with an empty name, and Firefox's checks display “Interactive elements must be labeled”. The carousel has no name, and the inspector shows no slides: the titles and links of all four follow one another, with nothing grouping them by slide.

Screenshot: Firefox on the Cloudburst home page, with the carousel at the top and the Accessibility tab of the developer tools below. In the tree, two button rows have an empty name and a text label flag; on the right, the checks read “Interactive elements must be labeled” and the properties show name: null.Marker 1. The next arrow, on the page. Marker 2. Its row in the accessibility tree: a button with an empty name, flagged text label. Marker 3. The name property, set to null.
In Firefox's Accessibility tab, the next arrowmarker 1 turns out to be a button with an empty namemarker 2: its name property is nullmarker 3.

The test fails, because an icon arrow only speaks to the eye. The inspector finds only its role, “button”, and nothing that says what it does.

#Is the slide change announced?

Stop the rotation and search the carousel's code for aria-live. This attribute creates a live region: the screen reader reads whatever changes inside it, without moving focus. Then click the next arrow: with NVDA and Firefox, the new slide should be announced.

The demo's code has no live region: the slide changes on screen, but nothing announces it. The test fails, because the person who asked for a new slide gets no feedback.

How to respect prefers-reduced-motion: sort every animation, in CSS and JavaScript

The fixed carousel looks the same, apart from one button: a few HTML attributes and a short script get it through all four tests. That “Pause slideshow” button comes first, so someone bothered by motion finds it before the moving content. Dropping automatic rotation is the other option: the carousel then needs no pause button and keeps its original look. We recommend that option when the client agrees, and the button otherwise.

#What changes in the HTML?

Avoid

Screenshot: the “Featured” carousel of Cloudburst, with the “Autumn collection” slide between two round arrows.
<div class="carousel">
  <h2>Featured</h2>
  <button class="arrow prev"><svg>…</svg></button>
  <button class="arrow next"><svg>…</svg></button>
  <div class="viewport">
    <div class="track">
      <div class="slide">
        <img src="autumn-collection.webp" alt="…">
        <h3>Autumn collection</h3>
        <p>Six canopies printed in our workshop…</p>
        <a href="/collection">See the collection</a>
      </div>
      <div class="slide">…</div>
      <div class="slide">…</div>
      <div class="slide">…</div>
    </div>
  </div>
</div>

Correct

Screenshot: the same carousel, with a “Pause slideshow” button added to the right of the “Featured” title.Marker 1. The “Pause slideshow” button, the only visible addition.
<section class="carousel" aria-roledescription="carousel" aria-labelledby="featured-title">
  <h2 id="featured-title">Featured</h2>
  <button type="button" class="rotation">Pause slideshow</button>
  <button type="button" class="arrow prev" aria-label="Previous slide">
    <svg aria-hidden="true">…</svg>
  </button>
  <button type="button" class="arrow next" aria-label="Next slide">
    <svg aria-hidden="true">…</svg>
  </button>
  <p class="status visually-hidden" aria-live="polite" aria-atomic="true"></p>
  <div class="viewport">
    <div class="track">
      <div class="slide" role="group" aria-roledescription="slide" aria-labelledby="s1-title">
        <img src="autumn-collection.webp" alt="…">
        <h3 id="s1-title">Autumn collection</h3>
        <p>Six canopies printed in our workshop…</p>
        <a href="/collection">See the collection</a>
      </div>
      <div class="slide" role="group" aria-roledescription="slide" aria-labelledby="s2-title" inert>…</div>
      <div class="slide" role="group" aria-roledescription="slide" aria-labelledby="s3-title" inert>…</div>
      <div class="slide" role="group" aria-roledescription="slide" aria-labelledby="s4-title" inert>…</div>
    </div>
  </div>
</section>
Only the “Pause slideshow” buttonmarker 1 changes how the Cloudburst carousel looks; the names, inert and the live region don't show.

The aria-roledescription attribute renames the type of element the screen reader announces: the section becomes a “carousel”, and each role="group" a “slide”. Its values are therefore written in the page's language. Each slide is named by its title, unique here, without the word “slide” the attribute already supplies.

With a sliding transition, inert takes the hidden slides away from Tab, the mouse and the screen reader, and keeps them visible as they slide. The major browsers have supported it since April 2023, according to MDN. Without a transition, hidden is enough. aria-hidden alone would leave the links within reach of Tab (our aria-hidden guide).

The live region is the status paragraph. Its visually-hidden class hides it on screen and keeps it readable for screen readers:

.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

#What does the JavaScript do?

The script puts inert on the hidden slides and stops the rotation whenever keyboard focus lands in the carousel. Only the button restarts it.

const carousel = document.querySelector(".carousel");
const track = carousel.querySelector(".track");
const slides = [...carousel.querySelectorAll(".slide")];
const rotation = carousel.querySelector(".rotation");
const status = carousel.querySelector(".status");
let current = 0;
let rotating = !matchMedia("(prefers-reduced-motion: reduce)").matches; // paused if the system asks for less motion
let hovering = false;
let timer;
 
function show(n) {
  current = (n + slides.length) % slides.length;
  track.style.transform = `translateX(-${current * 100}%)`;
  // Only the slide on screen stays reachable: Tab, mouse and screen reader.
  slides.forEach((slide, i) => (slide.inert = i !== current));
}
 
function updateRotation() {
  clearInterval(timer);
  if (rotating && !hovering) timer = setInterval(() => show(current + 1), 7000);
  rotation.textContent = rotating ? "Pause slideshow" : "Play slideshow";
}
 
// The person asks for a slide: rotation stops and the live region gets its title.
function goTo(n) {
  rotating = false;
  updateRotation();
  show(n);
  status.textContent = `Slide ${current + 1} of ${slides.length}: ${slides[current].querySelector("h3").textContent}`;
}
 
rotation.addEventListener("click", () => {
  rotating = !rotating;
  updateRotation();
});
// Every keyboard focus in the carousel stops rotation; only the button restarts it.
carousel.addEventListener("focusin", (event) => {
  if (!event.target.matches(":focus-visible")) return;
  rotating = false;
  updateRotation();
});
carousel.addEventListener("mouseenter", () => { hovering = true; updateRotation(); });
carousel.addEventListener("mouseleave", () => { hovering = false; updateRotation(); });
// Previous and next leave focus on the button.
carousel.querySelector(".prev").addEventListener("click", () => goTo(current - 1));
carousel.querySelector(".next").addEventListener("click", () => goTo(current + 1));
 
show(0);
updateRotation();

The :focus-visible check limits that stop to the keyboard. On the demo, Chrome and Firefox give focus to a button clicked with the mouse. Without the check, a click on “Pause slideshow” would stop the rotation through focus, then restart it right away.

Only one line of the script writes to the live region. It sits in goTo(), which only the arrows call, so automatic rotation announces nothing. The W3C's carousel example also mutes its announcements while it rotates. Otherwise every new slide would interrupt whatever else the person is reading, and the page would become unusable. An accessible carousel therefore says less than one that announces everything.

Yes. On the fixed demo, the first press of Tab stops the rotation, and the button changes to “Play slideshow”. The links of the hidden slides have left the tab order: four presses cross the carousel, down from six, and none reaches anything off screen.

In Firefox's Accessibility tab, the arrow is named “Next slide”, and the slide on screen carries its title. A click on that arrow writes the next title into the live region.

Click the demo, then press the key shown, or move on with “Next step”. Escape leaves the demo.
5 steps

On the fixed Cloudburst carousel, Tab stops four times inside it: the rotation button, the two arrows and the link of the slide on screen, nothing outside the frame.
The aria-hidden attribute: when to use it, and what to do when Tab reaches what it hides

The Includdy scan lists the non-conformities of your live home page, or of its carousel alone. For each one, you get a test plan to replay after the fix. Its guided fix then sets a compliant component, drafted by Includdy's AI, beside the faulty HTML, for you to check before it goes live.


Share this article

Frequently asked questions

Five WCAG success criteria cover a carousel's motion, button names and keyboard use. Motion falls under 2.2.2 Pause, Stop, Hide. Content that starts on its own, lasts more than five seconds and sits alongside other content needs a way to pause, stop or hide it, unless the motion is essential. The names of the arrow buttons fall under 4.1.2 Name, Role, Value.

Keyboard control falls under 2.1.1 Keyboard. The order in which Tab stops falls under 2.4.3 Focus Order, and a visible focus under 2.4.7 Focus Visible. The European standard EN 301 549 lists each of these criteria among its requirements for web pages. Following this standard is one voluntary way to meet the European Accessibility Act.

Not necessarily. WebAIM discourages carousels and recommends, as a best practice, that animated elements be paused by default.

A carousel that never rotates on its own no longer needs a pause button. The three other tests still apply: Tab only enters the slide on screen, every button and slide has a name, and the change the person asks for gets announced. The device setting that turns off animations is covered in our prefers-reduced-motion guide.

No. The W3C Carousel pattern leaves focus on the previous and next buttons, so people can press them as many times as they like.

Someone using a screen reader learns about the change from the live region, which announces the new slide without moving their focus.