<!-- generated by scripts/gen-docs.mjs from docs/system/card.md. Edit the source and rerun. -->
# Card

## Description
A white box that groups one item's title, details and actions. Its content is whatever you put in it: facts, a list, a picture, a chart.

`import { Card, CardHeader, CardTitle, CardDescription, CardAction, CardContent, CardFooter } from "@/components/cobalt/ui/card"`, source `registry/cobalt/ui/card.tsx`, status `ready`.
Foundation: `shadcn card (base-luma, base)`. Traps checked: `trap/surface-double-edge`, `trap/surface-matches-parent`, `trap/decor-pointer`, `trap/illustration-announced`, `trap/empty-value`, `trap/list-dead-gap`, `trap/link-as-button`, `trap/numbers-shift`, `trap/color-only-status`, `trap/touch-hover-flash`.

The card draws no border (0px) and no shadow (none), its corner is 16px and its spacing 24px, 16px at the small size, on `--card` one step off the page tint (`.design-system/evidence/card/grow-title-390.json`). In a 310px card at 390 beside an action, the title wraps to a second line at 34 characters. `trap/surface-double-edge` and `trap/surface-matches-parent` are answered by the flat surface (Foundation and Anti-slop), `trap/link-as-button` and `trap/list-dead-gap` by the link inside the card rather than on it (Rules), `trap/empty-value` by Empty inside the content (Rules), `trap/illustration-announced` by the host's `img` with its own `alt` (Rules). `trap/decor-pointer` n/a: the card has no decorative layer; `overflow-hidden` clips an image, which is content. `trap/numbers-shift` and `trap/color-only-status` n/a: the card renders the host's content and sets no figures or status of its own; Badge and Tag inside it answer them. `trap/touch-hover-flash` n/a: a card has no hover.

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| Base UI `useRender` for `render` on the card and the title, so a card can be a `section` or an `article` and its title an `h2`; the seven parts with `data-slot`; the header grid that puts `CardAction` in a second column and spans it over the title and description | The flat surface (D11): no border, no shadow, `--card` on the page tint with `--r-2xl` (G-03); `--card-spacing` as one variable for the gap and the padding, 24px by default and 16px at `size="sm"`; `overflow-hidden` with `pt-0` when an `img` is the first child, so a cover runs to the corners; the title at `text-base font-semibold` like every title (D21); `text-sm` for the body; the header declares no container, so a card sized by its content is as wide as its title and description and never narrower; the header's first column is `minmax(0, 1fr)` and the title and description break a word with no spaces (`wrap-anywhere`), so an address or a long name wraps inside the card and never pushes `CardAction` out of it |

## Examples
Research tool content: a fund's title, a copy button, its facts and an action; a cover picture; a list of filings; two funds under one heading; and three figures at the small size.

Real uses, 0 call sites (`rg -n "<Card\b" app docs/system/examples --glob '!docs/system/examples/card/*'`): the showcase chrome renders no Card of its own yet, and the examples folder does not count. 28 cards in 22 other example files wrap another component or a screen block in a card, 25 at the default size and 3 at the small size, with `className` only for width or placement. A few:
- The card around an icon button's example, `docs/system/examples/icon-button/in-card.tsx:44`.
- The card around a tab list, `docs/system/examples/tabs/in-card.tsx:7`.
- The card holding an empty state in its content, `docs/system/examples/empty/in-card.tsx:37`.
- The card in the skeleton's first load, with its title, description and values held by lines, `docs/system/examples/skeleton/in-card.tsx:79-90`.

Uses inside the registry, which other components build on (planned as product uses once the first screen lands on the system):
- The rail card, the small size with its 16px spacing on every side, rendered as a labelled `section` with its title an `h2`, `registry/cobalt/ui/rail-card.tsx:22-27`.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/card/default.tsx` | default | A fund's title, a copy button in the header, its facts, and one action at the foot |
| Not applicable: default.tsx shows the default size, 24px of spacing | size=default | The default spacing, 24px |
| `docs/system/examples/card/size-sm.tsx` | size=sm | 16px of spacing, for a side column or a dense grid |
| `docs/system/examples/card/state-with-image.tsx` | state:with-image | A picture as the first child fills the top corners |
| `docs/system/examples/card/state-as-section.tsx` | state:as-section | Rendered as a labelled section, with its title as an h2 |
| Not applicable: default.tsx shows the header with a copy button in its second column | state:with-action | The action at the top right, spanning the title and description |
| `docs/system/examples/card/in-section.tsx` | composition:Section | Two cards under one section heading |

Example: A fund's title, a copy button in the header, its facts, and one action at the foot (`docs/system/examples/card/default.tsx`)

```tsx
import { Button } from "@/components/cobalt/ui/button";
import {
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from "@/components/cobalt/ui/card";
import { CopyButton } from "@/components/cobalt/ui/copy-button";

const FUND = "Harbour Large Cap Core";

const facts: [string, string][] = [
  ["Net assets", "$4.8B"],
  ["Expense ratio", "0.42%"],
  ["Holdings", "186"],
];

export default function Example() {
  return (
    <Card id="example-card-fund" className="w-full max-w-sm">
      <CardHeader>
        <CardTitle>{FUND}</CardTitle>
        <CardDescription>Harbour Capital, filed Aug 29.</CardDescription>
        <CardAction>
          <CopyButton
            size="icon-sm"
            value="https://example.com/funds/harbour-large-cap-core"
            label={`Copy link to ${FUND}`}
          />
        </CardAction>
      </CardHeader>
      <CardContent>
        <dl className="flex flex-col gap-3">
          {facts.map(([term, detail]) => (
            <div key={term} className="flex items-baseline justify-between gap-4">
              <dt className="min-w-0 text-muted-foreground wrap-anywhere">{term}</dt>
              <dd className="shrink-0 text-foreground tabular-nums whitespace-nowrap">{detail}</dd>
            </div>
          ))}
        </dl>
      </CardContent>
      <CardFooter>
        <Button variant="secondary" className="w-full" render={<a href="#example-card-fund" />}>
          View holdings
        </Button>
      </CardFooter>
    </Card>
  );
}
```

Example: Two cards under one section heading (`docs/system/examples/card/in-section.tsx`)

```tsx
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/cobalt/ui/card";
import { Section } from "@/components/cobalt/ui/section";
import { TextLink } from "@/components/cobalt/ui/text-link";

const funds = [
  { name: "Harbour Large Cap Core", manager: "Harbour Capital", note: "New Form ADV filed Aug 29." },
  { name: "Northgate Credit Opportunities", manager: "Northgate Partners", note: "No new filings this month." },
];

export default function Example() {
  return (
    <Section
      id="watchlist"
      heading="Watchlist"
      action={
        <TextLink href="#watchlist-all" className="text-sm">
          See all
        </TextLink>
      }
      className="w-full"
    >
      <div className="grid gap-4 sm:grid-cols-2">
        {funds.map((fund) => (
          <Card key={fund.name}>
            <CardHeader>
              <CardTitle>{fund.name}</CardTitle>
              <CardDescription>{fund.manager}</CardDescription>
            </CardHeader>
            <CardContent>{fund.note}</CardContent>
          </Card>
        ))}
      </div>
    </Section>
  );
}
```

## Variants

### size
- `default`: 24px of spacing and gap, for a card on a page. Used at 0 call sites; 25 cards in the example files take it, such as `docs/system/examples/card/default.tsx`.
- `sm`: 16px of spacing and gap, for a rail or a dense grid. Used at 0 call sites; the rail card takes it, and 3 cards in the example files, such as `docs/system/examples/card/size-sm.tsx`.

Example: 16px of spacing, for a side column or a dense grid (`docs/system/examples/card/size-sm.tsx`)

```tsx
import { Card, CardContent } from "@/components/cobalt/ui/card";
import { Stat } from "@/components/cobalt/ui/stat";

const stats: { label: string; value: string; change: number; note: string }[] = [
  { label: "Net assets", value: "$4.8B", change: 6.1, note: "Against a year before" },
  { label: "Expense ratio", value: "0.42%", change: -2.3, note: "Below the median" },
  { label: "Holdings", value: "186", change: 6.9, note: "12 added in Q3" },
];

export default function Example() {
  return (
    // The row is a container, so the cards follow the room they get, not the window: three across
    // once each card has room for its figure on one line, and one column in a side column.
    <div className="@container w-full">
      <div className="grid gap-3 @xl:grid-cols-3">
        {stats.map((stat) => (
          // One part holds the label, the figure and its note, so they read as one group with the
          // stat's own rhythm instead of a header's gap between the label and its figure.
          <Card key={stat.label} size="sm">
            <CardContent>
              <Stat {...stat} positive={stat.label === "Expense ratio" ? "down" : "up"} />
            </CardContent>
          </Card>
        ))}
      </div>
    </div>
  );
}
```

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| default | render | read it, and use the controls inside it | the 16px corner and the 24px spacing | test |
| with-image | an `img` as the first child | read it | the image to the top edge and corners, no top padding | screenshot |
| as-section | `render={<section />}` or `<article />` | jump to it from the page outline | an `h2` title and a landmark | test |
| with-action | a `CardAction` child in the header | press the action | the action in the header's second column, top right, spanning the title and description rows, raised 4px so a 32px button sits centred on the title's line, and, for a ghost button, stepped out by its inset (8px for an icon, 12px for a word) so its glyph or word lines up with the content's edge, while a filled button keeps its box on that edge | test |

Example: A picture as the first child fills the top corners (`docs/system/examples/card/state-with-image.tsx`)

```tsx
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/cobalt/ui/card";

export default function Example() {
  return (
    <Card className="w-full max-w-sm">
      {/* The width and height hold the picture's place while it loads; the class crops it to a band. */}
      <img
        src="/photos/city-london.webp"
        alt="The London skyline over the Thames at sunset."
        width={800}
        height={800}
        decoding="async"
        className="h-36 w-full object-cover"
      />
      <CardHeader>
        <CardTitle>London office</CardTitle>
        <CardDescription>Desks for 12 on the fourth floor, facing the river.</CardDescription>
      </CardHeader>
      <CardContent>Two desks free today.</CardContent>
    </Card>
  );
}
```

Example: Rendered as a labelled section, with its title as an h2 (`docs/system/examples/card/state-as-section.tsx`)

```tsx
import { Card, CardContent, CardHeader, CardTitle } from "@/components/cobalt/ui/card";

const filings: [string, string][] = [
  ["Form ADV amendment", "Aug 29"],
  ["Form N-PORT", "Aug 14"],
  ["Form 13F", "Jul 30"],
];

export default function Example() {
  return (
    <Card render={<section aria-labelledby="recent-filings" />} className="w-full max-w-sm">
      <CardHeader>
        <CardTitle render={<h2 />} id="recent-filings">
          Recent filings
        </CardTitle>
      </CardHeader>
      <CardContent>
        <ul className="flex flex-col gap-3">
          {filings.map(([form, date]) => (
            <li key={form} className="flex justify-between gap-4">
              <span>{form}</span>
              <span className="text-muted-foreground tabular-nums">{date}</span>
            </li>
          ))}
        </ul>
      </CardContent>
    </Card>
  );
}
```

### State precedence
- With-image and with-action: both show. The image is above the header and the action inside it.
- As-section and every other state: both show. `render` changes the element, never the layout.
- Default and hover: Not applicable: a card has no hover; a link or button inside it hovers on its own.

### Motion
Not applicable: no motion. A card neither enters nor changes state in place; a host that swaps cards uses Crossfade.

## Props

<!-- Props generated from registry/cobalt/ui/card.tsx with TypeScript by scripts/props-table.mjs -->
### Card

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `size?` | `"default" \| "sm"` | `"default"` | `default` 24px of spacing and gap; `sm` 16px of spacing and gap. |

Also accepts every native `<div>` attribute; from Base UI: `render`.

### CardHeader

No props of its own.

Also accepts every native `<div>` attribute.

### CardFooter

No props of its own.

Also accepts every native `<div>` attribute.

### CardTitle

No props of its own.

Also accepts every native `<div>` attribute; from Base UI: `render`.

### CardAction

No props of its own.

Also accepts every native `<div>` attribute.

### CardDescription

No props of its own.

Also accepts every native `<div>` attribute.

### CardContent

No props of its own.

Also accepts every native `<div>` attribute.

Every native `div` attribute passes through the card and each part: `id`, `aria-labelledby`, `aria-*`. `render` on the card swaps the element for a `section` or an `article`, and on the title for a heading. `size` is `default` or `sm`. `className` is merged last and is for placement, such as `max-w-sm` or a grid span.

## Usage

### When to use
- One thing has a title, a line of detail and an action that belong together, and the page holds several such things side by side.
- A section's content is a set, such as lists or people, and each member needs its own surface.

### When not to use
- The content is a single list or table that fills the page. Use Section with the list in it instead, with no card.
- The surface floats over the page and needs a shadow. Use Popover or Dialog instead.
- The card would be a labelled block in a page's rail. Use RailCard instead, which is this card at the small size as a section.
- The thing is one row among many of the same shape. Use DataTable instead.

### Rules
- `rule/card-link-inside`: When a card leads somewhere, put the link in the title or the footer, such as `CardTitle` rendered through `render` with the host's link, instead of `onClick` on the card or an anchor around it, because a card holds other controls, and a card that is one big link has them nested inside it, which is invalid and gives one tab stop for several actions. Evidence: principle platform: an interactive element never contains another; principle wcag: 2.4.4 Link purpose in context, the link is the title's text, not the whole card's; app 22/22 example files outside the card folder keep the card inert and put the controls inside, and `docs/system/examples/card/default.tsx:47` puts its link in the footer (`rg -n "<Card\b" docs/system/examples --glob '!docs/system/examples/card/*'`). Check: lint trap/link-as-button (scripts/check-system.mjs); review.
  - Don't: `<a href="/lists/reading"><Card><CardHeader><CardTitle>Reading list</CardTitle></CardHeader><CardFooter><Button>Open</Button></CardFooter></Card></a>`
  - Do: `<Card><CardHeader><CardTitle render={<a href="/lists/reading" />}>Reading list</CardTitle></CardHeader><CardFooter><Button variant="secondary">Open</Button></CardFooter></Card>`
- `rule/card-section-when-titled`: When a card is a block of a page with its own title, render it with `render={<section aria-labelledby={id} />}` and its title with `render={<h2 />}` and that `id`, instead of a `div` with a bold line, because a page outline and a screen reader's landmark list find a section by its heading and skip a `div`. Evidence: single use `registry/cobalt/ui/rail-card.tsx:24-27`, the rail card renders every rail block this way; single use `docs/system/examples/card/state-as-section.tsx:12`, the filings card is a labelled section with an `h2`, which generalises because every titled block on a page is a section; principle wcag: 1.3.1 Info and relationships, headings and landmarks carry the structure. Check: test `card.test.tsx` renders the section form and reads the element, the label and the heading; review.
  - Don't: `<Card size="sm"><CardHeader><CardTitle>Recent</CardTitle></CardHeader></Card>`
  - Do: `<Card size="sm" render={<section aria-labelledby="recent" />}><CardHeader><CardTitle render={<h2 />} id="recent">Recent</CardTitle></CardHeader></Card>`
- `rule/card-empty-inside`: When a card has nothing to show, render Empty inside `CardContent` with the title in the product's words, instead of an empty card or a card left out, because a missing card shifts the grid and an empty one says nothing. Evidence: single use `docs/system/examples/empty/in-card.tsx:57`, the empty state inside a card, which generalises because every card's content can be empty; principle heuristic: visibility of system status, the card says why it is empty. Check: review of every card whose content comes from data.
  - Don't: `<Card><CardContent>{items.length > 0 && <List items={items} />}</CardContent></Card>`
  - Do: `<Card><CardContent>{items.length > 0 ? <List items={items} /> : <Empty align="start"><EmptyHeader><EmptyTitle>No saved items</EmptyTitle></EmptyHeader></Empty>}</CardContent></Card>`
- `rule/card-image-first-with-alt`: When a card has a cover, put the `img` as the card's first child with its own `alt`, instead of a background image on the card or an image after the header, because the card drops its top padding and clips to its corners only for a first-child `img`, and a background has no text alternative. Evidence: principle wcag: 1.1.1 Non-text content, an `img` carries `alt`, a background carries nothing; principle platform: the `has-[>img:first-child]:pt-0` selector in the card's class. Check: lint rule/arbitrary-value for a `bg-[url(` on a card (scripts/check-system.mjs); review.
  - Don't: `<Card className="bg-[url(/cover.jpg)] bg-cover"><CardHeader><CardTitle>Field notes</CardTitle></CardHeader></Card>`
  - Do: `<Card><img src="/cover.jpg" alt="The site at dawn" className="aspect-video w-full object-cover" /><CardHeader><CardTitle>Field notes</CardTitle></CardHeader></Card>`

### Content
- Follows `rule/writing-heading-sentence-case`.
- Follows `rule/writing-no-invented-numbers`.
- `rule/card-description-one-sentence`: When a `CardDescription` holds a sentence, write 1 sentence that ends in a period and says what the card holds or when it changed, such as "Twelve items, three unread.", instead of a second title or a paragraph, because the description sits 6px under the title at `text-sm` and a paragraph there pushes the content down on every card in the grid. A name or a label, such as a manager's name or "Net assets", is not a sentence and takes no period. Evidence: single use `docs/system/examples/card/default.tsx:27`, "Harbour Capital, filed Aug 29." is one sentence with a period; single use `docs/system/examples/card/state-with-image.tsx:18`, "Desks for 12 on the fourth floor, facing the river." is one sentence with a period; single use `docs/system/examples/card/in-section.tsx:28`, the manager's name takes none; person D21. Check: review.
  - Don't: `<CardDescription>Your reading list. It has twelve items and three of them are unread, and it was updated this morning by you</CardDescription>`
  - Do: `<CardDescription>Twelve items, three unread.</CardDescription>`

### Anti-slop
- `rule/card-flat`: When a card needs to stand out from the page, leave it as it is, the card colour on the page tint with its corner and spacing, instead of adding `border`, `shadow-*` or `ring-1` through `className`, because a card is flat (D11), and a border or a shadow on a resting surface draws an edge the page tint already draws. Evidence: person D11; measured 0px border and no shadow on the rendered card, .design-system/evidence/card/grow-title-390.json; app 28/28 cards in the example files outside the card folder pass at most a width or a margin in `className` (`rg -n "<Card\b" docs/system/examples --glob '!docs/system/examples/card/*'`). Check: lint rule/component-override on Card (scripts/check-system.mjs); probe `flatSurfaces` in probe.mjs.
  - Don't: `<Card className="border shadow-md">`
  - Do: `<Card>`
- `rule/card-no-eyebrow`: When a card needs a category above its title, put it in a `Badge` or a `Tag` beside the title or in the description, instead of a 12px label in capitals above the title, because a label in capitals is banned (D11, D12) and two stacked lines of title make two headings compete. Evidence: person D11; person D12. Check: lint rule/ban-uppercase (scripts/check-system.mjs).
  - Don't: `<CardHeader><div className="text-xs uppercase tracking-wide">LISTS</div><CardTitle>Reading list</CardTitle></CardHeader>`
  - Do: `<CardHeader><CardTitle className="flex items-center gap-2">Reading list <Badge variant="secondary">List</Badge></CardTitle></CardHeader>`
- `rule/card-no-nesting`: When a card's content is itself a set of things, lay them out in `CardContent` with `gap-3` or a list, instead of a card inside a card, because the inner card's colour equals the outer's, so its only edge would be a border, which a card never draws. Evidence: person D11; principle heuristic: aesthetic and minimalist design, one surface per unit. Check: lint by review of `data-slot="card"` inside `data-slot="card-content"`.
  - Don't: `<Card><CardContent><Card size="sm"><CardContent>Nested</CardContent></Card></CardContent></Card>`
  - Do: `<Card><CardContent className="flex flex-col gap-3"><div>First</div><div>Second</div></CardContent></Card>`

### Limits
- `rule/card-title-length`: When a title beside a `CardAction` would run past 33 characters at 390px, shorten it or move the detail into `CardDescription`, because at 34 characters the title wraps to a second line in a 310px card and the header grows while its neighbours' do not. Evidence: measured wrap at 34 characters in a 224px title column of a 310px card at 390, .design-system/evidence/card/grow-title-390.json. Check: probe `.design-system/scripts/grow-display.mjs` on the showcase at 390.
  - Don't: `<CardTitle>Reading list shared with the whole design team</CardTitle>`
  - Do: `<CardTitle>Reading list</CardTitle><CardDescription>Shared with the design team.</CardDescription>`

## Accessibility
Rests on a `div`, or on the host's `section` or `article` through `render`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before a card | skips the card and reaches the first control inside it, in document order: the action, then the content, then the footer | the first control inside, or the next control after the card | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| root, as a `div` | none | none | none | nothing of its own; its content is read in order | test |
| root, as a `section` | region | `aria-labelledby`, the title | none | the title and "region" when the reader lands on it; listed among the landmarks | test |
| title, as an `h2` | heading | its text | `aria-level` 2 | the text and "heading level 2" | test |
| title, as a `div` | none | none | none | the text as plain text | test |

## Tokens
| Part | State | Token |
|---|---|---|
| root | all | `--card`, `--card-foreground`, `--r-2xl`, `--card-spacing` set from `--spacing(6)`, `--spacing(4)` at the small size |
| description | all | `--muted-foreground` |

## Related
- RailCard: this card at the small size as a labelled section, for a page's rail.
- Section: a titled block of a page with no surface of its own.
- Empty: what goes in a card with nothing to show.
- Crossfade: swaps one card's content for another with a fade.
- Avatar: the user's picture, which is a mark rather than a card's content.
- DataTable: lists records with the same fields, as many as a server holds.
- ChartCard: a card with a chart's title, headline figure, change and note.
- MediaTile: a thing shown photo first, with no surface behind it.
- SettingsLayout: settings in sections, more than 2 of them.
- Thread: a conversation that scrolls on its own: day rows, runs of one sender, and a count of new messages instead of a jump.
- List and detail: a list beside the record that is open, which becomes two screens on a phone.
