Cards display content and actions about a single subject. Use a card to contain related elements, such as:
To show a set of items of the same kind, such as messages or contacts, use a list instead.
import { Card } from '@material-web/react'Basic card
Write the content of a card as its children, and choose its look with variant. A card is a container only, and adds no behavior.
import { Button, Card } from '@material-web/react'
const cover = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 140"><rect width="320" height="140" fill="#E8DEF8"/><path d="M0 120 90 60 150 100 200 75 320 125V140H0Z" fill="#6750A4" opacity=".6"/><circle cx="250" cy="40" r="20" fill="#6750A4"/></svg>',
)}`
export default function Basic() {
return (
<Card variant="outlined" className="w-80">
<img src={cover} alt="" className="h-35 w-full object-cover" />
<div className="p-space-200">
<div className="text-title-md">Eye Rhymes</div>
<div className="text-body-md text-on-surface-variant">Glass & Grace</div>
</div>
<div className="flex justify-end gap-space-100 p-space-200 pt-0">
<Button variant="text">Share</Button>
<Button>Play</Button>
</div>
</Card>
)
}The Card comes in three variants: elevated (the default), filled and outlined. The three differ in style only, so choose the one that suits your design.
import { Card } from '@material-web/react'
export default function Variants() {
return (
<>
<Card variant="elevated" className="w-52 p-space-200">
<div className="text-title-md">Elevated</div>
<div className="text-on-surface-variant">A drop shadow lifts it off the background.</div>
</Card>
<Card variant="filled" className="w-52 p-space-200">
<div className="text-title-md">Filled</div>
<div className="text-on-surface-variant">
A fill sets it subtly apart from the background.
</div>
</Card>
<Card variant="outlined" className="w-52 p-space-200">
<div className="text-title-md">Outlined</div>
<div className="text-on-surface-variant">A visual boundary runs around the container.</div>
</Card>
</>
)
}Elevated card
An elevated card has a shadow, which sets it further apart from the background than a filled card, and less than an outlined one.
import { Button, Card } from '@material-web/react'
export default function Elevated() {
return (
<Card variant="elevated" className="w-80">
<div className="p-space-200">
<div className="text-title-md">Eye Rhymes</div>
<div className="text-on-surface-variant">Glass & Grace</div>
</div>
<div className="flex justify-end p-space-200 pt-0">
<Button variant="tonal">Play</Button>
</div>
</Card>
)
}Filled card
A filled card has a fill that sets it subtly apart from the background, with less emphasis than the other two.
import { Button, Card } from '@material-web/react'
export default function Filled() {
return (
<Card variant="filled" className="w-80">
<div className="p-space-200">
<div className="text-title-md">Warped Speed</div>
<div className="text-on-surface-variant">Trapezoid</div>
</div>
<div className="flex justify-end p-space-200 pt-0">
<Button>Play</Button>
</div>
</Card>
)
}Outlined card
An outlined card has an outline, which can give it more emphasis than the other two.
import { Button, Card } from '@material-web/react'
export default function Outlined() {
return (
<Card variant="outlined" className="w-80">
<div className="p-space-200">
<div className="text-title-md">Personal Voice</div>
<div className="text-on-surface-variant">Decorations</div>
</div>
<div className="flex justify-end p-space-200 pt-0">
<Button>Play</Button>
</div>
</Card>
)
}Content
A card shows its children in a column, in the order written: an image that reaches the card's edges, padded text, a row of buttons. It clips its content to its rounded corners.
- Padding: A card has no padding. Add your own to its children, such as
p-space-200for 16dp from its sides. - Size: A card is as tall as its content. Like any
<div>, it fills the width of a block container and fits its content in a flex row. Give it a width with a class, such asw-80. - Direction: Add
flex-rowto lay the children out in a row. - Text: Text in a card has the
text-body-mdstyle and the on-surface color. Give a headline its own style, such astext-title-md. - Children: The children of a card cannot be a function.
Media
Write an image as a child of the card, with a height and w-full object-cover. It reaches the card's edges, and the card clips it to its rounded corners.
import { Card } from '@material-web/react'
const cover = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 180"><rect width="320" height="180" fill="#FFD8E4"/><path d="M0 150 90 80 150 130 200 100 320 160V180H0Z" fill="#7D5260" opacity=".6"/><circle cx="250" cy="50" r="22" fill="#7D5260"/></svg>',
)}`
export default function Media() {
return (
<Card variant="filled" className="w-80">
<img src={cover} alt="" className="h-45 w-full object-cover" />
<div className="p-space-200">
<div className="text-title-md">Warped Speed</div>
<div className="text-on-surface-variant">Recorded over one winter in a lighthouse.</div>
</div>
</Card>
)
}UI controls
A card can hold controls, such as buttons and icon buttons. Each one is a tab stop of its own. This card lays its children out in a row with flex-row, with the playback controls beside the cover.
import { Card, Icon, IconButton } from '@material-web/react'
const cover = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160"><rect width="160" height="160" fill="#E8DEF8"/><circle cx="80" cy="80" r="44" fill="#6750A4" opacity=".6"/><circle cx="80" cy="80" r="12" fill="#E8DEF8"/></svg>',
)}`
export default function Controls() {
return (
<Card variant="filled" className="w-88 flex-row">
<div className="flex flex-1 flex-col justify-between">
<div className="p-space-200">
<div className="text-title-md">Eye Rhymes</div>
<div className="text-on-surface-variant">Glass & Grace</div>
</div>
<div className="flex items-center gap-space-50 p-space-100">
<IconButton aria-label="Previous">
<Icon name="skip_previous" />
</IconButton>
<IconButton variant="filled" aria-label="Play">
<Icon name="play_arrow" />
</IconButton>
<IconButton aria-label="Next">
<Icon name="skip_next" />
</IconButton>
</div>
</div>
<img src={cover} alt="" className="w-35 object-cover" />
</Card>
)
}Primary action
A card itself cannot be pressed. To give a card a primary action, such as a link to a detail page, wrap the content that the action covers in a link or a button of your own inside the card:
- Position it with
relativeand add aRippleas its direct child, for the hover and focus layer and the press ripple. - Add the
focus-ring-insetclass for the focus indicator. It is drawn inside the element, where the card does not clip it.
import { Button, Card, Ripple } from '@material-web/react'
const cover = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 140"><rect width="320" height="140" fill="#E8DEF8"/><path d="M0 120 90 60 150 100 200 75 320 125V140H0Z" fill="#6750A4" opacity=".6"/><circle cx="250" cy="40" r="20" fill="#6750A4"/></svg>',
)}`
export default function PrimaryAction() {
return (
<Card variant="outlined" className="w-80">
<a
href="#primary-action"
className="relative focus-ring-inset flex flex-col text-on-surface no-underline outline-none"
>
<Ripple />
<img src={cover} alt="" className="h-35 w-full object-cover" />
<div className="p-space-200">
<div className="text-title-md">Eye Rhymes</div>
<div className="text-on-surface-variant">Glass & Grace</div>
</div>
</a>
<div className="flex justify-end p-space-200 pt-0">
<Button variant="text">Share</Button>
</div>
</Card>
)
}Write the card's other buttons outside the primary action, so that each one stays a control and a tab stop of its own.
Collections
Show cards together in a grid, a vertical list or a carousel, at most 8dp apart (gap-space-100).
import { Card } from '@material-web/react'
const cover = (fill: string, ink: string) =>
`data:image/svg+xml,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 120"><rect width="240" height="120" fill="${fill}"/><circle cx="180" cy="40" r="20" fill="${ink}"/></svg>`,
)}`
const albums = [
{ title: 'Eye Rhymes', artist: 'Glass & Grace', cover: cover('#E8DEF8', '#6750A4') },
{ title: 'Warped Speed', artist: 'Trapezoid', cover: cover('#FFD8E4', '#7D5260') },
{ title: 'Personal Voice', artist: 'Decorations', cover: cover('#FFD8E4', '#7D5260') },
{ title: 'NBR', artist: '21st Century Strangers', cover: cover('#E8DEF8', '#6750A4') },
]
export default function Collection() {
return (
<div className="grid w-full max-w-120 grid-cols-2 gap-space-100">
{albums.map((album) => (
<Card key={album.title} variant="filled">
<img src={album.cover} alt="" className="h-30 w-full object-cover" />
<div className="p-space-200">
<div className="text-title-md">{album.title}</div>
<div className="text-on-surface-variant">{album.artist}</div>
</div>
</Card>
))}
</div>
)
}Customization
Pass classes in className to restyle one card. A class of yours replaces the card's own class for the same property. See Styling for the classes and the theme.
import { Button, Card } from '@material-web/react'
export default function Custom() {
return (
<Card
variant="filled"
className="w-80 rounded-shape-xl bg-tertiary-container text-on-tertiary-container"
>
<div className="p-space-300">
<div className="text-title-lg">Glass & Grace</div>
<div>Live at the lighthouse, Friday at 8 PM.</div>
</div>
<div className="flex justify-end p-space-300 pt-0">
<Button className="bg-tertiary text-on-tertiary">Get tickets</Button>
</div>
</Card>
)
}- Color: Pass a container color role and its on-color, such as
bg-tertiary-container text-on-tertiary-container. - Shape: A card has 12dp corners. Pass a class of the corner scale for another radius, such as
rounded-shape-xl. - Elevation: Pass an elevation level for another shadow, such as
shadow-elevation-3. - States: A card reports no state, so
classNameandstyletake a value and cannot be a function. - Every card: To restyle all the cards at once, change the theme.
Use cases
Material lists what people should be able to do with assistive technology:
- Navigate to a card and the elements within a card
- Get appropriate feedback based on input type documented under Interaction & style
Interaction and style
- States: A card has no states of its own: hovering or pressing it changes nothing, as Material advises for a card that holds buttons and links. Each button and link in it shows its own states. For a primary action, the
Ripplein your link or button shows a state layer on hover and a ripple on a press, andfocus-ring-insetshows a focus ring for keyboard focus. - Color contrast: On-surface text has 4.5:1 contrast or more with the container of every variant, in light and dark schemes at every contrast level. Keep that contrast when you change a card's colors.
- Forced colors: Every card keeps an edge in forced colors.
- Dragging: A card cannot be dragged by itself. If you make cards draggable or swipeable, such as to reorder or dismiss them, Material requires a single-pointer alternative: offer the same actions in a menu or a bottom sheet, opened from a button in the card.
Focus
- A card cannot be pressed and is not a tab stop.
- Tab moves the focus to each button and link in it, in the order written, so that all of a card's controls come before the next card's, as Material advises.
- A primary action is a tab stop of its own. Write it before the card's other buttons, so that the focus reaches it first.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus to the next button or link, through each card in turn |
| Space or Enter | Press the focused button |
| Enter | Follow the focused link |
Labeling elements
- A card is a
<div>with no role. Screen readers read its content in the order written, so write it in reading order, such as a headline, an image, the supporting text and then the buttons. - Material advises hiding a purely decorative image from screen readers: give it an empty
alt, as in Media. For a directly actionable card, it advises the link or the button role by its use: wrap a primary action in a link to go to a page, and in a button to take an action.
variant'elevated' | 'filled' | 'outlined'Default'elevated'Three variants: elevated, filled, outlined
elevated: Elevated cards have a drop shadow, providing more separation from the background than filled cards, but less than outlined cardsfilled: Filled cards provide subtle separation from the background. This has less emphasis than elevated or outlined cards.outlined: Outlined cards have a visual boundary around the container. This can provide greater emphasis than the other variants.
Each provides the same legibility and functionality, so the variant you use depends on style alone.
Props of Card (1)
refRef<HTMLDivElement>Allows getting a ref to the component instance. Once the component unmounts, React will set
ref.currenttonull(or call the ref withnullif you passed a callback ref).
Card also takes the 278 attributes and events of the element it renders.