A carousel shows a collection of items that scroll on and off the screen, changing size as they move and settling into place: artwork, photos, featured entries, to browse rather than read in full. M3 has five layouts: multi-browse, uncontained, hero, center-aligned hero and full-screen.
Usage
The carousel fills the height it is given, and has none of its own: set one on it or on its
container. change fires with the value of the item that settles into the focal position: its index, as getValue() returns it.
import { createCarousel } from 'material';
const carousel = createCarousel({
ariaLabel: 'Places to explore',
slides: [
{
image: '/assets/playground/landscape-1.svg',
alt: 'Hills under a pale sky',
title: 'Highlands',
},
{
image: '/assets/playground/landscape-2.svg',
alt: 'Cliffs over the sea',
title: 'Coast',
},
{
image: '/assets/playground/landscape-3.svg',
alt: 'Dunes at noon',
title: 'Desert',
},
],
});
carousel.element.style.height = '320px';
carousel.on('change', ({ value }) => showSlide(value));
document.body.append(carousel.element);
<m-carousel aria-label="Places to explore" style="height: 320px">
<m-carousel-item src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky">Highlands</m-carousel-item>
<m-carousel-item src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea">Coast</m-carousel-item>
<m-carousel-item src="/assets/playground/landscape-3.svg" alt="Dunes at noon">Desert</m-carousel-item>
</m-carousel>
<script type="module">
const carousel = document.querySelector('m-carousel');
carousel.addEventListener('change', (event) => showSlide(event.detail.value));
</script>
import { Carousel, CarouselItem } from 'material/react';
import { showSlide } from './app';
export function Example() {
return (
<Carousel onChange={(event) => showSlide(event.detail.value)} ariaLabel="Places to explore" style={{ height: '320px' }}>
<CarouselItem src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky">Highlands</CarouselItem>
<CarouselItem src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea">Coast</CarouselItem>
<CarouselItem src="/assets/playground/landscape-3.svg" alt="Dunes at noon">Desert</CarouselItem>
</Carousel>
);
}
<script setup lang="ts">
import { MCarousel, MCarouselItem } from 'material/vue';
import { showSlide } from './app';
</script>
<template>
<MCarousel @change="showSlide($event.detail.value)" aria-label="Places to explore" style="height: 320px">
<MCarouselItem src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky">Highlands</MCarouselItem>
<MCarouselItem src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea">Coast</MCarouselItem>
<MCarouselItem src="/assets/playground/landscape-3.svg" alt="Dunes at noon">Desert</MCarouselItem>
</MCarousel>
</template>
<script lang="ts">
import { Carousel, CarouselItem } from 'material/svelte';
import { showSlide } from './app';
</script>
<Carousel onchange={(event) => showSlide(event.detail.value)} ariaLabel="Places to explore" style="height: 320px">
<CarouselItem src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky">Highlands</CarouselItem>
<CarouselItem src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea">Coast</CarouselItem>
<CarouselItem src="/assets/playground/landscape-3.svg" alt="Dunes at noon">Desert</CarouselItem>
</Carousel>
import { Carousel, CarouselItem } from 'material/solid';
import { showSlide } from './app';
export function Example() {
return (
<Carousel onChange={(event) => showSlide(event.detail.value)} ariaLabel="Places to explore" style="height: 320px">
<CarouselItem src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky">Highlands</CarouselItem>
<CarouselItem src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea">Coast</CarouselItem>
<CarouselItem src="/assets/playground/landscape-3.svg" alt="Dunes at noon">Desert</CarouselItem>
</Carousel>
);
}
Examples
Hero, with a call to action
variant picks the layout. hero shows one large item and a glimpse of the next;
hero-center centers it between two small ones. itemWidth is a large item's preferred
width, and in the hero layouts its maximum. A slide's buttonText and buttonUrl add a link.
import { createCarousel } from 'material';
const carousel = createCarousel({
variant: 'hero',
itemWidth: 480,
ariaLabel: 'Collections',
slides: [
{
image: '/assets/playground/landscape-4.svg',
alt: 'A forest path',
title: 'Spring collection',
description: 'Out now',
buttonText: 'Browse',
buttonUrl: '/collections/spring',
},
{
image: '/assets/playground/landscape-5.svg',
alt: 'A lake at dawn',
title: 'Lakeside',
},
],
});
carousel.element.style.height = '320px';
document.body.append(carousel.element);
<m-carousel variant="hero" item-width="480" aria-label="Collections" style="height: 320px">
<m-carousel-item src="/assets/playground/landscape-4.svg" alt="A forest path" description="Out now" button-text="Browse" button-url="/collections/spring">Spring collection</m-carousel-item>
<m-carousel-item src="/assets/playground/landscape-5.svg" alt="A lake at dawn">Lakeside</m-carousel-item>
</m-carousel>
import { Carousel, CarouselItem } from 'material/react';
export function Example() {
return (
<Carousel variant="hero" itemWidth={480} ariaLabel="Collections" style={{ height: '320px' }}>
<CarouselItem src="/assets/playground/landscape-4.svg" alt="A forest path" description="Out now" buttonText="Browse" buttonUrl="/collections/spring">Spring collection</CarouselItem>
<CarouselItem src="/assets/playground/landscape-5.svg" alt="A lake at dawn">Lakeside</CarouselItem>
</Carousel>
);
}
<script setup lang="ts">
import { MCarousel, MCarouselItem } from 'material/vue';
</script>
<template>
<MCarousel variant="hero" :item-width="480" aria-label="Collections" style="height: 320px">
<MCarouselItem src="/assets/playground/landscape-4.svg" alt="A forest path" description="Out now" button-text="Browse" button-url="/collections/spring">Spring collection</MCarouselItem>
<MCarouselItem src="/assets/playground/landscape-5.svg" alt="A lake at dawn">Lakeside</MCarouselItem>
</MCarousel>
</template>
<script lang="ts">
import { Carousel, CarouselItem } from 'material/svelte';
</script>
<Carousel variant="hero" itemWidth={480} ariaLabel="Collections" style="height: 320px">
<CarouselItem src="/assets/playground/landscape-4.svg" alt="A forest path" description="Out now" buttonText="Browse" buttonUrl="/collections/spring">Spring collection</CarouselItem>
<CarouselItem src="/assets/playground/landscape-5.svg" alt="A lake at dawn">Lakeside</CarouselItem>
</Carousel>
import { Carousel, CarouselItem } from 'material/solid';
export function Example() {
return (
<Carousel variant="hero" itemWidth={480} ariaLabel="Collections" style="height: 320px">
<CarouselItem src="/assets/playground/landscape-4.svg" alt="A forest path" description="Out now" buttonText="Browse" buttonUrl="/collections/spring">Spring collection</CarouselItem>
<CarouselItem src="/assets/playground/landscape-5.svg" alt="A lake at dawn">Lakeside</CarouselItem>
</Carousel>
);
}
Uncontained
Items keep one size and run off the trailing edge, and the list scrolls freely, without settling on an item.
import { createCarousel } from 'material';
const carousel = createCarousel({
variant: 'uncontained',
itemWidth: 240,
ariaLabel: 'Recent photos',
slides: [
{
image: '/assets/playground/landscape-1.svg',
alt: 'Hills under a pale sky',
},
{
image: '/assets/playground/landscape-2.svg',
alt: 'Cliffs over the sea',
},
{
image: '/assets/playground/landscape-3.svg',
alt: 'Dunes at noon',
},
],
});
carousel.element.style.height = '320px';
document.body.append(carousel.element);
<m-carousel variant="uncontained" item-width="240" aria-label="Recent photos" style="height: 320px">
<m-carousel-item src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky"></m-carousel-item>
<m-carousel-item src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea"></m-carousel-item>
<m-carousel-item src="/assets/playground/landscape-3.svg" alt="Dunes at noon"></m-carousel-item>
</m-carousel>
import { Carousel, CarouselItem } from 'material/react';
export function Example() {
return (
<Carousel variant="uncontained" itemWidth={240} ariaLabel="Recent photos" style={{ height: '320px' }}>
<CarouselItem src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky" />
<CarouselItem src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea" />
<CarouselItem src="/assets/playground/landscape-3.svg" alt="Dunes at noon" />
</Carousel>
);
}
<script setup lang="ts">
import { MCarousel, MCarouselItem } from 'material/vue';
</script>
<template>
<MCarousel variant="uncontained" :item-width="240" aria-label="Recent photos" style="height: 320px">
<MCarouselItem src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky" />
<MCarouselItem src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea" />
<MCarouselItem src="/assets/playground/landscape-3.svg" alt="Dunes at noon" />
</MCarousel>
</template>
<script lang="ts">
import { Carousel, CarouselItem } from 'material/svelte';
</script>
<Carousel variant="uncontained" itemWidth={240} ariaLabel="Recent photos" style="height: 320px">
<CarouselItem src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky" />
<CarouselItem src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea" />
<CarouselItem src="/assets/playground/landscape-3.svg" alt="Dunes at noon" />
</Carousel>
import { Carousel, CarouselItem } from 'material/solid';
export function Example() {
return (
<Carousel variant="uncontained" itemWidth={240} ariaLabel="Recent photos" style="height: 320px">
<CarouselItem src="/assets/playground/landscape-1.svg" alt="Hills under a pale sky" />
<CarouselItem src="/assets/playground/landscape-2.svg" alt="Cliffs over the sea" />
<CarouselItem src="/assets/playground/landscape-3.svg" alt="Dunes at noon" />
</Carousel>
);
}
multi-browse, the default, shows at least one large, one medium and one small item, and
more large ones as the container grows. full-screen shows one edge-to-edge item at a time
and scrolls vertically. The layouts follow Compose's keylines: each item's size and place
come from the scroll offset. In the factory, a slide's content (an element or markup)
replaces the image and text, and next(), prev() and goTo(index) move the carousel.
A slide's image may state the intrinsic size of the file with width and height, in
pixels. The browser reserves a box of that ratio before the image arrives, so the carousel does
not jump as files load. loading chooses when the file is fetched: the slide that opens the
carousel is "eager", every later one "lazy", and a slide can override either. decoding
passes the hint of the same name to the image. All four are optional.
API
Options
variantThe layout
'multi-browse' | 'uncontained' | 'hero' | 'hero-center' | 'full-screen''multi-browse'slidesThe items
CarouselSlide[][]itemWidthA large item's width in pixels; a maximum in the hero layouts
number280gapSpace between items in pixels
number8, 16 for full-screenpaddingSpace between the container's edges and the items
number16, 0 for full-screencornerRadiusThe items' corner radius in pixels
number28snapWhether scrolling settles on an item
booleantrue, false for uncontainedinitialSlideThe item shown first
number0minSmallItemWidth / maxSmallItemWidthA small item's width range
number40 / 56ariaLabelThe carousel's accessible name
string'Carousel'classAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'Slides
imageImage URL
stringaltThe image's alt text; title without it, and '' for a decorative image
stringwidth / heightThe image's intrinsic size in pixels
numberloading'eager' for the first slide, 'lazy' for every other
'eager' | 'lazy'decodingPassed to the <img>
'sync' | 'async' | 'auto'title / descriptionText over the image
stringbuttonText / buttonUrlA call-to-action link
stringcontentThe app's own content, instead of the image and text
HTMLElement | stringMethods
next() / prev()The next or previous item, stopping at the ends
CarouselComponentgoTo(index)Scrolls to an item, smoothly unless reduced motion is on
CarouselComponentgetCurrentSlide()The item in the focal position
numbergetVariant()The layout
CarouselVariantaddSlide(slide, index?) / removeSlide(index)Adds or removes an item
CarouselComponentslides.updateSlide(index, slide)Replaces an item's content in place
SlidesAPIslides.getSlide(index) / slides.getCount() / slides.getElements()Reads the items
CarouselSlide | null / number / HTMLElement[]on(event, handler) / off(event, handler)Events
CarouselComponentaddClass(...classes)Adds classes to the root
CarouselComponentdestroy()Removes the carousel, its observer and its listeners
voidEvents
changeAnother item settled into the focal position; value is its index. A goTo() reports its destination only
{ value }The web component's change carries { value } too.
Styling
The component writes --mtrl-carousel-corner on the root, the corner radius, and
--mtrl-carousel-fade on each item, from 1 when large to 0 when small, which the text
overlay fades with. It also writes each item's transform and clip-path as it scrolls:
don't set them from CSS. The root also carries the layout as a modifier, such as
mtrl-carousel--hero, which the stylesheet does not style.
.mtrl-carousel { }
.mtrl-carousel--vertical, .mtrl-carousel--snap { }
.mtrl-carousel__scroller, .mtrl-carousel__track, .mtrl-carousel__item, .mtrl-carousel__image { }
.mtrl-carousel__content, .mtrl-carousel__title, .mtrl-carousel__description, .mtrl-carousel__button { }
Measurements
From Compose's CarouselDefaults:
| Attribute | Value |
|---|---|
| Space between items | 8dp; 16dp full screen |
| Container padding | 16dp |
| Item corner | 28dp |
| Small item | 40 to 56dp wide |
| Anchor size | 10dp outside the container |