# Card

A self-contained item, such as an article teaser, on a raised surface.

**Our choice:** No standard exists: an `<article>` whose heading holds the one link, so the link name stays short.

_Diagram: Cards sit in a grid in the content area, each a raised box with a heading link and a short text._

## Live example

### Cards in a grid

```html
<div class="grid" style="--grid-min: 16rem">
  <article data-card>
    <header>
      <h3>Starter plan</h3>
      <p>For one person trying things out.</p>
    </header>
    <p>Free forever, with every component and theme.</p>
    <footer>
      <a href="#grid-title" data-button="primary small">Choose Starter</a>
    </footer>
  </article>
  <article data-card="link">
    <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 160'%3E%3Crect width='320' height='160' fill='%233451d1'/%3E%3Ccircle cx='250' cy='40' r='60' fill='%238ea2ff'/%3E%3Crect x='24' y='96' width='140' height='40' rx='8' fill='%23f6f7f9'/%3E%3C/svg%3E" alt="" width="320" height="160">
    <header>
      <h3><a href="#grid-title">Theming guide</a></h3>
      <p>Override semantic tokens to make a theme.</p>
    </header>
    <p>No build step: set a few custom properties and every component follows.</p>
    <footer>
      <span data-badge="accent">Guide</span>
      <a href="#grid-title" data-button="secondary small">View tokens</a>
    </footer>
  </article>
  <article data-card="link">
    <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 160'%3E%3Crect width='320' height='160' fill='%2317773a'/%3E%3Cpath d='M0 160 L120 50 L200 120 L260 70 L320 120 V160Z' fill='%235fd38d'/%3E%3C/svg%3E" alt="" width="320" height="160">
    <h3><a href="#grid-title">Atoms</a></h3>
    <p>Buttons, switches, fields and badges.</p>
  </article>
</div>
```

## Attributes

| Attribute or value | Where | Effect |
| --- | --- | --- |
| `data-card` | `<article>` (or `<section>`, `<li>`) | the card |
| `data-card="link"` |  | the whole card follows the link in its heading |

Children: an optional first `<img>` (or `<picture>`, `<video>`) as media, a heading, body content, and an optional `<footer>` for actions.

Wrap the heading and a one-line description in a `<header>` to set them close together, with the description muted, as shadcn/ui's card header does (<https://ui.shadcn.com/docs/components/card>).

### Elevation

`--dui-elevation-raised` and `--dui-elevation-raised-surface` (level 1), whose 1px ring draws the edge. The border is transparent; forced colors mode, which drops box-shadow, paints it in canvastext.

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Tab</kbd> | moves to each card's heading link, then to its footer actions |
| <kbd>Enter</kbd> | follows the focused link |

## Accessibility

- Every card has a heading. Use `<article>` when it stands on its own.
- Images need `alt`, or `alt=""` when decorative.
- `data-card="link"` stretches the heading's link over the card, so the link name is the heading ([Inclusive Components](https://inclusive-components.design/cards/)). Footer actions stay focusable. Card text cannot be selected with the mouse.

## Specifications

- [HTML `<article>` element](https://html.spec.whatwg.org/multipage/sections.html#the-article-element)
- [ui.shadcn.com card](https://ui.shadcn.com/docs/components/card)
- [WCAG Link purpose in context](https://www.w3.org/WAI/WCAG22/Understanding/link-purpose-in-context.html)
- [WCAG Focus visible](https://www.w3.org/WAI/WCAG22/Understanding/focus-visible.html)
- [W3C images](https://www.w3.org/WAI/tutorials/images/)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- Source: [`css/components/molecules/card.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/card.css), [`examples/molecules/card.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/card.html)

## How other systems do it

For comparison only; these are not the standard this system follows.

- Apple Human Interface Guidelines: No equivalent
- [Material Design 3: Cards](https://m3.material.io/components/cards/overview)
- Vercel Geist: No equivalent
