Automatic Image Slider in HTML and CSS Without JavaScript

You can build an automatic image slider with nothing but HTML and CSS. Put your images in a row inside a frame that hides the overflow, then slide the row along with a CSS @keyframes animation. Add a pause toggle, pause on hover and focus, and a prefers-reduced-motion fallback, and you’ve got a slider that’s light, works without scripts and respects your visitors.
This guide gives you the complete code, explains every part, and covers the mistakes that trip most people up. I’ve rewritten it from scratch, because the earlier version of this post had a sizing bug that made the slides fly off-screen.
Why Go CSS-Only?
- No scripts to load or maintain. There’s no library and no version updates, and it works on pages where you can’t add JavaScript.
- Smooth movement. The slider animates
transform, which browsers can usually animate without re-laying out the page. - Real images in the HTML. Every image is a normal
<img>with alt text, so it’s visible to assistive technology and to anything that reads your markup.
The trade-off is that CSS-only sliders are simple by design. There are no “next” buttons that jump to a specific slide, and no swiping while the animation runs. If you need those, a small script is the right tool.
Step 1: The HTML
Save this as index.html. Swap the image paths and alt text for your own.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS-only automatic image slider</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<section class="carousel" aria-label="Photo slideshow">
<input type="checkbox" id="pause-slider" class="pause-toggle">
<label for="pause-slider" class="pause-label">Pause slideshow</label>
<div class="slider" tabindex="0" role="region" aria-label="Slides">
<ul class="slides">
<li class="slide"><img src="images/lake.jpg" alt="Calm mountain lake at sunrise with mist over the water" width="1200" height="800"></li>
<li class="slide"><img src="images/city.jpg" alt="City skyline at night reflected in a river" width="1200" height="800"></li>
<li class="slide"><img src="images/forest.jpg" alt="Sunlight shining through tall pine trees in a forest" width="1200" height="800"></li>
<li class="slide"><img src="images/beach.jpg" alt="Empty sandy beach with turquoise water at midday" width="1200" height="800"></li>
<!-- Copy of the first slide so the loop restarts without a visible jump -->
<li class="slide slide--clone" aria-hidden="true"><img src="images/lake.jpg" alt="" width="1200" height="800"></li>
</ul>
</div>
</section>
</body>
</html>
What each part does:
.carouselis a labelled<section>, so screen reader users know what the region is.- The checkbox and label make a keyboard-accessible pause button with no JavaScript.
.slideris the frame that shows one slide at a time.tabindex="0"lets keyboard users focus it, which pauses the slideshow. In reduced-motion mode, it also lets them scroll with the arrow keys..slidesis the row of images. Using a list tells screen readers how many slides there are.- The clone repeats the first image. It’s
aria-hidden="true"with an emptyalt, so screen readers skip it. widthandheightlet the browser reserve space before the images load, which avoids layout shift.
Step 2: The CSS
Save this as styles.css next to the HTML file.
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
padding-block: 2rem;
background: #1e1e1e;
color: #fff;
font-family: system-ui, sans-serif;
}
/* Overall width of the slideshow, centred with auto margins */
.carousel {
width: min(100% - 2rem, 720px);
margin-inline: auto;
}
/* The frame: shows one slide, hides the rest */
.slider {
overflow: hidden;
border-radius: 12px;
}
.slider:focus-visible {
outline: 3px solid #4da3ff;
outline-offset: 4px;
}
/* The row of slides */
.slides {
display: flex;
margin: 0;
padding: 0;
list-style: none;
animation: autoplay 16s ease-in-out infinite;
}
/* Each slide is exactly as wide as the frame */
.slide {
flex: 0 0 100%;
}
.slide img {
display: block;
width: 100%;
height: auto;
aspect-ratio: 3 / 2;
object-fit: cover;
}
/* 4 real slides + 1 clone. Each slide gets 25% of the timeline:
it holds for 20% and spends 5% sliding to the next one. */
@keyframes autoplay {
0%, 20% { transform: translateX(0); }
25%, 45% { transform: translateX(-100%); }
50%, 70% { transform: translateX(-200%); }
75%, 95% { transform: translateX(-300%); }
100% { transform: translateX(-400%); }
}
/* Pause on hover, on keyboard focus, or when "Pause slideshow" is ticked */
.slider:hover .slides,
.slider:focus-within .slides,
.pause-toggle:checked ~ .slider .slides {
animation-play-state: paused;
}
.pause-label {
display: inline-block;
margin: 0 0 0.5rem 0.25rem;
cursor: pointer;
}
/* Visitors who asked their OS for less motion get a swipeable row instead */
@media (prefers-reduced-motion: reduce) {
.slides {
animation: none;
}
.slider {
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.slide {
scroll-snap-align: start;
}
.slide--clone,
.pause-toggle,
.pause-label {
display: none;
}
}
Step 3: How the Animation Works
The sizing. .slides is a flex row that’s the same width as the frame. Each .slide has flex: 0 0 100%, so each one is exactly one frame wide and the extra slides overflow to the right. That’s where overflow: hidden on .slider hides them.
The movement. A percentage in translateX() is measured against the element’s own width, and .slides is one frame wide. So translateX(-100%) moves the row exactly one slide to the left, -200% moves it two slides, and so on.
That’s the bug in my old version: it set the row to width: 300%, so -100% jumped three slides at once and showed empty space.
The timing. The animation runs for 16 seconds, and each of the four slides owns a quarter of it. Within each quarter, two keyframes share the same value (like 0%, 20%), which holds the slide still. The gap to the next keyframe (20% to 25%) is when it slides. That’s 3.2 seconds of viewing and 0.8 seconds of motion per slide. Because the hold keyframes don’t change anything, ease-in-out only affects the sliding parts.
The seamless loop. At 100%, the row has slid onto the clone, which looks identical to slide one. When the animation restarts at 0%, it jumps back to the real slide one, and nobody can tell. Without the clone, you’d see the whole row rewind at the end of every cycle.
Pausing. animation-play-state: paused freezes the animation where it is, and resuming continues from that point (per MDN). The ~ selector works because the checkbox comes before .slider in the same parent.
Accessibility Checklist
- Give users a way to stop it. WCAG success criterion 2.2.2 (Pause, Stop, Hide, Level A) requires a way to pause, stop or hide moving content that starts automatically and lasts more than five seconds. Hover alone doesn’t count, because touch and keyboard users can’t hover. That’s why the checkbox toggle is there.
- Respect reduced motion.
prefers-reduced-motion: reducematches when someone has turned on a “reduce motion” or “remove animations” setting on Windows, macOS, iOS or Android. In that case the code drops the animation and turns the slider into a row people can swipe or scroll, with each slide snapping into place. - Write useful alt text. Describe what matters in each image (“Empty sandy beach with turquoise water”), not “Image 1”. Purely decorative images and the clone get
alt="". - Don’t hide essential content in slides. Repeat anything important elsewhere on the page.
- Keep the focus outline so keyboard users can see where they are.
Accessibility overlays won’t fix a slider like this for you. I covered why in my post on the AccessiBe lawsuit.
Customising It
Change the speed. Change 16s. With four slides, each one gets a quarter of the total time.
Use a different number of slides. Give each slide 100 ÷ number of slides percent of the timeline, hold for most of it, and always finish on the clone. Here’s a three-slide version (three real slides plus a clone):
.slides {
animation: autoplay 12s ease-in-out infinite;
}
@keyframes autoplay {
0%, 28% { transform: translateX(0); }
33.333%, 61.333% { transform: translateX(-100%); }
66.666%, 94.666% { transform: translateX(-200%); }
100% { transform: translateX(-300%); }
}
Change the shape. Change aspect-ratio: 3 / 2 to 16 / 9 for widescreen, and the width of .carousel to fit your layout.
Common Pitfalls
- Setting a percentage width on the track. If
.slidesis wider than the frame, everytranslateXpercentage scales with it, and slides jump too far. - Keyframes that don’t match the slide count. If you add a fifth image but keep four steps, the last slide never shows, or you land on empty space.
- Forgetting the clone. Without it, the loop visibly rewinds.
- Lazy-loading every image. Never lazy-load the first slide, since it’s visible right away. If you add
loading="lazy"to the others, test that each image has loaded before it slides into view. - Mixed image sizes. Without
aspect-ratioandobject-fit: cover, portrait and landscape photos make the frame jump in height. - Hiding controls with
display: none. The old radio-button technique hid its inputs completely, so they did nothing for users. If a control matters, keep it reachable.
Since this slider adds no scripts, it pairs nicely with an edge cache. If you’re running WordPress, see my guide to getting the most from Cloudflare’s free plan.
FAQ
How do I pause a CSS slider on hover?
Set animation-play-state: paused on the moving element when its container is hovered, for example .slider:hover .slides. Add :focus-within for keyboard users and a checkbox toggle for touch users.
Why does my CSS slider show a blank space or skip slides?
Usually because the track has a percentage width like 300%. translateX percentages are measured against the track’s own width, so each step moves too far. Make the track the same width as the frame and give each slide flex: 0 0 100%.
How do I stop the slider jumping back to the first image?
Add a copy of the first slide at the end and finish the animation on it. When the loop restarts, the real first slide replaces its identical copy with no visible jump.
Does a CSS slider work with prefers-reduced-motion?
Yes, if you build it in. Wrap a rule in @media (prefers-reduced-motion: reduce) that sets animation: none, and offer a scrollable, scroll-snapping row instead.