Clean Markup

Script-Free Carousels with CSS Scroll Buttons and Markers

Script-Free Carousels with CSS Scroll Buttons and Markers

Building an accessible carousel on the web has traditionally demanded hundreds of lines of JavaScript to track scroll offsets, wire previous and next buttons, update active pagination dots, manage roving tabindex, and handle keyboard navigation without trapping focus. Even carefully engineered script-based carousels often suffer from layout thrashing or out-of-sync indicators when users swipe with touch gestures or resize the viewport.

The CSS Overflow Level 5 carousel pseudo-elements—::scroll-button(), ::scroll-marker-group, ::scroll-marker, and the :target-current pseudo-class—shift all of that state management into the browser engine itself. By declaring native scroll buttons and per-item scroll markers directly in CSS on a snap-enabled scroll container, the user agent automatically generates keyboard-focusable controls, disables boundary buttons via :disabled, tracks the currently visible slide, and exposes clean accessibility semantics without a single line of client-side JavaScript.

Example

.gallery-track {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  scroll-marker-group: after;
}

.gallery-track::scroll-button(left) {
  content: "‹" / "Previous slide";
}

.gallery-track::scroll-button(right) {
  content: "›" / "Next slide";
}

.gallery-slide {
  flex: 0 0 100%;
  scroll-snap-align: start;
}

.gallery-slide::scroll-marker {
  content: "";
}

.gallery-slide::scroll-marker:target-current {
  background-color: #0f172a;
}

Implementation

<section class="carousel-section" aria-roledescription="carousel" aria-label="Featured Case Studies">
  <ul class="carousel-track">
    <li class="carousel-slide">
      <article class="slide-card">
        <h3>Edge Routing Architecture</h3>
        <p>Sub-20ms regional failover built on stateless edge workers and health checks.</p>
      </article>
    </li>
    <li class="carousel-slide">
      <article class="slide-card">
        <h3>Design Token Pipeline</h3>
        <p>Automated contrast verification and multi-theme CSS variable generation.</p>
      </article>
    </li>
    <li class="carousel-slide">
      <article class="slide-card">
        <h3>Zero-Runtime Typography</h3>
        <p>Variable font subsetting with fluid clamp scales across desktop and mobile.</p>
      </article>
    </li>
    <li class="carousel-slide">
      <article class="slide-card">
        <h3>Accessible Data Tables</h3>
        <p>Sticky headers, keyboard focus indicators, and responsive scroll shadows.</p>
      </article>
    </li>
  </ul>
</section>
/* ==========================================================================
   Native CSS Carousel: ::scroll-button(), ::scroll-marker & :target-current
   ========================================================================== */

.carousel-section {
  --carousel-gap: 1rem;
  --carousel-accent: #2563eb;
  --carousel-surface: #f8fafc;
  --carousel-border: #cbd5e1;

  max-inline-size: 56rem;
  margin-inline: auto;
  position: relative;
}

/* Base horizontal scroll-snap track (works everywhere as progressive baseline) */
.carousel-track {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 100%;
  gap: var(--carousel-gap);
  margin: 0;
  padding: 0.5rem 0 1rem;
  list-style: none;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  anchor-name: --carousel-anchor;
  scroll-marker-group: after;
}

@media (min-width: 44rem) {
  .carousel-track {
    grid-auto-columns: calc((100% - var(--carousel-gap)) / 2);
  }
}

.carousel-slide {
  scroll-snap-align: start;
  scroll-snap-stop: always;
}

.slide-card {
  block-size: 100%;
  padding: 1.5rem;
  border: 1px solid var(--carousel-border);
  border-radius: 0.875rem;
  background-color: var(--carousel-surface);
}

.slide-card h3 {
  margin: 0 0 0.5rem;
  color: #0f172a;
}

.slide-card p {
  margin: 0;
  color: #475569;
  line-height: 1.55;
}

/* Hide redundant scrollbar when native scroll markers are supported */
@supports selector(::scroll-marker-group) {
  .carousel-track {
    scrollbar-width: none;
  }
}

/* Native Previous / Next Scroll Buttons */
.carousel-track::scroll-button(left),
.carousel-track::scroll-button(right) {
  position: fixed;
  position-anchor: --carousel-anchor;
  position-area: block-center;
  inline-size: 2.5rem;
  block-size: 2.5rem;
  border: 1px solid var(--carousel-border);
  border-radius: 9999px;
  background-color: #ffffff;
  color: #0f172a;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 2px 8px rgb(15 23 42 / 0.12);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.carousel-track::scroll-button(left) {
  content: "‹" / "Previous slide";
  justify-self: start;
  margin-inline-start: 0.5rem;
}

.carousel-track::scroll-button(right) {
  content: "›" / "Next slide";
  justify-self: end;
  margin-inline-end: 0.5rem;
}

.carousel-track::scroll-button(*):disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.carousel-track::scroll-button(*):focus-visible {
  outline: 2px solid var(--carousel-accent);
  outline-offset: 2px;
}

/* Native Pagination Markers Container */
.carousel-track::scroll-marker-group {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  padding-block-start: 0.75rem;
}

.carousel-slide::scroll-marker {
  content: "";
  inline-size: 0.65rem;
  block-size: 0.65rem;
  border: 1.5px solid #64748b;
  border-radius: 9999px;
  background-color: transparent;
  cursor: pointer;
  transition: background-color 0.2s ease, inline-size 0.2s ease;
}

.carousel-slide::scroll-marker:target-current {
  inline-size: 1.5rem;
  border-color: var(--carousel-accent);
  background-color: var(--carousel-accent);
}

.carousel-slide::scroll-marker:focus-visible {
  outline: 2px solid var(--carousel-accent);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .carousel-track {
    scroll-behavior: auto;
  }

  .carousel-track::scroll-button(*),
  .carousel-slide::scroll-marker {
    transition: none;
  }
}

Main Point: CSS ::scroll-button() and ::scroll-marker pseudo-elements turn standard horizontal scroll-snap containers into keyboard-accessible, paginated carousels without client-side JavaScript state management.

Credits

Photo by Adrian Cuj on Unsplash

← Home