HTML Carousel Slider: Code and No-Code Way

A carousel slider shows one or a few items at a time and lets visitors swipe or click to the next. The simplest HTML carousel is a flex row with CSS scroll-snap and two buttons. When you need autoplay, loop, fade or coverflow, a slider component saves the work. Both are below.
What is a carousel slider?
A carousel slider is a UI component that displays content in a horizontal (or vertical) sequence, letting users swipe or click through items one at a time. Think image galleries, testimonial rotators, product showcases, or feature highlights. The content slides into view with a transition effect while the rest stays hidden.
Good carousel sliders are touch-friendly on mobile, support keyboard navigation, and include visual indicators like dots or arrows so users know there's more content to see.
How do you build an HTML carousel slider with CSS and JavaScript?
A horizontal row of slides that scrolls and snaps into place, with buttons that scroll by one slide. Touch swiping works because it is native scrolling.
<div class="carousel" id="carousel">
<div>Slide 1</div>
<div>Slide 2</div>
<div>Slide 3</div>
<div>Slide 4</div>
</div>
<button id="prev" aria-label="Previous slide">Prev</button>
<button id="next" aria-label="Next slide">Next</button>
.carousel { display: flex; gap: 16px; overflow-x: auto; scroll-snap-type: x mandatory; scrollbar-width: none; }
.carousel > * { flex: 0 0 80%; scroll-snap-align: center; }
const c = document.getElementById('carousel');
const step = () => c.firstElementChild.getBoundingClientRect().width + 16;
document.getElementById('prev').onclick = () => c.scrollBy({ left: -step(), behavior: 'smooth' });
document.getElementById('next').onclick = () => c.scrollBy({ left: step(), behavior: 'smooth' });
Change flex-basis to show more slides at once (for example 33% for three). This version has no autoplay, no loop and no pagination dots; adding those is where a library or a component starts to pay off.
How do you add a carousel slider with HTML attributes?
The Swiper transform from OneType Transforms is a premium component: choose a paid plan at transforms.onetype.ai/pricing and take your personal script tag from the Get Started page.
<script src="https://cdn.transforms.onetype.ai/bundle/{YOUR-TOKEN}.js"></script>
Wrap your slides in a container with the ot="swiper" attribute. Every direct child becomes a slide:
<div ot="swiper" ot-navigation="true" ot-pagination="bullets">
<div>Slide 1</div>
<div>Slide 2</div>
<div>Slide 3</div>
<div>Slide 4</div>
</div>
You can put anything inside a slide: images, cards, text, video embeds, whole sections.
Transition effects
Six built-in effects you can switch with a single attribute:
- slide - standard horizontal movement (default)
- fade - cross-dissolve between slides
- coverflow - 3D perspective with depth and rotation, great for galleries
- cards - stacks slides like a deck of cards
- cube - 3D cube rotation
- flip - 3D flip transition
To use the coverflow effect:
<div ot="swiper" ot-effect="coverflow" ot-slides-per-view="2" ot-centered="true" ot-loop="true">
<div>Slide 1</div>
<div>Slide 2</div>
<div>Slide 3</div>
<div>Slide 4</div>
<div>Slide 5</div>
</div>
For the cards effect:
<div ot="swiper" ot-effect="cards" ot-grab-cursor="true" style="max-width: 320px;">
<div>Card 1</div>
<div>Card 2</div>
<div>Card 3</div>
</div>
Navigation and pagination options
You control navigation and pagination with HTML attributes. No CSS to write - the transform includes styled controls by default.
Navigation arrows: Add ot-navigation="true" to show prev/next arrow buttons.
Pagination: Set ot-pagination to one of three styles - bullets (dots), fraction (1/5 counter), or progressbar (progress bar).
Scrollbar: Add ot-scrollbar="true" for a draggable scrollbar beneath the slider.
You can combine all three at once. Here's a slider with arrows, fraction pagination, and multiple visible slides:
<div ot="swiper" ot-navigation="true" ot-pagination="fraction" ot-slides-per-view="3" ot-space-between="20">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
</div>
Autoplay and looping
Want the carousel to advance automatically? Add ot-autoplay="true". The default delay is 3 seconds between slides. Change it with ot-autoplay-delay (in milliseconds):
<div ot="swiper" ot-autoplay="true" ot-autoplay-delay="5000" ot-autoplay-pause="true" ot-loop="true">
<div>Slide 1</div>
<div>Slide 2</div>
<div>Slide 3</div>
</div>
ot-autoplay-pause="true" stops the autoplay when users hover over the carousel - gives them time to read or interact. ot-loop="true" creates an infinite loop so the carousel cycles back to the first slide after the last one.
If you want the slider to jump back to the first slide instead of seamlessly looping, use ot-rewind="true" instead of loop.
Multiple slides and spacing
Show more than one slide at a time with ot-slides-per-view. Add gaps between slides with ot-space-between (pixels):
<div ot="swiper" ot-slides-per-view="3" ot-space-between="24" ot-centered="true">
<div>Card 1</div>
<div>Card 2</div>
<div>Card 3</div>
<div>Card 4</div>
<div>Card 5</div>
</div>
Set ot-slides-per-view="auto" to size slides based on their content width instead of dividing the container equally. Pair it with ot-free-mode="true" for a free-scrolling layout where slides don't snap to fixed positions.
All configuration options
Every option is controlled via ot- HTML attributes. Here's the full list:
- ot-direction - horizontal or vertical (default: horizontal)
- ot-slides-per-view - number of visible slides or "auto" (default: 1)
- ot-space-between - gap between slides in pixels (default: 0)
- ot-speed - transition speed in milliseconds (default: 300)
- ot-loop - infinite loop (default: false)
- ot-rewind - rewind to first slide after last (default: false)
- ot-effect - slide, fade, coverflow, cards, cube, or flip (default: slide)
- ot-autoplay - enable autoplay (default: false)
- ot-autoplay-delay - autoplay delay in ms (default: 3000)
- ot-autoplay-pause - pause autoplay on hover (default: false)
- ot-pagination - bullets, fraction, or progressbar
- ot-navigation - show prev/next arrows (default: false)
- ot-scrollbar - show draggable scrollbar (default: false)
- ot-centered - center the active slide (default: false)
- ot-free-mode - free scrolling without snapping (default: false)
- ot-grab-cursor - show grab cursor on hover (default: false)
- ot-autoheight - adjust height to active slide (default: false)
- ot-keyboard - enable keyboard navigation (default: false)
- ot-mousewheel - enable mousewheel navigation (default: false)
Works on every platform
The carousel works anywhere you can add a script tag and HTML. Step-by-step guides: Webflow, WordPress, Elementor, Squarespace, Shopify and Framer. On static HTML, React, Vue or Next.js, add the script tag and the HTML.
Common use cases
- Image galleries - showcase photography, portfolio pieces, or product images with coverflow or fade effects
- Testimonial sliders - rotate customer reviews with autoplay and pagination dots
- Product showcases - display multiple products with slides-per-view and spacing
- Hero banners - full-width autoplay carousel for homepage headers
- Card carousels - feature cards, pricing plans, or team members in a swipeable row
- Logo bars - infinite-loop carousel of client or partner logos
Building the whole website, not one effect?
Describe your business in one sentence and Divhunt Studio builds the site for you. You can start from one of the website templates and change anything by chatting. Building is free; you pay when you publish.
FAQ
How do I make a carousel slider in HTML and CSS only?
Use a flex row with overflow-x: auto and scroll-snap-type. Buttons or dots then need a little JavaScript.
How do I add autoplay to a carousel?
With the component, set ot-autoplay="true" and a delay in ot-autoplay-delay. In plain code, call scrollBy on a timer and pause it on hover and on focus.
Is a carousel good for SEO and accessibility?
Keep the slide content in the page HTML (not loaded later), give every button a label, and do not auto-advance faster than people can read.
