CSS-only Carousel Slider

CSS-only Carousel Slider

As a frontend developer, my days are primarily filled with crafting JavaScript-based components using frameworks like Vue, React, or Angular. These frameworks excel at empowering me to create reusable frontend elements, whether it's buttons, modals, links, or accordions. However, as I reflect on my reliance on these frameworks, I'm prompted to question their necessity for components that may not require JavaScript at all.

Consider a button, for instance – at its core, it's merely a styled clickable area. So, why do I default to creating JavaScript-based components for such fundamental elements?

In this article, I aim to showcase the capabilities of modern browsers and demonstrate how we can build a fully functional carousel slider without relying on JavaScript. By doing so, I hope to encourage you to reconsider the need for JavaScript when developing frontend components. Let's explore the possibilities and challenge the status quo together.

In my opinion, a carousel slider should be simple. It's just a list with items representing each slide.

The container for the carousel (the UL element) takes up the whole width, set at 100%. Each slide item (let's say 3 items) also fills the width completely. When put together, they make a list with a horizontal scrollbar. The total width of the list is 300%, but the container remains at 100%, so we get a scrollbar.

This arrangement turns the list into a carousel. You can scroll sideways to see new slides, like swiping on a phone. We use CSS properties like "scroll-snap-type" and "scroll-behavior" to mimic this swipe effect. When you scroll or drag to see the next slide, the browser automatically moves to the start of each slide. For example, if you scroll 70%, you'll see part of the first slide and most of the second. After you stop, the browser smoothly transitions to the second slide, since it's the most visible.

Knowing this, we're already on our way to creating a nearly perfect carousel slider:

See the Pen  CSS-only Carousel Slider | Basic Slider by Jasper Seinhorst (@jasper-seinhorst) on CodePen.

Adding navigation to see `next` and `previous` slide

An ideal carousel should be easy for everyone to use. While our Basic Carousel Slider works well on mobile devices with touchscreens, it's not as friendly for people using desktops with a mouse. To fix this, let's upgrade our slider by adding navigation buttons. You might think we need JavaScript for this, but I'll show you how to do it with CSS tricks.

Our carousel is basically a list with a scrollbar. We can use the browser's default scroll behavior, called anchor scrolling. When you click on a link, the browser smoothly scrolls to the right spot. Here's how to do it:

  1. Give each slide a unique name.
  2. Add links within each slide to move to the next or previous one.
  3. When you click a link, the browser will smoothly scroll to that slide.
  4. And with 'scroll-behavior: smooth' in CSS, the transition looks nice.

Let's give it a try!

See the Pen CSS-only Carousel Slider | Navigation by Jasper Seinhorst (@jasper-seinhorst) on CodePen.

Summary

In wrapping up, we've seen how HTML and CSS alone can create impressive components like the carousel slider. While JavaScript usually steals the show, we've proven that advanced features are achievable without it. By sticking to CSS-only solutions, we simplify our code and boost accessibility and performance. While there might be limits to how far we can push component behavior, HTML and CSS cover most frontend needs well. So, next time you're starting a project, remember the power of CSS-only components. Keep it simple, challenge the norm, and open up a world of frontend possibilities.

Comments

There are no comments yet, leave yours below.

Leave a comment

Do you have an addition, question or experience related to this article? Share it below.

Comments are briefly reviewed before they appear.

Read more about:

Dark mode on your website

A website can respond to the visitor's system appearance through prefers-color-scheme. This article introduces dark-mode styles and switching images with the same media query, using the browser support available at the time. A practical starting point for making more than just the page background adapt to a dark theme.

Continue reading

Do we still need Sass and Less?

Native CSS now covers many reasons we once reached for Sass or Less. But runtime variables are not compiler variables, and familiar nesting syntax can behave differently. This January 2026 comparison weighs where a preprocessor still earns its place, with examples and a feature checklist to help you choose based on what your project actually uses.

Continue reading

The bullshit of implementing web accessibility

The bullshit of implementing web accessibility

An accessibility audit can turn green while the next feature introduces the same barriers again. I explain why a round of fixes is not enough, and how design, development, content and testing must share responsibility. Accessibility needs to survive the next release, not just the audit.

Continue reading