Interactive
Autoplay carousel
Adds autoplay to the CSS carousel: pauses on hover, on focus, in a background tab and with reduced motion, has a play and pause button, and the current dot fills up until the next slide. Needs monsoon.js.
Needs monsoon.js. The styles work on their own, and the behavior comes from the optional interactive layer: add <script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script> or import 'monsooncss/js'. See the interactive layer.
Example
<div class="carousel-auto max-w-xl" data-m-carousel-auto data-m-carousel-auto-interval="4000">
<div class="carousel carousel-full">
<div id="auto-1" class="carousel-item"><div class="aspect-video w-full bg-gradient-to-br from-primary to-accent grid place-items-center text-white text-xl font-semibold">Plan</div></div>
<div id="auto-2" class="carousel-item"><div class="aspect-video w-full bg-gradient-to-br from-info to-success grid place-items-center text-white text-xl font-semibold">Build</div></div>
<div id="auto-3" class="carousel-item"><div class="aspect-video w-full bg-gradient-to-br from-warning to-danger grid place-items-center text-white text-xl font-semibold">Ship</div></div>
</div>
<nav class="carousel-nav"><a class="carousel-dot" href="#auto-1" aria-label="Slide 1"></a><a class="carousel-dot" href="#auto-2" aria-label="Slide 2"></a><a class="carousel-dot" href="#auto-3" aria-label="Slide 3"></a></nav>
</div>JavaScript API
Attribute
data-m-carousel-auto
Options
| Option | Description |
|---|---|
data-m-carousel-auto-interval="4000" | |
data-m-carousel-auto-force (play even with reduced motion) |
Methods
| Method | Description |
|---|---|
Monsoon.carouselAuto(el).next() | |
Monsoon.carouselAuto(el).prev() | |
Monsoon.carouselAuto(el).go(2) | |
Monsoon.carouselAuto(el).play() | |
Monsoon.carouselAuto(el).pause() |
Events
| Event | Description |
|---|---|
monsoon:slide { index } |
Classes
7 classes. Click a name to copy it. Utilities on the same element always win over component defaults, so any of them can be adjusted with plain utility classes.
| Class | Type | What it does |
|---|---|---|
| Part | Part of the autoplay carousel. On a <div> in the example. Sets block display, overflow, width, background, generated content and absolute position.CSS |
|
| Modifier | Modifier, combined with the carousel class. Not used in the example above. Sets grid display, alignment, width, height, margin and text color.CSS |
|
| Base | Base class of the autoplay carousel. On a <div> in the example. Sets flex display, gap, overflow, corner radius and hidden by default.CSS |
|
| Part | Part of the autoplay carousel. On a <div> in the example. Sets flex display and alignment.CSS |
|
| Modifier | Modifier, combined with the carousel class. On a <div> in the example. Sets alignment, and --carousel-gap.CSS |
|
| Part | Part of the autoplay carousel. On a <nav> in the example. Sets flex display, alignment, gap and padding.CSS |
|
| Part | Part of the autoplay carousel. On an <a> in the example. Sets width, height, corner radius, background and transition.CSS |
Usage
The styles ship inside the same stylesheet as the utilities, and the behavior comes from the optional monsoon.js script, which enhances any element with a matching data-m attribute, including markup added later.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/monsooncss">
<script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script>
<!-- Autoplay carousel: start from the base class, then add modifiers -->
<div class="carousel-auto max-w-xl" data-m-carousel-auto data-m-carousel-auto-interval="4000">
<div class="carousel carousel-full">
<div id="auto-1" class="carousel-item"><div class="aspect-video w-full bg-gradient-to-br from-primary to-accent grid place-items-center text-white text-xl font-semibold">Plan</div></div>
<div id="auto-2" class="carousel-item"><div class="aspect-video w-full bg-gradient-to-br from-info to-success grid place-items-center text-white text-xl font-semibold">Build</div></div>
<div id="auto-3" class="carousel-item"><div class="aspect-video w-full bg-gradient-to-br from-warning to-danger grid place-items-center text-white text-xl font-semibold">Ship</div></div>
</div>
<nav class="carousel-nav"><a class="carousel-dot" href="#auto-1" aria-label="Slide 1"></a><a class="carousel-dot" href="#auto-2" aria-label="Slide 2"></a><a class="carousel-dot" href="#auto-3" aria-label="Slide 3"></a></nav>
</div>Theme it with --seed on any ancestor, for example style="--seed: #0d9488", and force a scheme with class="dark" or class="light" on a wrapper.
Accessibility
- Wrap navigation in
navwith anaria-labelwhen there is more than one on the page, and mark the current link witharia-current="page". - Every interactive part shows a visible focus ring with
:focus-visible, and tone colors pair with-foregroundtokens that keep text at 4.5:1 or better in light and dark mode.