Carousel
alphaA row of slides you go through one at a time (or several at a time): previous and next buttons, one dot per page, and, if requested, automatic rotation with a pause button. It scrolls with native scroll-snap, so it also moves with a finger, the wheel, or the keyboard on the track.
Content · TaishoDS Core, in plain HTML.
Examples
One working example per case. Each example is the HTML of its preview—copy it as is, and with Core’s CSS and JS loaded, it looks and works the same.
Team projects
News with automatic rotation
Two per view in a wide container
Showing the next slide
Many slides
While loading
A single slide
Class reference
Every value of each modifier, with its HTML ready to copy.
Responsive
Each slide’s width comes from a utility: w-full, w-1/2, or w-1/3. With the container prefix (@xs:w-1/2) it changes with the carousel’s own width.
When to use
- Several items of equal weight compete for the same spot, and seeing one at a time is enough: announcements, project templates, screenshots of a deployment.
- The order matters little, and any slide can be reached with the dots.
- On narrow screens a row of cards has to fit without shrinking: two per view when the carousel has room (
@xs:w-1/2, from 512 px of its own width) and one in a narrow column.
When not to use
- The content matters and nobody should miss it. What’s out of view is rarely seen →
cardin agrid. - They’re different sections of the same thing (Overview, Activity, Settings) →
tabs. - They’re steps with an order to follow →
stepper. - It’s a long list of data to compare →
tableorpagination.
Do
- The
aria-labelsays what it shows (“News”, “Team projects”), without the word “carousel”: the role already announces it. - Automatic rotation is for short notices with a long interval (5 seconds or more). It leaves time to read, and the pause button comes first.
- Each slide makes sense on its own. Someone who lands on the third one hasn’t seen the first two.
Don’t
- Don’t hide a primary action in a slide that isn’t in view.
- Don’t add automatic rotation to a carousel with forms or links people need to use.
- Don’t overlay the controls on the slides. They cover the content and the focus.
TaishoDS Core
Needs Core’s JS, which handles focus, keyboard, and state.
The current slide, the dots, the ends, and automatic rotation are state. Without JS the track is a scroll-snap row you can scroll, with no buttons or dots.
Core hooks into it through these attributes. They go in the markup, and there’s nothing to initialize.
data-ts-carouseldata-ts-carousel-autoplaydata-ts-carousel-controlsdata-ts-carousel-counterdata-ts-carousel-dotdata-ts-carousel-dotsdata-ts-carousel-indexdata-ts-carousel-loopdata-ts-carousel-nextdata-ts-carousel-pause-labeldata-ts-carousel-peekdata-ts-carousel-playdata-ts-carousel-play-labeldata-ts-carousel-prevdata-ts-carousel-slidedata-ts-carousel-statusdata-ts-carousel-track
What it does: Shows the controls and the dots, clones the model dot up to the number of pages (one per group of slides in view), and names each slide “2 of 5” if it has no aria-label. An IntersectionObserver tracks the current one; a ResizeObserver recalculates the pages when the width changes, and if the dots don’t fit they become a “3 / 12” counter. At the ends, previous and next get aria-disabled (unless data-ts-carousel-loop is set) and focus stays put. When a slide is reached, the role="status" region (data-ts-carousel-status) says “Slide 3 of 5” or the slide’s own name; while rotating it stays quiet. With automatic rotation, the pause button changes its name and icon without being replaced, and the current dot fills up during the interval (no bar with reduced motion).
| Attribute | What it does |
|---|---|
data-ts-carousel-autoplay | On the root. Rotates on its own every that many milliseconds (6000). It doesn’t start with prefers-reduced-motion: reduce (the root keeps is-paused and the button starts it) and it stops if the preference changes. It pauses while the pointer is over it, while the tab is hidden, and while it’s out of view; it stops when keyboard focus moves inside or the person navigates, and it doesn’t resume on its own. It needs the data-ts-carousel-play button. |
data-ts-carousel-loop | On the root. Next on the last slide goes back to the first (and vice versa), and the arrow keys on the dots wrap around. |
data-ts-carousel-index | On the root. The first slide in view, starting at 0. Core writes it; if your code changes it, the carousel scrolls without firing an event. |
data-ts-carousel-play-label, data-ts-carousel-pause-label | On the root. The name of the rotation button when stopped and when rotating. Without them, Core’s texts in the page’s language. |
data-ts-carousel-dot | The model dot, inside data-ts-carousel-dots. Core clones it and derives the name template from its aria-label (the first number is the dot’s and the last one, the total). |
data-ts-carousel-status | An empty <span class="ts sr-only" role="status"> region next to the track. Core writes the slide that was reached there. The track’s HTML doesn’t change when it scrolls, so without it the screen reader doesn’t announce the change. |
aria-busy="true" | On the root, while the slides load: no controls, dots, or rotation. Inside goes one slide with a ts skeleton (the “While loading” example). |
data-ts-carousel-peek | On the root. How much of the neighboring slide shows on each side, the hint that there’s more (32 is px; other lengths work too). For every carousel, the theme’s peek token. Defaults to 0px; without JS, with more than one slide, the next one shows anyway. |
ts:change | Fires with detail.index. From the buttons, the dots, and rotation it’s cancelable (if prevented, nothing changes). When swiping or using the wheel it has already happened and can’t be prevented (cancelable: false). |
From the CDN, without a build, load taisho.carousel.css along with taisho.base.css. Also load:
taisho.button.csstaisho.icon.csstaisho.skeleton.csstaisho.spinner.css
For how Core works in general, see Behavior.
Prevent a change
The buttons, the dots, and rotation fire a cancelable ts:change before changing. With preventDefault() the track doesn’t move, the dots and the ends stay as they were, and focus stays on the selected dot. In this example it doesn’t go past the third slide. When swiping, the change has already happened and can’t be prevented.
<section class="ts carousel" id="deployments" data-ts-carousel aria-roledescription="carousel" aria-label="Recent deployments">
…
</section>
<script type="module">
document.getElementById('deployments').addEventListener('ts:change', (e) => {
if (e.detail.index > 2) e.preventDefault();
});
</script>Theme tokens
Values you change from the theme, in themes.<theme>.components[…]. When a value has two, the second is for compact density.
| Variable | Type | Factory value |
|---|---|---|
--ts-carousel-radius | length | var(--ts-radius-xl) |
--ts-carousel-gap | length | var(--ts-space-half) |
--ts-carousel-dot-size | length | var(--ts-sizing-8) |
--ts-carousel-dot-color | color | var(--ts-color-field-border) |
--ts-carousel-dot-color-active | color | var(--color-gray-1000) |
--ts-carousel-dot-surface | color | var(--color-background-100) |
--ts-carousel-peek | length | 0px |
Accessibility
Implements the Carousel pattern from the WAI-ARIA APG.
- The root is a region with
aria-roledescription="carousel"and itsaria-label. Each slide is arole="tabpanel"witharia-roledescription="slide"and a “2 of 5” name (Core writes it if it’s missing). The dots are arole="tablist"ofrole="tab"elements witharia-selectedandaria-controlspointing to the slides of their page: a single Tab stop. - The control bar comes before the track and outside the slide area, so it doesn’t cover the content or the focus (WCAG 2.2, 2.4.11). The rotation button is first in the DOM and in the Tab order. It doesn’t use
aria-pressed: it changes its name (“Stop automatic rotation” / “Start…”) and its icon without being replaced. - At the ends, previous and next get
aria-disabled="true"rather thandisabled: the button keeps focus, which never falls tobody. - Rotation (WCAG 2.2.2) exists only with
data-ts-carousel-autoplay. It doesn’t start withprefers-reduced-motion: reduceand stops if the preference changes. It pauses while the pointer is over it, while the tab is hidden, and while it’s out of view; it stops when keyboard focus moves inside or on any navigation, and doesn’t resume on its own. While it runs, the current dot fills up during the interval (no bar with reduced motion). - When a slide is reached with the buttons, the dots, the keyboard, or a swipe, a hidden
role="status"region (data-ts-carousel-status) says “Slide 3 of 5”, or the slide’s own name. The track isn’t a live region: scrolling doesn’t change its HTML, so it wouldn’t announce anything. While rotating, the region stays quiet. - No mouse dragging (WCAG 2.2, 2.5.7). It moves with the buttons, the dots, the keyboard, or native scrolling. The dots have a 24×24 area even though they look 8 px wide (2.5.8). The current dot changes shape, not just color; with
forced-colorsit uses system colors. - With
aria-busy="true"on the root, the status region says “Loading” and there are no controls, dots, or rotation. When the dots don’t fit, the “3 / 12” counter is shown but not read (aria-hidden); next to it, hidden text says “Slide 3 of 12”. A single slide has norole="tabpanel"and no “1 of 1” name. - Without JavaScript, the track is a scroll-snap row you can focus and scroll with the arrow keys; the controls and the dots stay hidden.
Keyboard
On top of what the browser already does (Tab moves focus, Enter activates a button).
| Key | What it does |
|---|---|
| Tab | Moves through the rotation button (first), previous, next, the track, and the list of dots, which is a single stop. With keyboard focus inside, rotation stops and doesn’t resume on its own. |
| Enter/Space | On the rotation button, starts or stops it. On previous or next, changes the slide and focus stays on the button (at an end nothing happens). |
| →← | On a dot: they move to the next and previous dot and select it (the slide follows focus, as in Tabs). They stop at the ends; with loop they wrap around. On an RTL page they swap: ← moves forward and → moves back. |
| Home/End | On a dot: first and last page. |
| →←onthetrack | The track is a Tab stop and scrolls with the browser’s arrow keys; the slide left in view becomes the current one. |
Verification
What was actually tested. Anything that wasn’t tested says so—we’d rather say “not verified” than claim too much.
- axe WCAG 2.2 A + AA in a real browser, in light and dark mode, before every release.
- keyboard Key map documented above, checked against the component.
- screen reader Not verified with a screen reader. Test it in your own combination of components, not just here, where each one appears alone and on its happy path.