A card groups the content and actions about one subject on one surface: a title, media, supporting text and buttons. Use cards when each item needs its own title, media and controls, and should read as a separate object. M3 has three: elevated, filled and outlined.
Usage
The card places its sections in M3 order: media, header, content, then actions.
import { createCard } from 'material';
const card = createCard({
header: { title: 'The Kiss', subtitle: 'Gustav Klimt, 1908' },
content: { text: 'A couple embracing, wrapped in gold leaf and ornament.' },
buttons: [{ text: 'Details', variant: 'text' }],
});
document.body.append(card.element);
<m-card headline="The Kiss" subhead="Gustav Klimt, 1908">
A couple embracing, wrapped in gold leaf and ornament.
<m-button slot="actions" variant="text">Details</m-button>
</m-card>
import { Card, Button } from 'material/react';
export function Example() {
return (
<Card headline="The Kiss" subhead="Gustav Klimt, 1908">
A couple embracing, wrapped in gold leaf and ornament.
<Button slot="actions" variant="text">Details</Button>
</Card>
);
}
<script setup lang="ts">
import { MCard, MButton } from 'material/vue';
</script>
<template>
<MCard headline="The Kiss" subhead="Gustav Klimt, 1908">
A couple embracing, wrapped in gold leaf and ornament.
<template #actions>
<MButton variant="text">Details</MButton>
</template>
</MCard>
</template>
<script lang="ts">
import { Card, Button } from 'material/svelte';
</script>
<Card headline="The Kiss" subhead="Gustav Klimt, 1908">
A couple embracing, wrapped in gold leaf and ornament.
{#snippet actions()}
<Button variant="text">Details</Button>
{/snippet}
</Card>
import { Card, Button } from 'material/solid';
export function Example() {
return (
<Card headline="The Kiss" subhead="Gustav Klimt, 1908">
A couple embracing, wrapped in gold leaf and ornament.
<Button slot="actions" variant="text">Details</Button>
</Card>
);
}
Examples
Media
media puts an image at the top, clipped to the card's shape. Its alt is required when the
image carries meaning, and '' when it does not. In the factory, aspectRatio ('16:9',
'4:3', '1:1') sets its crop, and contain: true fits it whole.
import { createCard } from 'material';
const card = createCard({
variant: 'filled',
media: {
src: '/assets/playground/landscape-1.svg',
alt: 'A mountain landscape',
},
header: { title: 'Highlands', subtitle: 'Three days on foot' },
});
document.body.append(card.element);
<m-card variant="filled" headline="Highlands" subhead="Three days on foot">
<img slot="media" src="/assets/playground/landscape-1.svg" alt="A mountain landscape">
</m-card>
import { Card } from 'material/react';
export function Example() {
return (
<Card variant="filled" headline="Highlands" subhead="Three days on foot">
<img slot="media" src="/assets/playground/landscape-1.svg" alt="A mountain landscape" />
</Card>
);
}
<script setup lang="ts">
import { MCard } from 'material/vue';
</script>
<template>
<MCard variant="filled" headline="Highlands" subhead="Three days on foot">
<template #media>
<img src="/assets/playground/landscape-1.svg" alt="A mountain landscape" />
</template>
</MCard>
</template>
<script lang="ts">
import { Card } from 'material/svelte';
</script>
<Card variant="filled" headline="Highlands" subhead="Three days on foot">
{#snippet media()}
<img src="/assets/playground/landscape-1.svg" alt="A mountain landscape" />
{/snippet}
</Card>
import { Card } from 'material/solid';
export function Example() {
return (
<Card variant="filled" headline="Highlands" subhead="Three days on foot">
<img slot="media" src="/assets/playground/landscape-1.svg" alt="A mountain landscape" />
</Card>
);
}
Clickable
clickable makes the whole card a button: it is in the tab order, Enter and Space click
it, and a press shows the ripple. By default, keep other controls out of a clickable card.
Opt in with promoteChildActions: true (promote-child-actions on <m-card>) to let a clickable
card containing child actions become an article container. The card leaves the tab order;
its child controls remain keyboard reachable, activating a child does not activate the card,
and the development warning stays off. The option defaults to false.
import { createCard } from 'material';
const card = createCard({
variant: 'outlined',
clickable: true,
header: { title: 'Unsaved changes' },
content: { text: 'Your edits have not been published yet.' },
});
document.body.append(card.element);
<m-card variant="outlined" clickable headline="Unsaved changes">Your edits have not been published yet.</m-card>
import { Card } from 'material/react';
export function Example() {
return (
<Card variant="outlined" clickable headline="Unsaved changes">Your edits have not been published yet.</Card>
);
}
<script setup lang="ts">
import { MCard } from 'material/vue';
</script>
<template>
<MCard variant="outlined" clickable headline="Unsaved changes">Your edits have not been published yet.</MCard>
</template>
<script lang="ts">
import { Card } from 'material/svelte';
</script>
<Card variant="outlined" clickable headline="Unsaved changes">Your edits have not been published yet.</Card>
import { Card } from 'material/solid';
export function Example() {
return (
<Card variant="outlined" clickable headline="Unsaved changes">Your edits have not been published yet.</Card>
);
}
import { createCard } from 'material';
const childActionCard = createCard({
clickable: true, promoteChildActions: true, buttons: [{ text: 'Read more' }],
});
document.body.append(childActionCard.element);
A card has no width of its own: it fills the column, grid cell or flex track it is in, as the
M3 specs describe. The --small, --medium and --large classes fix it at 344, 480 and
624dp. The factory's content helpers, createCardHeader(), createCardContent(),
createCardMedia() and createCardActions(), build sections for setHeader(),
addContent(), addMedia() and setActions(). The web component takes its sections in
slots: media, avatar, headline, subhead, header-action, the content, and actions.
Composing Loading and Expandable Cards
createCard is a pure layout and surface container — it does not ship built-in loading spinners or accordion state. Compose those states with createProgress and createIconButton:
Loading Card (createCard + createProgress)
Place an indeterminate createProgress inside a createCardContent section and mark the card root with aria-busy="true" until the content settles:
import {
createCard,
createCardContent,
createCardHeader,
createProgress,
} from 'material';
const loadingCard = createCard({ variant: 'outlined' });
loadingCard.element.setAttribute('aria-busy', 'true');
loadingCard.setHeader(createCardHeader({ title: 'Market summary' }));
const body = createCardContent();
const spinner = createProgress({
variant: 'circular',
indeterminate: true,
ariaLabel: 'Loading market summary',
});
body.append(spinner.element);
loadingCard.addContent(body);
// When data arrives, tear down the progress indicator and clear aria-busy:
function onDataReady(summaryText: string) {
spinner.destroy();
body.textContent = summaryText;
loadingCard.element.removeAttribute('aria-busy');
}
Expandable Card (createCard + createIconButton)
Pass a standard createIconButton (without toggle: true, following the WAI-ARIA APG Disclosure pattern with a constant accessible name so aria-expanded conveys the collapsed/expanded state and aria-pressed is not set) as the action of createCardHeader, and wire aria-expanded, aria-controls, setIcon(), and the collapsible section's hidden property in its click listener:
import {
createCard,
createCardContent,
createCardHeader,
createIconButton,
} from 'material';
const expandMore =
'<svg viewBox="0 0 24 24" width="24" height="24"><path fill="currentColor" d="M7.41 8.59 12 13.17l4.59-4.58L18 10l-6 6-6-6z"/></svg>';
const expandLess =
'<svg viewBox="0 0 24 24" width="24" height="24"><path fill="currentColor" d="M7.41 15.41 12 10.83l4.59 4.58L18 14l-6-6-6 6z"/></svg>';
const details = createCardContent({
text: 'Full itinerary, check-in instructions, and local transit passes.',
});
details.id = 'trip-card-details';
details.hidden = true;
const expandButton = createIconButton({
icon: expandMore,
ariaLabel: 'Trip details',
});
expandButton.element.setAttribute('aria-expanded', 'false');
expandButton.element.setAttribute('aria-controls', details.id);
expandButton.on('click', () => {
const expanded = details.hidden;
details.hidden = !expanded;
expandButton.element.setAttribute('aria-expanded', String(expanded));
expandButton.setIcon(expanded ? expandLess : expandMore);
});
const expandableCard = createCard({
variant: 'elevated',
content: { text: '3 nights in Lisbon · Flight + Hotel' },
});
expandableCard.setHeader(
createCardHeader({
title: 'Trip summary',
subtitle: 'May 15–17',
action: expandButton.element,
}),
);
expandableCard.addContent(details);
API
Options
variantCard variant
'elevated' | 'filled' | 'outlined''elevated'clickableA button: ripple, Enter and Space, and the click event
booleanfalsepromoteChildActionsOpt in so a clickable card with child actions becomes an article container (promote-child-actions on <m-card>)
booleanfalseinteractiveHover and press states only: no role, no tab stop, no key handling
booleanfalsefullWidthwidth: 100%
booleanfalsedraggableHTML drag, with elevation while dragged and dragstart / dragend
booleanfalseheader{ title, subtitle, avatar, action, class }
CardHeaderConfigundefinedcontent{ text, html, children, padding, class }; html wins over text, padding is on by default
CardContentConfigundefinedmedia{ src, alt, element, aspectRatio, contain, position, class }; position is 'top' or 'bottom'
CardMediaConfigundefinedactions{ actions, align, fullBleed, vertical, class }; align is 'start', 'center', 'end' or 'space-between'
CardActionsConfigundefinedbuttonsAn actions row of buttons, added a microtask after creation
ButtonConfig[]undefinedaria{ role, label, labelledby, describedby }, each written as an aria- attribute; role defaults to article, or button when clickable
CardAriaAttributesundefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'headerConfig, contentConfig, mediaConfig and actionsConfig are the long forms of
header, content, media and actions.
Methods
setHeader(element)Replaces the header; after the media when there is some, otherwise first
CardComponentaddContent(element)Appends a content section; one without the mtrl-card-content class is ignored
CardComponentaddMedia(element, position?)Inserts media at the top (default) or the bottom
CardComponentsetActions(element)Replaces the actions row, last
CardComponentmakeDraggable(onDragStart?)Makes the card draggable, keeping aria-grabbed in step
CardComponentfocus()Focuses the card
CardComponentdestroy()Removes the card and its listeners
void| Helper | Returns | Description |
|---|---|---|
createCardHeader(config) |
HTMLElement |
A header from CardHeaderConfig |
createCardContent(config) |
HTMLElement |
A content section from CardContentConfig |
createCardMedia(config) |
HTMLElement |
Media from CardMediaConfig |
createCardActions(config) |
HTMLElement |
An actions row from CardActionsConfig |
Events
clickA clickable card was clicked, or activated with Enter or Space
dragstart / dragendA draggable card's drag began or ended
{ event }The web component's activation is the native click.
Styling
The elevation follows the variant, and rises while the card is hovered or dragged.
.mtrl-card { }
.mtrl-card--elevated, .mtrl-card--filled, .mtrl-card--outlined { }
.mtrl-card--interactive, .mtrl-card--focused, .mtrl-card--dragging, .mtrl-card--full-width { }
.mtrl-card--small, .mtrl-card--medium, .mtrl-card--large { }
.mtrl-card__header, .mtrl-card__header-text, .mtrl-card__header-title, .mtrl-card__header-subtitle { }
.mtrl-card__header-avatar, .mtrl-card__header-action { }
.mtrl-card__media, .mtrl-card__content, .mtrl-card__actions { }
Measurements
| Attribute | Value | Token |
|---|---|---|
| Container corner | 12dp | ContainerShape (CornerMedium), all three variants |
| Elevated container | surface-container-low |
ElevatedCardTokens.ContainerColor |
| Filled container | surface-container-highest |
FilledCardTokens.ContainerColor |
| Outlined container | surface |
OutlinedCardTokens.ContainerColor |
| Outline | 1dp outline-variant; on-surface focused |
OutlinedCardTokens.OutlineColor / OutlineWidth / FocusOutlineColor |
| Elevated elevation | Level 1, level 2 hovered | ContainerElevation / HoverContainerElevation |
| Filled and outlined elevation | Level 0, level 1 hovered | HoverContainerElevation |
| Dragged elevation | Level 4 elevated, level 3 otherwise | DraggedContainerElevation |