Auto height
AutoHeight makes the carousel as tall as the selected snap's tallest slide, and follows the selection as it changes.
Auto height
<div class="carousel carousel--auto-height">
<div class="carousel__viewport">
<div class="carousel__container">
<div class="carousel__slide"><div class="carousel__card">1</div></div>
<div class="carousel__slide"><div class="carousel__card">2</div></div>
<div class="carousel__slide"><div class="carousel__card">3</div></div>
<div class="carousel__slide"><div class="carousel__card">4</div></div>
<div class="carousel__slide"><div class="carousel__card">5</div></div>
</div>
</div>
<div class="carousel__controls">
<div class="carousel__buttons">
<button class="carousel__button carousel__button--prev" type="button" aria-label="Previous slide">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m15 18-6-6 6-6" /></svg>
</button>
<button class="carousel__button carousel__button--next" type="button" aria-label="Next slide">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 18 6-6-6-6" /></svg>
</button>
</div>
<div class="carousel__dots"></div>
</div>
</div>.carousel--auto-height {
--slide-size: 100%;
}
/* AutoHeight writes the selected slide's height on the container. The slides keep their own heights (not the row's),
and the height changes smoothly */
.carousel--auto-height .carousel__container {
align-items: flex-start;
transition: height 0.3s ease;
}
/* Slides of different heights */
.carousel--auto-height .carousel__slide:nth-child(2) {
--slide-height: 18rem;
}
.carousel--auto-height .carousel__slide:nth-child(3) {
--slide-height: 9rem;
}
.carousel--auto-height .carousel__slide:nth-child(4) {
--slide-height: 22rem;
}
.carousel--auto-height .carousel__slide:nth-child(5) {
--slide-height: 11rem;
}
@media (prefers-reduced-motion: reduce) {
.carousel--auto-height .carousel__container {
transition: none;
}
}import Spill from "@bozdev/spill";
import AutoHeight from "@bozdev/spill/plugins/auto-height";
const root = document.querySelector(".carousel--auto-height");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");
// AutoHeight sets the container's height to the selected slide's; the CSS makes the change smooth
const api = Spill(root.querySelector(".carousel__viewport"), { align: "start" }, [AutoHeight()]);
// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
prev.disabled = !api.canScrollPrev();
next.disabled = !api.canScrollNext();
};
// A dot for each snap
const buildDots = () => {
dots.replaceChildren(
...api.scrollSnapList().map((_, index) => {
const dot = document.createElement("button");
dot.type = "button";
dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
dot.addEventListener("click", () => api.scrollTo(index));
return dot;
}),
);
};
const selectDot = () => {
[...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};
const refresh = () => {
buildDots();
selectDot();
toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
selectDot();
toggleButtons();
});AutoHeight has embla-carousel-auto-height’s options. It writes one thing, a height on the container; the CSS below does the rest.
Add it
import Spill from "@bozdev/spill";
import AutoHeight from "@bozdev/spill/plugins/auto-height";
const api = Spill(viewport, { align: "start" }, [AutoHeight()]);
It is also a named export of the package, import { AutoHeight } from "@bozdev/spill", and Spill.AutoHeight with a <script> tag. By attributes, it is auto-height:
<div data-spill-carousel='{"align":"start"}' data-spill-plugins="auto-height" aria-label="Reviews">
<div class="viewport" data-spill-viewport>
<div class="container">…</div>
</div>
</div>
In api.plugins() it is autoHeight.
The CSS it needs
.container {
display: flex;
align-items: flex-start; /* each slide keeps its own height */
transition: height 0.2s; /* the change, smooth */
}
@media (prefers-reduced-motion: reduce) {
.container {
transition: none;
}
}
align-items: flex-start is the one that matters. A flex row stretches its slides to its own height by default: AutoHeight would then measure the selected slide as tall as the row already is, and the carousel would stay as tall as its tallest slide.
The transition is up to you. The first height, written as the carousel is built, doesn’t animate: the container goes from auto to a length, and auto doesn’t transition. Before that — and without JavaScript — the row is as tall as its tallest slide.
What it does
It sets height on the container, inline, in pixels: the tallest of the selected snap’s slides, by their offsetHeight (borders and padding count, margins don’t). The viewport, its own height left to the content, follows the container.
It measures:
- when the carousel is built,
- on
select— under a finger, as soon as another snap is nearest, - on
resize— a new width of the viewport or a slide, - on
slideFocus.
destroy() removes the height. A rebuild — api.reInit(), a breakpoint, slides added or removed — removes it too, then measures again.
Only the selected snap
Only the selected snap’s slides count. A slide beside it that is taller is cut off at the bottom — Spill writes overflow-y: hidden on a row’s viewport. Show one snap at a time, or let the slides in view share a snap with slidesToScroll: "auto".
AutoHeight is for a row. A column (axis: "y") has the height you give its viewport.
A slide that grows later
It measures again on a new width, not on a new height. A slide that grows by itself — an image that loads, text that opens — isn’t followed until the next select.
- Give images their
widthandheightattributes: the slide has its height before they load. - Or call
api.reInit()after the change. That rebuilds the carousel and its plugins — an Autoplay on it starts over.
Built while hidden
In a closed tab or a dialog, every slide measures 0, and AutoHeight writes nothing. Once the carousel shows, Spill measures again and AutoHeight fits it.
Options
| Option | Default | |
|---|---|---|
active |
true |
false: no height written |
breakpoints |
{} |
Options by media query, merged in while it matches |
AutoHeight.globalOptions holds options every AutoHeight starts from, under its own — as Spill.globalOptions does for the carousel.
AutoHeight on a phone only — on a wide screen, where your CSS shows three slides side by side, the row keeps its own height:
Spill(viewport, { align: "start" }, [
AutoHeight({ breakpoints: { "(min-width: 1024px)": { active: false } } }),
]);
A breakpoint that starts or stops matching rebuilds the carousel: the height is removed, then written again if AutoHeight is active. More on screens: Breakpoints.