---
title: "Density"
description: "Compactness is an explicit whitespace policy, not a global zoom factor. Keep typography, geometry and target sizes independent."
slug: "density"
---

import { DocPage, PageHeader, SkillPanel, Example, ExampleLabel, ExampleHint, ExampleCode, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, PageOverlay, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeCard, TableCard } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="density" title="Density" introStyle="max-width:38rem;">
  Compactness is an explicit whitespace policy, not a global zoom factor. Keep typography, geometry and target sizes independent.
</PageHeader>

<h2 id="presets" class="h2-display">Opt in to interface density</h2>

<DocText>Density changes whitespace, not display resolution. The presets are adjustable design choices: compact <code>0.75</code>, comfortable <code>1</code>, spacious <code>1.25</code>. They inherit into descendants; an inner density attribute starts a new density context. Every code block on this page shows exactly the classes that render the demo above it - only the demo's decorative colors are omitted.</DocText>

```html example schema="none"
<div class="flex flex-wrap gap-4" data-demo="density">
  <div data-density="compact" class="stack demo-outline p-3" style="flex: 1; min-width: 10rem">
    <p class="demo-label demo-label-inline">compact - 0.75×</p>
    <div class="demo-tile p-4" data-variant="muted">p-4</div>
    <div class="demo-tile p-4" data-variant="muted">p-4</div>
  </div>
  <div class="stack demo-outline p-3" style="flex: 1; min-width: 10rem">
    <p class="demo-label demo-label-inline">comfortable - 1×</p>
    <div class="demo-tile p-4" data-variant="muted">p-4</div>
    <div class="demo-tile p-4" data-variant="muted">p-4</div>
  </div>
  <div data-density="spacious" class="stack demo-outline p-3" style="flex: 1; min-width: 10rem">
    <p class="demo-label demo-label-inline">spacious - 1.25×</p>
    <div class="demo-tile p-4" data-variant="muted">p-4</div>
    <div class="demo-tile p-4" data-variant="muted">p-4</div>
  </div>
</div>
```


<div class="sep" />

<h2 id="what-changes" class="h2-display">What changes and what does not</h2>

<div class="overflow-x-auto mb-6">
  <table class="table" data-variant="simple">
    <thead>
      <tr>
        <th scope="col">Quantity</th>
        <th scope="col">Density behavior</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Numeric gaps, padding, margins; stack gap; default container gutters</td>
        <td>Scaled.</td>
      </tr>
      <tr>
        <td>Numeric widths, heights, square sizes, icon dimensions</td>
        <td>Unchanged.</td>
      </tr>
      <tr>
        <td>Fonts and line heights</td>
        <td>Unchanged.</td>
      </tr>
      <tr>
        <td>Literal px spacing, automatic margins, percentage sizes</td>
        <td>Unchanged.</td>
      </tr>
      <tr>
        <td>Existing button/input recipes and their fixed dimensions</td>
        <td>Unchanged until explicitly migrated.</td>
      </tr>
      <tr>
        <td>Interaction target minima</td>
        <td>Independent CSS-pixel constraints.</td>
      </tr>
    </tbody>
  </table>
</div>

<div class="sep" />

<h2 id="real-world" class="h2-display">A realistic compact surface</h2>

<DocText>The same settings panel under two density contexts. In <code>compact</code> the <code>p-4</code> panel padding and the <code>stack</code>'s default gap scale to 0.75×, in <code>spacious</code> to 1.25× - typography, button heights and border radii stay identical. That is the density contract on real UI: a whitespace policy, not a zoom.</DocText>

```html example schema="none"
<div class="flex flex-wrap gap-4">
  <div style="flex: 1; min-width: 14rem">
    <p class="demo-label">data-density="compact"</p>
    <div data-density="compact" class="card stack p-4">
      <div class="flex items-center gap-3">
        <div>
          <p class="text-sm m-0">Wi-Fi</p>
          <p class="text-xs text-muted-foreground m-0">Connected to HomeNet</p>
        </div>
        <button class="btn ms-auto" type="button" data-variant="outline" data-size="sm">Edit</button>
      </div>
      <div class="flex items-center gap-3">
        <div>
          <p class="text-sm m-0">Bluetooth</p>
          <p class="text-xs text-muted-foreground m-0">2 devices paired</p>
        </div>
        <button class="btn ms-auto" type="button" data-variant="outline" data-size="sm">Edit</button>
      </div>
      <div class="flex items-center gap-3">
        <div>
          <p class="text-sm m-0">Notifications</p>
          <p class="text-xs text-muted-foreground m-0">Allowed for 12 apps</p>
        </div>
        <button class="btn ms-auto" type="button" data-variant="outline" data-size="sm">Edit</button>
      </div>
    </div>
  </div>
  <div style="flex: 1; min-width: 14rem">
    <p class="demo-label">data-density="spacious"</p>
    <div data-density="spacious" class="card stack p-4">
      <div class="flex items-center gap-3">
        <div>
          <p class="text-sm m-0">Wi-Fi</p>
          <p class="text-xs text-muted-foreground m-0">Connected to HomeNet</p>
        </div>
        <button class="btn ms-auto" type="button" data-variant="outline" data-size="sm">Edit</button>
      </div>
      <div class="flex items-center gap-3">
        <div>
          <p class="text-sm m-0">Bluetooth</p>
          <p class="text-xs text-muted-foreground m-0">2 devices paired</p>
        </div>
        <button class="btn ms-auto" type="button" data-variant="outline" data-size="sm">Edit</button>
      </div>
      <div class="flex items-center gap-3">
        <div>
          <p class="text-sm m-0">Notifications</p>
          <p class="text-xs text-muted-foreground m-0">Allowed for 12 apps</p>
        </div>
        <button class="btn ms-auto" type="button" data-variant="outline" data-size="sm">Edit</button>
      </div>
    </div>
  </div>
</div>
```


<div class="sep" />

<h2 id="advanced" class="h2-display">Real world: a dense data table</h2>

<DocText>The same four records - name, role, last modified - rendered twice with identical markup; only the density attribute changes. In <code>compact</code> the panel's <code>p-4</code> padding, the <code>stack</code> gap and each row's <code>gap-2</code> shrink to 0.75×; in <code>spacious</code> they grow to 1.25×. The avatars (<code>size-8</code>), the font sizes and the <code>ms-auto</code> date alignment do not move: density scales the whitespace between and around the rows, never the type or the geometry. That is what makes a compact mode safe to offer on data-heavy screens - rows pack tighter without shrinking text below readability.</DocText>

```html example schema="none"
<div class="flex flex-wrap gap-4">
  <div style="flex: 1; min-width: 14rem">
    <p class="demo-label">data-density="compact"</p>
    <div data-density="compact" class="card stack p-4">
      <div class="flex items-center gap-2">
        <div class="demo-tile size-8 shrink-0" data-variant="secondary">AL</div>
        <div class="flex-1 min-w-0">
          <p class="text-sm m-0">Ada Lovelace</p>
          <p class="text-xs text-muted-foreground m-0">Engineer</p>
        </div>
        <span class="text-xs text-muted-foreground ms-auto shrink-0">Mar 4</span>
      </div>
      <div class="flex items-center gap-2">
        <div class="demo-tile size-8 shrink-0" data-variant="secondary">GH</div>
        <div class="flex-1 min-w-0">
          <p class="text-sm m-0">Grace Hopper</p>
          <p class="text-xs text-muted-foreground m-0">Admiral</p>
        </div>
        <span class="text-xs text-muted-foreground ms-auto shrink-0">Mar 3</span>
      </div>
      <div class="flex items-center gap-2">
        <div class="demo-tile size-8 shrink-0" data-variant="secondary">AT</div>
        <div class="flex-1 min-w-0">
          <p class="text-sm m-0">Alan Turing</p>
          <p class="text-xs text-muted-foreground m-0">Mathematician</p>
        </div>
        <span class="text-xs text-muted-foreground ms-auto shrink-0">Feb 27</span>
      </div>
      <div class="flex items-center gap-2">
        <div class="demo-tile size-8 shrink-0" data-variant="secondary">ED</div>
        <div class="flex-1 min-w-0">
          <p class="text-sm m-0">Edsger Dijkstra</p>
          <p class="text-xs text-muted-foreground m-0">Scientist</p>
        </div>
        <span class="text-xs text-muted-foreground ms-auto shrink-0">Feb 19</span>
      </div>
    </div>
  </div>
  <div style="flex: 1; min-width: 14rem">
    <p class="demo-label">data-density="spacious"</p>
    <div data-density="spacious" class="card stack p-4">
      <div class="flex items-center gap-2">
        <div class="demo-tile size-8 shrink-0" data-variant="secondary">AL</div>
        <div class="flex-1 min-w-0">
          <p class="text-sm m-0">Ada Lovelace</p>
          <p class="text-xs text-muted-foreground m-0">Engineer</p>
        </div>
        <span class="text-xs text-muted-foreground ms-auto shrink-0">Mar 4</span>
      </div>
      <div class="flex items-center gap-2">
        <div class="demo-tile size-8 shrink-0" data-variant="secondary">GH</div>
        <div class="flex-1 min-w-0">
          <p class="text-sm m-0">Grace Hopper</p>
          <p class="text-xs text-muted-foreground m-0">Admiral</p>
        </div>
        <span class="text-xs text-muted-foreground ms-auto shrink-0">Mar 3</span>
      </div>
      <div class="flex items-center gap-2">
        <div class="demo-tile size-8 shrink-0" data-variant="secondary">AT</div>
        <div class="flex-1 min-w-0">
          <p class="text-sm m-0">Alan Turing</p>
          <p class="text-xs text-muted-foreground m-0">Mathematician</p>
        </div>
        <span class="text-xs text-muted-foreground ms-auto shrink-0">Feb 27</span>
      </div>
      <div class="flex items-center gap-2">
        <div class="demo-tile size-8 shrink-0" data-variant="secondary">ED</div>
        <div class="flex-1 min-w-0">
          <p class="text-sm m-0">Edsger Dijkstra</p>
          <p class="text-xs text-muted-foreground m-0">Scientist</p>
        </div>
        <span class="text-xs text-muted-foreground ms-auto shrink-0">Feb 19</span>
      </div>
    </div>
  </div>
</div>
```


<div class="sep" />

<h2 id="component-density" class="h2-display">Components with native density</h2>

<DocText>Beyond the utility scale above, the components whose information density a user actually dials in implement <code>data-density</code> themselves - set it on the component root and their internal whitespace scales with the same <code>0.75 / 1 / 1.25</code> ratio, <code>comfortable</code> being identical to the unsized default. Each component page shows the three steps side by side:</DocText>

<div class="overflow-x-auto mb-6">
  <table class="table" data-variant="simple">
    <thead>
      <tr>
        <th scope="col">Component</th>
        <th scope="col">What the density scales</th>
      </tr>
    </thead>
    <tbody>
      <tr><td><a class="link" href="card.html">Card</a></td><td>Header / content / footer section paddings</td></tr>
      <tr><td><a class="link" href="table.html">Table</a></td><td>Head, cell and caption padding</td></tr>
      <tr><td><a class="link" href="accordion.html">Accordion</a></td><td>Trigger and panel padding</td></tr>
      <tr><td><a class="link" href="steps.html">Steps</a></td><td>Step and content gaps (size ladder owns indicator size)</td></tr>
      <tr><td><a class="link" href="timeline.html">Timeline</a></td><td>Per-item rhythm (connector gap + trailing space)</td></tr>
      <tr><td><a class="link" href="tree-view.html">Tree View</a></td><td>Row padding-block (indent is structural, never scaled)</td></tr>
      <tr><td><a class="link" href="panel.html">Panel</a></td><td>Title bar height and the body padding (the card's scale)</td></tr>
      <tr><td><a class="link" href="sidebar.html">Sidebar</a></td><td>Link rows and the content frame</td></tr>
      <tr><td><a class="link" href="command.html">Command</a></td><td>Palette rows, search wrapper, group headings</td></tr>
      <tr><td><a class="link" href="context-menu.html">Context Menu</a></td><td>Container padding and item rows</td></tr>
      <tr><td><a class="link" href="sortable.html">Sortable</a></td><td>List gap and item padding</td></tr>
      <tr><td><a class="link" href="pagination.html">Pagination</a></td><td>Inter-cell gap only (size ladder owns cell dimensions)</td></tr>
    </tbody>
  </table>
</div>

<DocText>Components already covered by a dedicated size ladder (Button, Input, Badge, ...) deliberately stay out: their compactness is the size axis, and stacking a second whitespace axis on the same padding would make the two fight. Density stays the policy for <em>collections and surfaces</em>; sizes stay the policy for <em>controls</em>.</DocText>

<div class="sep" />

<h2 id="custom-density" class="h2-display">Customize a context</h2>

```css
.dense-inspector { --layout-density: 0.9; }

/* Component-owned opt-in recipe. No changes to existing .btn rules. */
.inspector-action {
  min-inline-size: 44px;
  min-block-size: 44px;
  padding-inline: calc(var(--size-base) * 3 * var(--layout-density));
}
```


<DocText>Use a valid nonnegative number. Numeric spacing clamps negative density to zero; invalid CSS token types are not runtime-validated. Named size aliases stay geometric. Do not express density by changing the root font size or devicePixelRatio.</DocText>

<div class="sep" />

<h2 id="input-policy" class="h2-display">Pointer capability is a separate policy</h2>

```css
.inspector-action {
  min-inline-size: 44px;
  min-block-size: 44px;
}

@media (any-pointer: coarse) {
  .inspector-action { padding-inline: 1rem; }
}
```


<DocText>The example already keeps a 44px target baseline; the media query only adds breathing room. <code>any-pointer: coarse</code> can detect an available coarse pointer on a hybrid device, but it is not a complete description of a user's access needs. Keep controls usable without that signal. <a class="link" href="https://www.w3.org/TR/mediaqueries-4/#any-input" target="_blank" rel="noopener">Media Queries: available pointer capabilities</a></DocText>

<DocText>The AA target-size criterion is 24 × 24 CSS pixels with exceptions; the enhanced AAA criterion uses 44 × 44 with exceptions. Check actual target geometry, not just icon dimensions. <a class="link" href="https://www.w3.org/WAI/WCAG22/Understanding/target-size-minimum.html" target="_blank" rel="noopener">WCAG 2.2: Target Size (Minimum), AA</a> · <a class="link" href="https://www.w3.org/WAI/WCAG22/Understanding/target-size-enhanced.html" target="_blank" rel="noopener">WCAG 2.2: Target Size (Enhanced), AAA</a></DocText>

</DocPage>
