# Card

A surface that groups related content.

Source: https://www.zazz.sh/api/primitives/card/

## Installation

```bash
pnpm dlx zazz-ui add card
```

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Anatomy

Use `<ui-card>` when the card would otherwise be a `div`, and `data-ui="card"` when it has a better element of its own: `<article data-ui="card">` for a post or a product, `<form data-ui="card">` for a sign-in form.

Mark its direct children with `data-card-slot`:

- `header`: an image, a `<figure>`, or an `<hgroup>`. A header that is an `img`, `picture`, or `video`, a `figure` without a `figcaption`, or an element holding only an `img`, `picture`, or `video` bleeds over the card's padding to its rounded edge. The bleed follows `--p` (and `--ui-card-p`), not the per-side padding utilities like `--pt` or `--px`.
- `body`: the card's main content.
- `footer`: actions, a price, or an author.

`data-card-orientation="horizontal"` puts the header in a start column spanning the card's rows, with the body above the footer in the end column, and needs a `header` slot. It applies once the card's nearest container is at least 40rem wide (the `sm` breakpoint) and stacks below that, so give the card's parent `--container-type: inline-size` to follow the parent's width instead of the page's (a `data-ui="layout"` child already is one). Set the column's width with `--ui-card-header-w` (default `40%`; `auto` fits an avatar). A media header (an `img` or `video`, bare or alone in a `figure` or wrapper) fills the column's height and crops to it with `object-fit: cover`. `data-card-orientation="vertical"` names the default, stacked layout.

## Examples

### Default

```html
<div
  style="
    --display: grid;
    --template-cols: repeat(auto-fit, minmax(min(100%, max(14rem, 40%)), 1fr));
    --gap: 6;
    --w: 100%;
  "
>
  <ui-card>
    <figure
      data-card-slot="header"
      style="
        --aspect: 3 / 2;
        --radius: var(--ui-card-radius);
        --overflow: hidden;
      "
    >
      <img
        width="600"
        height="400"
        loading="lazy"
        src="https://images.unsplash.com/photo-1564760055775-d63b17a55c44?auto=format&fit=crop&w=600&h=400&q=60"
        alt="Elephants crossing a dusty riverbed"
        style="
          --size: 100%;
          --object-fit: cover;
        "
      />
    </figure>
    <hgroup
      data-card-slot="body"
      style="
        --display: grid;
        --gap: 1;
      "
    >
      <h3 data-ui="text-lg">Default</h3>
      <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
        Bare: no padding, radius, or surface, so media runs edge to edge.
      </p>
    </hgroup>
    <footer data-card-slot="footer">
      <a href="#" data-ui="button" data-button-variant="outline">Read field notes</a>
    </footer>
  </ui-card>
  <ui-card data-card-variant="outline">
    <figure data-card-slot="header" style="--aspect: 3 / 2">
      <img
        width="600"
        height="400"
        loading="lazy"
        src="https://images.unsplash.com/photo-1564760055775-d63b17a55c44?auto=format&fit=crop&w=600&h=400&q=60"
        alt="Elephants crossing a dusty riverbed"
        style="
          --size: 100%;
          --object-fit: cover;
        "
      />
    </figure>
    <hgroup
      data-card-slot="body"
      style="
        --display: grid;
        --gap: 1;
      "
    >
      <h3 data-ui="text-lg">Outline</h3>
      <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
        Padding, the radius, and a border. The header image bleeds to the card's edges.
      </p>
    </hgroup>
    <footer data-card-slot="footer">
      <a href="#" data-ui="button" data-button-variant="outline">Read field notes</a>
    </footer>
  </ui-card>
  <ui-card data-card-variant="muted">
    <figure data-card-slot="header" style="--aspect: 3 / 2">
      <img
        width="600"
        height="400"
        loading="lazy"
        src="https://images.unsplash.com/photo-1564760055775-d63b17a55c44?auto=format&fit=crop&w=600&h=400&q=60"
        alt="Elephants crossing a dusty riverbed"
        style="
          --size: 100%;
          --object-fit: cover;
        "
      />
    </figure>
    <hgroup
      data-card-slot="body"
      style="
        --display: grid;
        --gap: 1;
      "
    >
      <h3 data-ui="text-lg">Muted</h3>
      <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
        Padding, the radius, and the muted surface, for quieter groupings.
      </p>
    </hgroup>
    <footer data-card-slot="footer">
      <a href="#" data-ui="button" data-button-variant="primary">Read field notes</a>
    </footer>
  </ui-card>
  <ui-card data-card-variant="floating">
    <figure data-card-slot="header" style="--aspect: 3 / 2">
      <img
        width="600"
        height="400"
        loading="lazy"
        src="https://images.unsplash.com/photo-1564760055775-d63b17a55c44?auto=format&fit=crop&w=600&h=400&q=60"
        alt="Elephants crossing a dusty riverbed"
        style="
          --size: 100%;
          --object-fit: cover;
        "
      />
    </figure>
    <hgroup
      data-card-slot="body"
      style="
        --display: grid;
        --gap: 1;
      "
    >
      <h3 data-ui="text-lg">Floating</h3>
      <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
        Padding, the radius, the card surface, a border, and a small shadow.
      </p>
    </hgroup>
    <footer data-card-slot="footer">
      <a href="#" data-ui="button" data-button-variant="primary">Read field notes</a>
    </footer>
  </ui-card>
</div>
```

### Horizontal

```html
<div
  style="
    --display: grid;
    --gap: 6;
  "
>
  <ui-card data-card-variant="outline" data-card-orientation="horizontal">
    <figure data-card-slot="header">
      <img
        width="600"
        height="400"
        loading="lazy"
        src="https://images.unsplash.com/photo-1564760055775-d63b17a55c44?auto=format&fit=crop&w=600&h=400&q=60"
        alt="Elephants crossing a dusty riverbed"
        style="
          --size: 100%;
          --object-fit: cover;
        "
      />
    </figure>
    <hgroup
      data-card-slot="body"
      style="
        --display: grid;
        --gap: 1;
      "
    >
      <h3 data-ui="text-lg">Horizontal</h3>
      <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
        The header takes the start column, with the body above the footer in the end column. Below a
        40rem container, the card stacks.
      </p>
    </hgroup>
    <footer data-card-slot="footer">
      <a href="#" data-ui="button" data-button-variant="outline">Read field notes</a>
    </footer>
  </ui-card>
  <ui-card
    data-card-variant="floating"
    data-card-orientation="horizontal"
    style="--ui-card-header-w: auto"
  >
    <div data-card-slot="header">
      <figure
        role="img"
        aria-label="John Doe"
        style="
          --display: grid-pile;
          --size: 12;
          --text: var(--color-primary-foreground);
          --bg: var(--color-primary);
          --radius: full;
          --overflow: clip;
        "
      >
        <span aria-hidden="true" style="--font-weight: strong">AO</span>
      </figure>
    </div>
    <hgroup
      data-card-slot="body"
      style="
        --display: grid;
        --gap: 1;
      "
    >
      <h3 data-ui="text-lg">John Doe</h3>
      <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
        Lead tracker, Okavango survey. A header that isn't media keeps the card's padding, and
        <code>--ui-card-header-w: auto</code> sizes its column to the avatar.
      </p>
    </hgroup>
    <footer
      data-card-slot="footer"
      style="
        --display: flex;
        --gap: 2;
      "
    >
      <button type="button" data-ui="button" data-button-variant="primary">Follow</button>
      <button type="button" data-ui="button" data-button-variant="ghost">Message</button>
    </footer>
  </ui-card>
</div>
```

### Product

```html
<ui-card data-card-variant="outline" style="--max-w: 80">
  <figure
    data-card-slot="header"
    style="
      --aspect: 1;
      --bg: var(--color-muted);
    "
  >
    <img
      width="480"
      height="480"
      loading="lazy"
      src="https://images.unsplash.com/photo-1505740420928-5e560c06d30e?auto=format&fit=crop&w=480&h=480&q=60"
      alt="Over-ear headphones on a yellow background"
      style="
        --size: 100%;
        --object-fit: cover;
      "
    />
  </figure>
  <div
    data-card-slot="body"
    style="
      --display: grid;
      --gap: 2;
    "
  >
    <div
      style="
        --display: flex;
        --items: center;
        --gap: 2;
      "
    >
      <span data-ui="badge" data-badge-variant="success">In stock</span>
      <span data-ui="badge" data-badge-variant="muted">Free shipping</span>
    </div>
    <h3 data-ui="text-lg">Field monitor headphones</h3>
    <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
      Closed-back, 40 hours of battery, and a fold-flat frame for the trail.
    </p>
    <p data-ui="text-xl" style="--font-weight: strong">$189</p>
  </div>
  <footer
    data-card-slot="footer"
    style="
      --display: flex;
      --gap: 2;
    "
  >
    <button type="button" data-ui="button" data-button-variant="primary" style="--flex: 1">
      Add to cart
    </button>
    <button
      type="button"
      data-ui="button"
      data-button-variant="outline"
      data-button-size="icon"
      aria-label="Save to wishlist"
      aria-pressed="false"
    >
      <svg
        xmlns="http://www.w3.org/2000/svg"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
      >
        <path
          d="M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z"
        />
      </svg>
    </button>
  </footer>
</ui-card>
```

### Login

```html
<form
  data-ui="card"
  data-card-variant="floating"
  aria-labelledby="card-login-title"
  style="
    --w: 100%;
    --max-w: 96;
  "
>
  <hgroup
    data-card-slot="header"
    style="
      --display: grid;
      --gap: 1;
    "
  >
    <h3 id="card-login-title" data-ui="text-lg">Sign in</h3>
    <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
      Log sightings and sync your field notes.
    </p>
  </hgroup>
  <div
    data-card-slot="body"
    style="
      --display: grid;
      --gap: 4;
    "
  >
    <div data-ui="field">
      <label data-field-slot="label" for="card-login-email">Email</label>
      <input
        data-ui="input"
        id="card-login-email"
        name="card-login-email"
        type="email"
        required
        placeholder="you@example.com"
        autocomplete="email"
      />
    </div>
  </div>
  <footer
    data-card-slot="footer"
    style="
      --display: grid;
      --gap: 3;
      --text-align: center;
    "
  >
    <button type="submit" data-ui="button" data-button-variant="primary">Send a login code</button>
    <p data-ui="text-sm">
      New here?
      <a href="#" data-ui="text-link">Create an account</a>
    </p>
  </footer>
</form>
```

### Article

```html
<article data-ui="card" data-card-variant="floating" style="--max-w: 96">
  <figure data-card-slot="header" style="--aspect: 3 / 2">
    <img
      width="600"
      height="400"
      loading="lazy"
      src="https://images.unsplash.com/photo-1564760055775-d63b17a55c44?auto=format&fit=crop&w=600&h=400&q=60"
      alt="Elephants crossing a dusty riverbed"
      style="
        --size: 100%;
        --opacity: 1;
        --opacity--hover: 0.75;
        --scale: 1;
        --scale--hover: 1.02;
        --transition: var(--default-transition);
        --object-fit: cover;
      "
    />
  </figure>
  <div
    data-card-slot="body"
    style="
      --display: grid;
      --gap: 2;
    "
  >
    <div
      style="
        --display: flex;
        --items: center;
        --gap: 2;
      "
    >
      <span data-ui="badge" data-badge-variant="muted">Research</span>
    </div>
    <hgroup
      style="
        --display: grid;
        --gap: 2;
      "
    >
      <h3 data-ui="text-lg">
        <a href="#">Migration patterns</a>
      </h3>
      <p
        data-ui="text-sm"
        style="
          --line-clamp: 2;
          --text: var(--color-muted-foreground);
        "
      >
        Satellite collars reveal ancient routes herds still follow each dry season across the
        floodplain.
      </p>
    </hgroup>
  </div>
  <footer
    data-card-slot="footer"
    style="
      --display: flex;
      --items: center;
      --pt: 4;
      --gap: 2;
      --border-t: 1;
    "
  >
    <figure
      role="img"
      style="
        --display: grid-pile;
        --size: 11;
        --border-color: var(--color-border);
        --border-width: 1px;
        --radius: full;
        --overflow: clip;
      "
    >
      <img
        width="64"
        height="64"
        loading="lazy"
        src="https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?auto=format&fit=facearea&facepad=2&w=64&h=64&q=90"
        alt="John Doe"
        aria-hidden="true"
        style="
          --order: 9999;
          --size: 100%;
          --bg: transparent;
          --object-fit: cover;
        "
      />
      <figcaption
        style="
          --display: flex;
          --items: center;
          --justify: center;
          --order: -9999;
          --size: 100%;
          --font-weight: strong;
          --text: var(--color-primary-foreground);
          --bg: var(--color-primary);
        "
      >
        <span style="--sr: only">John Doe</span>
        <span aria-hidden="true">JD</span>
      </figcaption>
    </figure>
    <div style="--display: flex-col">
      <span data-ui="text-sm" style="--font-weight: strong">John Doe</span>
      <span data-ui="text-xs" style="--text: var(--color-muted-foreground)"
        >Founder, ACME Inc.</span
      >
    </div>
  </footer>
</article>
```

### Subgrid

```html
<div
  style="
    --display: grid;
    --template-cols: repeat(auto-fit, minmax(min(100%, max(14rem, 40%)), 1fr));
    --gap: 6;
    --w: 100%;
  "
>
  <ui-card
    data-card-variant="outline"
    style="
      --row-span: 3;
      --template-rows: subgrid;
    "
  >
    <figure data-card-slot="header" style="--aspect: 3 / 2">
      <img
        width="600"
        height="400"
        loading="lazy"
        src="https://images.unsplash.com/photo-1564760055775-d63b17a55c44?auto=format&fit=crop&w=600&h=400&q=60"
        alt="Elephants crossing a dusty riverbed"
        style="
          --size: 100%;
          --object-fit: cover;
        "
      />
    </figure>
    <div
      data-card-slot="body"
      style="
        --display: grid;
        --place-content: start;
        --gap: 2;
      "
    >
      <div
        style="
          --display: flex;
          --items: center;
          --gap: 2;
        "
      >
        <span data-ui="badge" data-badge-variant="muted">Research</span>
      </div>
      <h3 data-ui="text-lg">Migration patterns</h3>
      <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
        Satellite collars reveal ancient routes herds still follow each dry season across the
        floodplain.
      </p>
    </div>
    <footer
      data-card-slot="footer"
      style="
        --pt: 4;
        --border-t: 1;
      "
    >
      <a
        href="#"
        data-ui="button"
        data-button-variant="link"
        style="
          --justify: end;
          --text-align: end;
        "
        >Read article &rarr;</a
      >
    </footer>
  </ui-card>
  <ui-card
    data-card-variant="outline"
    style="
      --row-span: 3;
      --template-rows: subgrid;
    "
  >
    <figure data-card-slot="header" style="--aspect: 3 / 2">
      <img
        width="600"
        height="400"
        loading="lazy"
        src="https://images.unsplash.com/photo-1500382017468-9049fed747ef?auto=format&fit=crop&w=600&h=400&q=60"
        alt="Zebras grazing at dawn on the savanna"
        style="
          --size: 100%;
          --object-fit: cover;
        "
      />
    </figure>
    <div
      data-card-slot="body"
      style="
        --display: grid;
        --place-content: start;
        --gap: 2;
      "
    >
      <div
        style="
          --display: flex;
          --items: center;
          --gap: 2;
        "
      >
        <span data-ui="badge" data-badge-variant="muted">Field notes</span>
      </div>
      <h3 data-ui="text-lg">How quickly zebras jump</h3>
      <p data-ui="text-sm" style="--text: var(--color-muted-foreground)">
        Striped coats and sudden bursts of speed help herds evade predators when the grasslands go
        quiet at dusk, long after the light fades from the plain.
      </p>
    </div>
    <footer
      data-card-slot="footer"
      style="
        --pt: 4;
        --border-t: 1;
      "
    >
      <a
        href="#"
        data-ui="button"
        data-button-variant="link"
        style="
          --justify: end;
          --text-align: end;
        "
        >Read article &rarr;</a
      >
    </footer>
  </ui-card>
</div>
```

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="card"` | identity | free value |
| `<ui-card>` | tag | free value |
| `data-card-orientation` | preset | `horizontal` `vertical` |
| `data-card-variant` | preset | `floating` `muted` `outline` |
| `data-card-slot` | slot | `body` `footer` `header` |

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-card-bg` | `transparent` | — |
| `--ui-card-border-color` | `transparent` | — |
| `--ui-card-border-style` | `solid` | — |
| `--ui-card-border-width` | `1px` | — |
| `--ui-card-corner-shape` | `var(--radius-corner-shape)` | — |
| `--ui-card-display` | `grid` | — |
| `--ui-card-gap` | `var(--space-sm)` | — |
| `--ui-card-header-w` | `40%` | — |
| `--ui-card-p` | `0` | — |
| `--ui-card-place-content` | `start stretch` | — |
| `--ui-card-radius` | `var(--radius-lg)` | — |
| `--ui-card-shadow` | `none` | — |
| `--ui-card-text` | `var(--color-card-foreground)` | — |
