CSS Carousels
In this post we are going to discuss how we can make CSS-only carousels

Hey all, back again with another little side mission to help me in my day to day. For the most part when it comes to carousels I like to use Swiper for quick and easy carousels.
Carousels
For the most part this works great and does pretty much everything I want from it; however, in later versions I’ve encountered issues with autoplay not working if there are not enough base items in the carousel to fill the width of the screen, which is fine but presents strange juddering and is not great for clients who don’t want to add that many items.
While there are workarounds for this for clients who just want a simple autoplaying carousel, I set about trying to look for CSS-only solutions.
Two different jobs
When I started digging, I realised that “carousel” covers two pretty different things:
- A slider – the user swipes or clicks through slides, one (or a few) at a time. Think product images or testimonials.
- A ticker – a strip of items that scrolls along on its own, like a row of client logos. This is what most of my clients actually mean when they ask for an “autoplaying carousel”.
CSS can handle both of these now, so let’s go through them one at a time.
Part 1: a slider with scroll snap
The foundation for any CSS carousel is scroll snap. It’s been supported in every major browser since 2020, so there’s nothing to worry about here. You make a container scroll horizontally, then tell the browser where each item should “snap” to when the user stops scrolling:
<ul class="slider" tabindex="0" aria-label="Example slides">
<li>Slide 1</li>
<li>Slide 2</li>
<li>Slide 3</li>
</ul>
.slider {
display: flex;
gap: 1rem;
overflow-x: auto;
scroll-snap-type: x mandatory;
/* Stop a swipe at the end from scrolling the page or going "back" */
overscroll-behavior-x: contain;
}
.slider > li {
flex: 0 0 100%; /* one slide at a time – try 80% to peek at the next one */
scroll-snap-align: center;
}
That’s it. No JavaScript, and you already get swiping on touch screens, trackpad and mouse-wheel scrolling, and keyboard scrolling with the arrow keys once the slider has focus. That’s what the tabindex="0" is for: some browsers won’t let you focus a scroll container without it, and the aria-label tells screen reader users what they’ve landed on. Because it’s just a list, screen readers can read every slide too.
The new bit: scroll buttons and markers
What scroll snap doesn’t give you is the previous/next arrows and the little dots underneath. This is where the really exciting new stuff comes in. Chrome and Edge (from version 135) let you generate both of these in CSS:
.slider {
scroll-marker-group: after; /* creates a container for the dots */
}
/* Previous and next buttons */
.slider::scroll-button(left) {
content: "◄";
}
.slider::scroll-button(right) {
content: "►";
}
/* One dot per slide */
.slider > li::scroll-marker {
content: "";
width: 1rem;
height: 1rem;
border: 2px solid currentColor;
border-radius: 50%;
}
/* The dot for the slide that's currently in view */
.slider > li::scroll-marker:target-current {
background: currentColor;
}
The browser turns these into real, focusable buttons, gives them accessible names, and even disables the arrows when you reach the end. Pretty cool!
The catch is that this is still marked as experimental, and at the time of writing it doesn’t work in Firefox or Safari at all. The good news is those browsers just ignore it, so people still get a perfectly usable swipeable slider, just without the arrows and dots. For a client project where the arrows really matter, I’d still add real <button> elements with a small bit of JS for now, and come back to this once it lands everywhere.
Here’s the whole thing working. Swipe it, scroll it, or click into it and use your arrow keys:
Show the full code
<ul class="slider" tabindex="0" aria-label="Example slides">
<li>Slide 1</li>
<li>Slide 2</li>
<li>Slide 3</li>
<li>Slide 4</li>
<li>Slide 5</li>
</ul>/* The slider: works in every browser */
.slider {
display: flex;
gap: 16px;
overflow-x: auto;
scroll-snap-type: x mandatory;
/* Stop a swipe at the end from scrolling the page or going "back" */
overscroll-behavior-x: contain;
list-style: none;
margin: 0;
padding: 0;
}
.slider:focus-visible {
outline: 3px solid #ffd100;
outline-offset: 4px;
}
.slider > li {
flex: 0 0 100%; /* one slide at a time - try 80% to peek at the next one */
scroll-snap-align: center;
display: grid;
place-items: center;
min-height: 220px;
border-radius: 16px;
background: #007847;
color: #fff;
font: 700 32px/1.2 system-ui, sans-serif;
}
.slider > li:nth-child(even) {
background: #005c36;
}
/* Arrows and dots: Chrome and Edge 135+ only, other browsers ignore this */
.slider {
anchor-name: --slider;
scroll-marker-group: after;
}
.slider::scroll-button(*) {
position: absolute;
position-anchor: --slider;
align-self: anchor-center;
width: 48px;
height: 48px;
border: 0;
border-radius: 50%;
background: #fff;
color: #000;
font-size: 20px;
cursor: pointer;
}
.slider::scroll-button(*):focus-visible {
outline: 3px solid #ffd100;
outline-offset: 2px;
}
.slider::scroll-button(*):disabled {
opacity: 0.4;
cursor: default;
}
.slider::scroll-button(left) {
content: "◄";
left: calc(anchor(left) + 16px);
}
.slider::scroll-button(right) {
content: "►";
right: calc(anchor(right) + 16px);
}
.slider::scroll-marker-group {
display: flex;
justify-content: center;
gap: 12px;
margin-top: 16px;
}
.slider > li::scroll-marker {
content: "";
width: 16px;
height: 16px;
border: 2px solid #007847;
border-radius: 50%;
}
/* The dot for the slide that's currently in view */
.slider > li::scroll-marker:target-current {
background: #007847;
}Part 2: the autoplaying ticker
This is the one that actually fixes my Swiper problem. The trick is to put the items in the HTML twice, side by side, then slide both copies to the left forever. When the first copy has moved completely out of view, the second one is sitting exactly where the first one started, so the loop is seamless.
<div class="ticker">
<ul class="ticker-group">
<li>Logo 1</li>
<li>Logo 2</li>
<li>Logo 3</li>
</ul>
<!-- An exact copy, hidden from screen readers and keyboard users -->
<ul class="ticker-group" aria-hidden="true" inert>
<li>Logo 1</li>
<li>Logo 2</li>
<li>Logo 3</li>
</ul>
</div>
.ticker {
--gap: 2rem;
display: flex;
gap: var(--gap);
overflow: hidden;
}
.ticker-group {
display: flex;
flex-shrink: 0;
gap: var(--gap);
justify-content: space-around;
/* Each copy is always at least as wide as the ticker... */
min-width: 100%;
}
@keyframes ticker-scroll {
to {
/* ...so moving it one full width (plus the gap) loops perfectly */
translate: calc(-100% - var(--gap));
}
}
That min-width: 100% is the line that fixes the problem I had with Swiper. Each copy is always at least as wide as the ticker, so even if a client only gives me three logos, they just spread out to fill the space and the loop still lines up. No empty gaps and no juddering.
Notice the aria-hidden and inert on the copy as well. Without those, screen reader users would hear every logo twice, and if the items were links, keyboard users would have to tab through them all twice.
Making it accessible
An animation that runs forever needs a bit of care. Some people get motion sickness from things moving on screen, and the accessibility guidelines (WCAG 2.2.2) say that anything moving automatically for more than five seconds needs a way to pause it. So here are the three things I make sure the ticker does.
1. Only move if the user is happy with motion. Rather than adding the animation and then turning it off for reduced motion, I only add it inside a prefers-reduced-motion: no-preference query. Anyone who has asked their device for less motion just gets a normal static row:
@media (prefers-reduced-motion: no-preference) {
.ticker-group {
animation: ticker-scroll 20s linear infinite;
}
}
/* With reduced motion, there's no need for the copy */
@media (prefers-reduced-motion: reduce) {
.ticker-group[aria-hidden="true"] {
display: none;
}
.ticker-group {
flex-wrap: wrap;
}
}
2. Pause on hover and focus, so people can actually read (or click) something:
.ticker:hover .ticker-group,
.ticker:focus-within .ticker-group {
animation-play-state: paused;
}
3. Give people a pause control. Hover doesn’t help on a phone, so there needs to be a visible control too. I didn’t want to reach for JavaScript just for this, and it turns out a checkbox and :has() can do it:
<div class="ticker-wrapper">
<label>
<input type="checkbox" class="ticker-pause" />
Pause the logos
</label>
<div class="ticker">…</div>
</div>
.ticker-wrapper:has(.ticker-pause:checked) .ticker-group {
animation-play-state: paused;
}
A checkbox is a real form control, so it works with a keyboard and screen readers straight away, and :has() has worked in every major browser since late 2023.
Here it is with only three logos (the exact situation that tripped up Swiper for me). Hover over it, or tick the box to pause it. If you’ve got reduced motion turned on, you’ll just see a static row:
- Logo 1
- Logo 2
- Logo 3
Show the full code
<div class="ticker-wrapper">
<label class="ticker-control">
<input type="checkbox" class="ticker-pause" />
Pause the logos
</label>
<div class="ticker">
<ul class="ticker-group">
<li>Logo 1</li>
<li>Logo 2</li>
<li>Logo 3</li>
</ul>
<!-- An exact copy, hidden from screen readers and keyboard users -->
<ul class="ticker-group" aria-hidden="true" inert>
<li>Logo 1</li>
<li>Logo 2</li>
<li>Logo 3</li>
</ul>
</div>
</div>.ticker-wrapper {
display: grid;
gap: 12px;
}
.ticker-control {
display: inline-flex;
align-items: center;
gap: 8px;
justify-self: start;
font: 16px system-ui, sans-serif;
cursor: pointer;
}
.ticker-pause {
width: 20px;
height: 20px;
accent-color: #007847;
}
.ticker {
--gap: 32px;
display: flex;
gap: var(--gap);
overflow: hidden;
}
.ticker-group {
display: flex;
flex-shrink: 0;
gap: var(--gap);
justify-content: space-around;
/* Each copy is always at least as wide as the ticker... */
min-width: 100%;
list-style: none;
margin: 0;
padding: 0;
}
.ticker-group li {
padding: 16px 32px;
border-radius: 999px;
background: #007847;
color: #fff;
font: 700 20px system-ui, sans-serif;
white-space: nowrap;
}
@keyframes ticker-scroll {
to {
/* ...so moving it one full width (plus the gap) loops perfectly */
translate: calc(-100% - var(--gap));
}
}
/* Only move if the user is happy with motion */
@media (prefers-reduced-motion: no-preference) {
.ticker-group {
animation: ticker-scroll 20s linear infinite;
}
}
/* Pause on hover, on focus, or with the checkbox */
.ticker:hover .ticker-group,
.ticker:focus-within .ticker-group,
.ticker-wrapper:has(.ticker-pause:checked) .ticker-group {
animation-play-state: paused;
}
/* With reduced motion: a static row, no copy and no pause control */
@media (prefers-reduced-motion: reduce) {
.ticker-group[aria-hidden="true"],
.ticker-control {
display: none;
}
.ticker-group {
flex-wrap: wrap;
}
}Wrapping up
So where did I land? For the simple autoplaying carousels my clients keep asking for, the CSS ticker is now my go-to. It’s a handful of lines, it doesn’t need a library, and it doesn’t care how many items it’s given. For proper sliders, scroll snap does most of the work today, and once scroll buttons and markers land in Firefox and Safari, I think a lot of us can say goodbye to carousel libraries altogether.
If you’d like to dig in further, MDN has a great guide on creating CSS carousels, and the Chrome team put together a CSS carousel gallery with loads of examples.