Skip to content
IndexRequest a UIAboutContact

Swipe Carousel

A horizontal carousel with native scroll snapping, drag with the mouse, arrows and dots.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
Build a carousel 280x160px with four colour-block slides ("One" to "Four"), each exactly the width of the viewport. Use a flex row with overflow-x auto, scroll-snap-type x mandatory, scroll-snap-align start and hidden scrollbars. Add mouse drag: on pointerdown with pointerType mouse, disable snapping, follow the pointer by updating scrollLeft, and on release re-enable snapping and smooth-scroll to the nearest slide. Add Previous and Next buttons (disabled at the ends) and a row of four dots; the active dot (derived from scrollLeft on scroll) becomes a 18px wide pill in 200ms. Clicking a dot scrolls to its slide. The region has aria-roledescription carousel and each slide has aria-label "n of 4". Arrow keys scroll when the track is focused. Under prefers-reduced-motion use instant scrolling.

About this pattern

Definition

A row of slides that scrolls horizontally and always settles with one slide aligned. Touch swipes natively, a mouse can drag, and buttons and dots provide other ways to move.

Purpose & UX principles

  • Shows a set of equal items in limited space.
  • CSS scroll snapping gives a native, smooth feel and keeps momentum; JavaScript only adds mouse dragging and indicators.
  • Dots and arrow buttons are real buttons with labels, so keyboard users are not forced to swipe.

Interaction states

  • Slide active (dot filled), dragging (cursor grabbing, snap suspended), arrows disabled at the ends, focus-visible.

Motion specification

  • Native scroll-snap with scroll-behavior: smooth for button navigation.
  • Active dot widens in 200ms.
  • Reduced motion: navigation jumps without smooth scrolling.

Responsive behaviour

  • One slide per view on phones; slides keep a fixed ratio and fill the width.
  • Arrow buttons hide on touch-only devices if space is tight.

Code

HTML
<div class="car" role="region" aria-roledescription="carousel" aria-label="Featured">
  <div class="track" id="track" tabindex="0">
    <div class="s s1" aria-label="1 of 4">One</div><div class="s s2" aria-label="2 of 4">Two</div><div class="s s3" aria-label="3 of 4">Three</div><div class="s s4" aria-label="4 of 4">Four</div>
  </div>
  <div class="ctl"><button id="prev" aria-label="Previous slide">‹</button><span class="dots" id="dots"></span><button id="next" aria-label="Next slide">›</button></div>
</div>

Related patterns

Bottom Sheet

↗
Bottom SheetGestures & Touch

Tab Navigation

↗
Tab NavigationNavigation

Pagination

↗
PaginationNavigation