Front-end tutorial ·

How to Build a Carousel with HTML, CSS, and JavaScript

Build a carousel that visitors can swipe, scroll, or control with the keyboard. The example uses an HTML list, CSS scroll snap, and a small JavaScript script. Try it here, then save the full HTML file to adapt for your site.

Try the working carousel

Use Next and Previous, swipe across a card, or focus the slide area with Tab and press an arrow key. The counter follows the visible slide. Slides stay in place until you interact.

Download the complete HTML example Open the standalone demo

1. Mark up the slides and controls in HTML

Give the carousel a heading and place each slide in a list item. The list has a tab stop because it is a scrollable area. Use native buttons so Enter and Space activate them. The script reveals the controls after attaching their listeners.

<section class="carousel" aria-labelledby="carousel-title">
  <h1 id="carousel-title">Portfolio color studies</h1>
  <ul class="carousel-track" role="list" tabindex="0" aria-label="Portfolio slides">
    <li><h2>Aurora</h2></li>
    <li><h2>Grove</h2></li>
    <li><h2>Ember</h2></li>
  </ul>
  <div class="carousel-controls" hidden>
    <button type="button" data-step="-1">Previous</button>
    <span class="carousel-count" role="status" aria-atomic="true"></span>
    <button type="button" data-step="1">Next</button>
  </div>
</section>

2. Make a horizontal track with CSS scroll snap

Flexbox lays the slides side by side. overflow-x: auto makes the list scroll, and scroll-snap-align: start lines up each slide with the edge of the track. The example gives each slide the full track width. Keep position: relative on the track so the script can measure each slide's offset.

.carousel-track {
  position: relative;
  display: flex;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scroll-snap-type: x mandatory;
  gap: .8rem;
  margin: 0;
  padding: 0 0 .45rem;
  list-style: none;
  scrollbar-width: thin;
}

.carousel-track > li {
  flex: 0 0 100%;
  min-inline-size: 0;
  box-sizing: border-box;
  scroll-snap-align: start;
  padding: clamp(.8rem, 4vw, 1.25rem);
  border: 1px solid #514a71;
  border-radius: 1rem;
  background: #1b1d2e;
}

Swipe and horizontal scrolling still work when JavaScript is unavailable. Keep the track's focus outline visible. Place any button or link for a slide inside that slide's list item.

3. Add Previous, Next, and the slide count

The script finds the slide whose left edge is closest to the track's scroll position. Each button asks to move one slide, and the track's arrow keys do the same. Clamping the target keeps the carousel within its first and last slides.

function move(step) {
  update();
  const target = Math.max(0, Math.min(current + step, slides.length - 1));
  if (target === current) return;
  track.scrollLeft = offset(slides[target]);
  update();
}

Measuring each slide position accounts for the gap between cards. Update the count after both button navigation and manual scrolling. The example uses role="status" for the position, sets aria-disabled at each end, and leaves focus on the active control. Button navigation moves immediately. See the W3C carousel accessibility pattern for additional guidance.

4. Adapt it to your site

  1. Start with the downloaded file. Open it in your browser, then replace the sample headings and descriptions. Copy a list item to add another slide. The script counts the items automatically.
  2. Add your artwork. Replace a decorative color swatch with an image when the image carries information. Give meaningful images alternative text and specify their dimensions to reserve space before they load.
  3. Keep the three layers together. Move the section, CSS, and script into your project. Scope the example's global button and page styles to your component when merging them into an existing stylesheet.
  4. Test the first and last slides. Try Next, Previous, touch scrolling, and keyboard navigation at a narrow width. Check that the counter agrees with the slide and that the page itself fits the screen.

Choose a Lienzo carousel template

Each catalog entry has an interactive preview and a separate ZIP with the source, README, and license notices. If you want to follow both Lienzo templates on video, watch the carousel walkthrough with Spanish audio.

PASS IT ON

Know someone who could use this?

Share it with someone working on their next idea. A useful resource can make all the difference.

WhatsAppTelegramFacebookEmail

View link