Skip to content

Card

Groups the content and actions of one subject on a surface of its own.

Order PO-2416
Ferrolva Acciai, delivery to Brescia

12 pallets of steel bar are on the road. Luca expects them at the Brescia dock on Thursday.

Installation

pnpm dlx shadcn@latest add @silkware/card

To copy the component by hand, install its dependency:

pnpm add cn

Then copy the source into components/ui/card.tsx:

components/ui/card.tsx
import * as React from "react"import { cn } from "cn"
function Card({  className,  size = "default",  ...props}: React.ComponentProps<"div"> & { size?: "default" | "sm" }) {  return (    <div      data-slot="card"      data-size={size}      className={cn(        "group/card flex flex-col gap-(--card-spacing) overflow-hidden rounded-xl bg-card py-(--card-spacing) text-sm text-card-foreground shadow-xs ring-1 ring-foreground/10 [--card-spacing:--spacing(6)] has-[>img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(4)] *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",        className      )}      {...props}    />  )}
function CardHeader({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="card-header"      className={cn(        "group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-(--card-spacing) has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-(--card-spacing)",        className      )}      {...props}    />  )}
function CardTitle({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="card-title"      className={cn(        "font-heading text-base leading-normal font-medium group-data-[size=sm]/card:text-sm",        className      )}      {...props}    />  )}
function CardDescription({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="card-description"      className={cn("text-sm text-muted-foreground", className)}      {...props}    />  )}
function CardAction({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="card-action"      className={cn(        "col-start-2 row-span-2 row-start-1 self-start justify-self-end",        className      )}      {...props}    />  )}
function CardContent({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="card-content"      className={cn("flex flex-col gap-3 px-(--card-spacing)", className)}      {...props}    />  )}
function CardFooter({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="card-footer"      className={cn(        "flex items-center rounded-b-xl px-(--card-spacing) [.border-t]:pt-(--card-spacing)",        className      )}      {...props}    />  )}
export {  Card,  CardHeader,  CardFooter,  CardTitle,  CardAction,  CardDescription,  CardContent,}

Usage

import {  Card,  CardAction,  CardContent,  CardDescription,  CardFooter,  CardHeader,  CardTitle,} from "@/components/ui/card"
<Card>  <CardHeader>    <CardTitle>Order PO-2416</CardTitle>    <CardDescription>Ferrolva Acciai</CardDescription>  </CardHeader>  <CardContent>12 pallets are on the road.</CardContent>  <CardFooter>    <Button>Confirm delivery</Button>  </CardFooter></Card>

Every part is a plain div, so a card has no role and no keyboard behaviour. CardTitle is not a heading element: when the card sits in a page outline, give it role="heading" with an aria-level, or render the title as a heading yourself. A card that opens something needs a real link or button inside it.

Examples

Default

A header with title, description and an optional action, then content and a footer. Place CardAction inside CardHeader: it moves to the top right.

Order PO-2416
Ferrolva Acciai, delivery to Brescia

12 pallets of steel bar are on the road. Luca expects them at the Brescia dock on Thursday.

Size

size="sm" shrinks the spacing from 24px to 16px and the title to the small text size. Use it for dense grids and for summaries.

Default
Open purchase orders

14

Small
Open purchase orders

14

Sections

Add the border-b class to CardHeader or border-t to CardFooter to separate a part from the content. The card adjusts its padding to keep the spacing even.

Quality check QC-0318
Brescia, line 2

Marco found 3 of 240 housings outside tolerance and held the batch.

Held at 09:40

With an image

An img as the first child runs to the card's edges and drops the top padding. Always give it alt text.

Silkware mark
Padova site
Assembly and packaging

Two lines, 38 people, one shift pattern.

Accessibility

A card is a visual container. The accessible structure comes from what you put in it.

KeyAction
TabMoves focus to the next control inside the card. The card itself is not focusable.

React Aria provides nothing here: the card is not built on a primitive.

You provide:

  • A heading level for the title when the card is a section of the page.
  • Real controls for actions: a Button for an action, a link for navigation.
  • alt text on images, or empty alt when the image is decoration.
  • A name on a card that is a landmark, through role="region" and aria-labelledby, only when people need to jump to it.

Guidelines

When to use

  • To present one subject with its facts and actions: an order, a supplier, a site.
  • To make a grid of comparable subjects, such as one card per site.
  • To separate a self-contained panel, such as a settings group, from the page.

When not to use

  • To give a page section a frame. Use a plain section with a heading and space; a border adds noise. See Information hierarchy.
  • For rows of titled entries. Use an Item group.
  • For records with the same fields. Use a Table.
  • To show a message with a status. Use an Alert.
  • Inside another card. Flatten the content, or split it into two cards.

Content

  • One subject per card. The title names it in a few words.
  • The description adds one fact, not a sentence of explanation.
  • Put one main action in the footer, last in the row. A card with five buttons is a page.
  • Keep cards in a grid the same height by keeping their content the same shape.

One subject

Supplier Plastrena
On time for 11 of the last 12 deliveries

Next delivery: Tuesday, 14 pallets.

Do

Give a card one subject and let its content support it.

Supplier Plastrena
On time for 11 of the last 12 deliveries

Next delivery: Tuesday, 14 pallets.

Don’t

Don’t nest a card in a card. The second border adds depth without meaning.

API reference

Card

Renders a div with the card surface: a 12px radius, a hairline ring and no shadow. It accepts every div prop and the props below.

PropTypeDefaultDescription
size?"default" | "sm""default"The spacing inside the card. Small uses 16px instead of 24px.
className?stringNoneClasses merged after the base classes.

CardHeader

Renders a grid for CardTitle, CardDescription and CardAction. It switches to two columns when a CardAction is present. It accepts every div prop.

CardTitle

Renders a div with the heading font. It accepts every div prop.

CardDescription

Renders a div in muted text. It accepts every div prop.

CardAction

Renders a div placed at the top right of the header. It accepts every div prop.

CardContent

Renders a div with the side padding of the card and a 12px gap between children. It accepts every div prop.

CardFooter

Renders a flex div aligned to the centre. It accepts every div prop.