---
title: "Checkbox"
description: "Checkbox component - native checkboxes with custom styling and indeterminate state."
slug: "checkbox"
---

import { DocPage, PageHeader, SkillPanel, 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="checkbox" title="Checkbox" component="checkbox">
  A control that toggles between checked and unchecked. Built on native <code>&lt;input type="checkbox"&gt;</code> with CSS-only checkmark.
</PageHeader>

<SkillPanel component="checkbox">
  <SkillField label="Native basis">
<SkillText><code>&lt;input type="checkbox"&gt;</code> element styled with CSS <code>appearance: none</code>.</SkillText>
  </SkillField>

  <SkillField label="Web Platform APIs">
<SkillRow>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox" title="native toggle control with built-in keyboard and form support"><code>&lt;input type="checkbox"&gt;</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/:checked" title="matches checked state"><code>:checked</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/:indeterminate" title="matches the indeterminate (mixed) state"><code>:indeterminate</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible" title="keyboard-only focus ring"><code>:focus-visible</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid" title="post-interaction validation styling"><code>:user-invalid</code></ApiPill>
  </SkillRow>
  </SkillField>

  <SkillField label="Classes">
<SkillRow>
    <code>.checkbox</code>
  </SkillRow>
  </SkillField>

  <SkillField label="Accessibility">
<SkillText dense>• The native <code>&lt;input type="checkbox"&gt;</code> provides all keyboard and screen reader support.</SkillText>
  <SkillText dense>• Use <code>&lt;label&gt;</code> with <code>for</code> to associate the label text.</SkillText>
  <SkillText dense>• Use <code>&lt;fieldset&gt;</code> + <code>&lt;legend&gt;</code> for checkbox groups.</SkillText>
  <SkillText dense>• Use <code>aria-invalid="true"</code> for validation errors.</SkillText>
  <p class="text-muted-foreground m-0">• Use <code>indeterminate</code> property via JS for the indeterminate (mixed) state.</p>
  </SkillField>

  <SkillField label="Notes">
<SkillText dense>• Styled with <code>appearance: none</code> and a custom checkmark via <code>::after</code> pseudo-element.</SkillText>
  <SkillText dense>• The checkmark uses a CSS-only approach - no SVG or icon font needed.</SkillText>
  <SkillText dense>• Indeterminate state is set via JavaScript: <code>checkbox.indeterminate = true;</code>.</SkillText>
  <p class="text-muted-foreground m-0">• In <code>forced-colors: active</code>, the checkbox reverts to <code>appearance: auto</code> so Windows High Contrast Mode controls rendering.</p>
  </SkillField>
</SkillPanel>

```html example label="Default" hint="Basic checkbox with label."
<div class="checkbox-item">
  <input class="checkbox" type="checkbox" id="terms" />
  <label for="terms">Accept terms and conditions</label>
</div>
```
```html example label="With description" hint="Checkbox with helper text."
<div class="checkbox-item-block">
  <input class="checkbox" type="checkbox" id="d-notify" style="margin-top: 0.125rem" aria-describedby="d-notify-desc" />
  <div>
    <label for="d-notify" class="item-title">Enable notifications</label>
    <p class="field-description" id="d-notify-desc">You can enable or disable notifications at any time.</p>
  </div>
</div>
```

```html example label="Disabled" hint="disabled keeps the box legible (full-strength edge, muted fill, a softened primary fill when checked) and mutes the label, with the not-allowed cursor over both - no opacity fade."
<div class="flex flex-col gap-3">
  <div class="checkbox-item">
    <input class="checkbox" type="checkbox" id="d-dis" disabled />
    <label for="d-dis">Disabled</label>
  </div>
  <div class="checkbox-item">
    <input class="checkbox" type="checkbox" id="d-dis-checked" disabled checked />
    <label for="d-dis-checked">Disabled checked</label>
  </div>
</div>
```

```html example label="Checked by default"
<div class="checkbox-item">
  <input class="checkbox" type="checkbox" id="option" checked />
  <label for="option">Pre-checked option</label>
</div>
```
```html example label="Indeterminate" hint="The mixed state has no HTML attribute - set it with checkbox.indeterminate = true. Pick a state below; a click on the checkbox clears the mixed state (the browser does that) and the picker follows. For what the state is for - a box that summarises a group - see Select all below."
<div class="flex flex-col gap-4">
  <div class="checkbox-item">
    <input class="checkbox" type="checkbox" id="select-all" />
    <label for="select-all">Select all</label>
  </div>
  <fieldset class="radio-group" data-orientation="horizontal" id="select-all-state">
    <legend class="label">State</legend>
    <div class="radio-item">
      <input class="radio" type="radio" name="select-all-state" id="sa-unchecked" value="unchecked" />
      <label for="sa-unchecked">Unchecked</label>
    </div>
    <div class="radio-item">
      <input class="radio" type="radio" name="select-all-state" id="sa-checked" value="checked" />
      <label for="sa-checked">Checked</label>
    </div>
    <div class="radio-item">
      <input class="radio" type="radio" name="select-all-state" id="sa-indeterminate" value="indeterminate" checked />
      <label for="sa-indeterminate">Indeterminate</label>
    </div>
  </fieldset>
</div>
<script>
  const box = document.getElementById('select-all');
  const picker = document.getElementById('select-all-state');
  const apply = (state) => {
    box.indeterminate = state === 'indeterminate';
    box.checked = state === 'checked';
  };
  const current = () => (box.indeterminate ? 'indeterminate' : box.checked ? 'checked' : 'unchecked');
  apply(picker.querySelector(':checked').value);
  picker.addEventListener('change', (e) => apply(e.target.value));
  box.addEventListener('change', () => {
    document.getElementById('sa-' + current()).checked = true;
  });
</script>
```
```html example label="Invalid" hint="Shows destructive border via aria-invalid='true'."
<div class="checkbox-item">
  <input class="checkbox" type="checkbox" id="d-invalid" aria-invalid="true" />
  <label for="d-invalid">Accept terms and conditions</label>
</div>
```

```html example label="Select all" hint="What the mixed state is for: a parent box above a group. Tick some items and it shows the dash, tick them all and it fills, untick everything and it empties. Clicking it while mixed or empty ticks every item; clicking it while full clears them. The parent names its children with aria-controls, which is also all the small script needs to wire them up."
<fieldset style="border: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.5rem">
  <legend style="font-size: 0.875rem; font-weight: 500; margin-bottom: 0.25rem"
    >Items to display on the desktop:</legend
  >
  <div class="checkbox-item">
    <input class="checkbox" type="checkbox" id="d-all" aria-controls="d-all-1 d-all-2 d-all-3 d-all-4" />
    <label for="d-all">Select all</label>
  </div>
  <div style="display: flex; flex-direction: column; gap: 0.5rem; padding-inline-start: 1.625rem">
    <div class="checkbox-item">
      <input class="checkbox" type="checkbox" id="d-all-1" checked />
      <label for="d-all-1">Hard disks</label>
    </div>
    <div class="checkbox-item">
      <input class="checkbox" type="checkbox" id="d-all-2" checked />
      <label for="d-all-2">External disks</label>
    </div>
    <div class="checkbox-item">
      <input class="checkbox" type="checkbox" id="d-all-3" />
      <label for="d-all-3">CDs, DVDs, and iPods</label>
    </div>
    <div class="checkbox-item">
      <input class="checkbox" type="checkbox" id="d-all-4" />
      <label for="d-all-4">Connected servers</label>
    </div>
  </div>
</fieldset>
<script>
  // a parent box = a .checkbox whose aria-controls lists its children's ids
  document.querySelectorAll('.checkbox[aria-controls]').forEach((parent) => {
    const children = parent
      .getAttribute('aria-controls')
      .split(' ')
      .map((id) => document.getElementById(id));
    // some ticked = mixed, all = checked, none = empty
    const sync = () => {
      const ticked = children.filter((c) => c.checked).length;
      parent.checked = ticked === children.length;
      parent.indeterminate = ticked > 0 && ticked < children.length;
    };
    // clicking the parent (mixed or empty -> checked, checked -> empty) sets every child
    parent.addEventListener('change', () => {
      children.forEach((c) => {
        c.checked = parent.checked;
      });
      sync();
    });
    children.forEach((c) => c.addEventListener('change', sync));
    sync();
  });
</script>
```

```html example label="Group" hint="Multiple checkboxes in a group using &lt;fieldset&gt; and &lt;legend&gt;."
<fieldset style="border: none; padding: 0; margin: 0" aria-describedby="d-grp-desc">
  <legend style="font-size: 0.875rem; font-weight: 500; margin-bottom: 0.25rem"
    >Select items to display on the desktop:</legend
  >
  <p class="field-description" id="d-grp-desc" style="margin-bottom: 0.75rem">Choose which items are visible.</p>
  <div style="display: flex; flex-direction: column; gap: 0.5rem">
    <div class="checkbox-item">
      <input class="checkbox" type="checkbox" id="d-grp-1" checked />
      <label for="d-grp-1">Hard disks</label>
    </div>
    <div class="checkbox-item">
      <input class="checkbox" type="checkbox" id="d-grp-2" checked />
      <label for="d-grp-2">External disks</label>
    </div>
    <div class="checkbox-item">
      <input class="checkbox" type="checkbox" id="d-grp-3" />
      <label for="d-grp-3">CDs, DVDs, and iPods</label>
    </div>
    <div class="checkbox-item">
      <input class="checkbox" type="checkbox" id="d-grp-4" />
      <label for="d-grp-4">Connected servers</label>
    </div>
  </div>
</fieldset>
```

<StatesSection>
  <p class="text-sm text-muted-foreground">Machine contract - verified against <a class="link" href="../schemas/checkbox.schema.json"><code>checkbox.schema.json</code></a> by <code>bun run verify</code>:</p>

```states
| State | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `checked` | `boolean` | `true`, `false` | `false` | Native checked state (panel checkbox ⇄ real click, both directions). |
| `disabled` | `boolean` | `true`, `false` | `false` | Interaction off (native `disabled` attribute). |
| `required` | `boolean` | `true`, `false` | `false` | Post-validation marks the field invalid (`:user-invalid`). |
| `invalid` | `boolean` | `true`, `false` | `false` | Manual invalid flag (`aria-invalid="true"`). |
| `indeterminate` | `boolean` | `true`, `false` | `false` | Mixed state (the `indeterminate` property - no HTML attribute; a click clears it). |
```

</StatesSection>

<SourceFiles component="checkbox" section="css" />

</DocPage>
