webdevpuneet.com

CSS Carousel Without JavaScript: scroll-marker Dots

CSS Carousel Without JavaScript: scroll-marker Dots · Carousels · Plain HTML, CSS & JS · Live preview

About this UI Snippet

A Carousel Built From CSS Pseudo-Elements, Not a Library

Screenshot of the CSS Carousel Without JavaScript: scroll-marker Dots snippet rendered live

Chrome now builds carousel controls for you. Give a scroll container scroll-marker-group and the browser generates a dot for every slide, marks the one in view, and adds previous and next buttons you can style like any other element. This snippet is a five-slide travel carousel that uses exactly that, with no slider library and no script on the happy path.

Dots from ::scroll-marker

Each .nc-slide::scroll-marker becomes one dot inside the group that scroll-marker-group: after places below the track. The active dot uses :target-current, so the browser keeps it in sync with the slide in view and the dot stretches into a pill with a plain transition. Dots are real focusable controls and pressing one scrolls to its slide.

Arrows from ::scroll-button

::scroll-button(inline-start) and (inline-end) are generated buttons that scroll the container by one page. The content value takes an alt text after a slash, so screen readers announce "Previous slide" instead of an arrow glyph. They disable themselves at either end, which the CSS styles with :disabled. Anchor positioning pins them to the middle of the track.

Scroll snap does the motion

The track is a flex row with scroll-snap-type: x mandatory and each slide snaps to centre. Touch swipe, trackpad, arrow keys and wheel all work without any handler, and overscroll-behavior-x: contain stops the page from navigating back on a hard swipe.

Fallback for other browsers

A short script tests CSS.supports('selector(::scroll-button(*))'). Where it fails, two ordinary buttons appear and call scrollBy. The slides, snapping and swiping still work everywhere, so nothing breaks while support spreads.

Customising

Change flex-basis to show two or three slides at once, recolour the dots, or move the group above the track. For a drag-driven version with momentum see the magnetic snap carousel.

Accessibility

The generated buttons and dots are real focusable controls, the arrows are announced as "Previous slide" and "Next slide", and the track itself is focusable so keyboard users can scroll it with the arrow keys. Smooth scrolling is switched off for people who prefer reduced motion.

What's included

Features

Generated dots
One ::scroll-marker per slide, with no markup for the indicators.
Generated arrows
::scroll-button previous and next that disable at the ends.
Active-dot styling
:target-current stretches the current dot into a pill.
Accessible names
The content alt text gives each arrow a spoken label.
Scroll snap motion
Swipe, wheel, keys and trackpad work with no event handlers.
Anchor-positioned controls
Arrows are pinned to the track with position-anchor, not wrapper hacks.
Feature-detected fallback
Two buttons appear only where CSS.supports fails.
Reduced motion
Smooth scrolling and dot transitions switch off for users who ask.

Step by step

How to Use

  1. 1
    Give the list scroll-snapSet overflow-x: auto and scroll-snap-type: x mandatory on the ul, and scroll-snap-align: center on each li. This alone gives you a swipeable carousel in every browser.
  2. 2
    Turn on the generated dotsAdd scroll-marker-group: after to the list and a ::scroll-marker with content: "" to each slide. Chrome creates one dot per slide and fills :target-current for the one in view.
  3. 3
    Turn on the generated arrowsStyle ::scroll-button(inline-start) and (inline-end). Put the spoken name after a slash in content, as in content: "→" / "Next slide".
  4. 4
    Pin the arrows to the trackName the list with anchor-name: --nc, then use position-anchor and top: anchor(center) so the buttons sit over the edges without a wrapper.
  5. 5
    Keep the fallback scriptIt tests CSS.supports for ::scroll-button and, only if that fails, reveals two ordinary buttons that call scrollBy by one container width.
  6. 6
    Show more than one slideChange flex: 0 0 100% to 0 0 calc(50% - 8px) for two slides at a time and keep scroll-snap-align on each.

Real-world uses

Common Use Cases

Content-site feature rows
Add a swipeable strip of featured articles or destinations to a blog or travel page without shipping a slider library or its bundle weight.
Product and testimonial strips
Show reviews or product cards that scroll sideways on a phone, with dots that tell shoppers how many there are and where they are.
Performance-budget projects
Replace a 30 KB carousel package on a site judged on Core Web Vitals; the controls cost a few lines of CSS and no main-thread work.
Photo and itinerary strips
On a holiday or estate-agent page, let visitors swipe through five or six photos or trip days, with dots showing how many are left, and no gallery plugin to keep updated.

Got questions?

Frequently Asked Questions

Chromium browsers from version 135 (Chrome and Edge). Safari and Firefox do not support them yet, which is why the snippet includes a feature test and a two-button fallback. Snapping and swiping work in every browser regardless.

Not in supporting browsers. The script only checks CSS.supports and, if the pseudo-elements are missing, reveals fallback buttons. You can delete it if you accept a plain swipe carousel without arrows in older browsers.

The dots and arrows are real focusable controls generated by the browser, the arrows get spoken names from the content alt text, and the track is focusable for keyboard scrolling. Add a clear aria-label for the list and keep contrast high.

Change the slide's flex-basis from 100% to 50% or 33.33% (subtract the gap), and keep scroll-snap-align on each slide. The dots then represent each slide, so consider fewer slides or grouping them.

Copy & paste

Source Code

<section class="nc-stage">
  <h1 class="nc-title">Weekend trips, no JavaScript</h1>
  <div class="nc-frame">
    <ul class="nc-carousel" aria-label="Weekend trip ideas" tabindex="0">
      <li class="nc-slide" style="--h:18"><span>01</span><h2>Lake District</h2><p>Two nights, one ferry, zero traffic.</p></li>
      <li class="nc-slide" style="--h:350"><span>02</span><h2>Isle of Skye</h2><p>Fairy pools and a very long drive.</p></li>
      <li class="nc-slide" style="--h:150"><span>03</span><h2>Peak District</h2><p>Hill walks and a pub at the end.</p></li>
      <li class="nc-slide" style="--h:285"><span>04</span><h2>Cornish Coast</h2><p>Cliff paths, pasties and surf.</p></li>
      <li class="nc-slide" style="--h:215"><span>05</span><h2>Norfolk Broads</h2><p>Slow boats and wide skies.</p></li>
    </ul>
    <div class="nc-fallback" hidden>
      <button type="button" data-dir="-1" aria-label="Previous slide">&#8592;</button>
      <button type="button" data-dir="1" aria-label="Next slide">&#8594;</button>
    </div>
  </div>
  <p class="nc-note" id="ncNote"></p>
</section>

Extend with AI

Take This Snippet Further With AI

Paste this snippet into an AI coding assistant and ask it to explain how ::scroll-marker, :target-current and ::scroll-button work and why the fallback is feature-detected. Useful follow-ups: show three slides at once on desktop, move the dots above the track, or add an autoplay that pauses on hover and focus. Ask it to keep the alt text on the arrow content values.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a carousel with no JavaScript carousel logic, using the CSS scroll-marker and scroll-button features, in plain HTML and CSS plus a tiny feature-detect script.

Requirements:
- A ul with five full-width slides (gradient cards with a number, heading and sentence) in a flex row with scroll-snap-type x mandatory, snap-align center, hidden scrollbar and overscroll-behavior-x contain. Make the list focusable and give it an aria-label.
- On the scroll container set scroll-marker-group: after and style ::scroll-marker-group as a centred flex row. Give each slide a ::scroll-marker (content '') as a small dot, and use :target-current to stretch the active dot into a wider pill with a transition.
- Add ::scroll-button(inline-start) and ::scroll-button(inline-end) with arrow content plus a slash alt text ("Previous slide", "Next slide"), round styling, :hover, :disabled, and anchor positioning (anchor-name on the track, position-anchor and top: anchor(center)) so they sit over the left and right edges.
- Script: if CSS.supports('selector(::scroll-button(*))') is false, unhide two normal fallback buttons that call scrollBy by one container width; otherwise show a note that the controls are native.
- Respect prefers-reduced-motion and work at phone width.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.