---
title: Checkbox for Astro
description: "Native Astro checkboxes for independent choices and form values."
seo:
    title: Checkbox for Astro | Shadcn & Tailwind CSS
related:
    - /components/radio-group
    - /components/switch
sidebar:
    label: "Checkbox"
---

```astro
---
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"
---

<div class="flex flex-col gap-6">
  <div class="flex items-center gap-3">
    <Checkbox id="terms" />
    <Label for="terms">Include review notes</Label>
  </div>
  <div class="flex items-start gap-3">
    <Checkbox id="terms-2" checked />
    <div class="grid gap-2">
      <Label for="terms-2">Include review notes</Label>
      <p class="text-muted-foreground text-sm">
        Attach the current review notes when you share this project.
      </p>
    </div>
  </div>
  <div class="flex items-start gap-3">
    <Checkbox id="toggle" disabled />
    <Label for="toggle">Send task reminders</Label>
  </div>
  <Label class="hover:bg-accent/50 has-[[aria-checked=true]]:border-primary has-[[aria-checked=true]]:bg-primary/5 flex items-start gap-3 rounded-lg border p-3">
    <Checkbox id="toggle-2" checked />
    <div class="grid gap-1.5 font-normal">
      <p class="text-sm leading-none font-medium">Send task reminders</p>
      <p class="text-muted-foreground text-sm">
        Receive a reminder when a task needs your review.
      </p>
    </div>
  </Label>
</div>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/checkbox
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/checkbox/checkbox.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = Omit<HTMLAttributes<"input">, "type">

const { class: className, ...props } = Astro.props
---

<input
  type="checkbox"
  role="checkbox"
  data-slot="checkbox"
  class={cn("relative inline-flex size-4.5 sm:size-4 shrink-0 appearance-none items-center justify-center rounded-[.25rem] border border-input bg-background not-dark:bg-clip-padding shadow-xs/5 outline-none ring-ring transition-shadow focus-visible:ring-2 focus-visible:ring-offset-1 focus-visible:ring-offset-background aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:ring-destructive/48 disabled:cursor-not-allowed disabled:opacity-64 checked:border-primary checked:bg-primary checked:shadow-none indeterminate:shadow-none dark:not-checked:bg-input/32 dark:aria-invalid:ring-destructive/24 not-disabled:not-checked:not-aria-invalid:inset-shadow-[0_1px_--theme(--color-black/4%)] dark:not-disabled:not-checked:not-aria-invalid:inset-shadow-[0_-1px_--theme(--color-white/6%)] peer", className)}
  {...props}
/>

<style>
  /*
    The mark is a masked ::before on the input.
    Keep opacity and color in this sheet. These rules are unlayered, so they
    win over Tailwind utilities. A class such as checked:before:opacity-100
    cannot override opacity: 0, and its before: variant also replaces content.
  */
  [data-slot="checkbox"] {
    color: inherit;
  }

  [data-slot="checkbox"]::before {
    content: "";
    position: absolute;
    inset: 0;
    margin: auto;
    width: 14px;
    height: 14px;
    opacity: 0;
    background-color: currentColor;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 14px 14px;
    -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.5 8.5l3 3 6-6'/%3E%3C/svg%3E");
    -webkit-mask-repeat: no-repeat;
    -webkit-mask-position: center;
    -webkit-mask-size: 14px 14px;
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.5 8.5l3 3 6-6'/%3E%3C/svg%3E");
    mask-repeat: no-repeat;
    mask-position: center;
    mask-size: 14px 14px;
  }

  [data-slot="checkbox"]:checked {
    color: var(--color-primary-foreground);
  }

  [data-slot="checkbox"]:checked::before {
    opacity: 1;
  }

  [data-slot="checkbox"]:indeterminate {
    color: var(--color-foreground);
  }

  [data-slot="checkbox"]:indeterminate::before {
    opacity: 1;
    -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none'%3E%3Cpath d='M3.5 8h9' stroke='black' stroke-width='2.5' stroke-linecap='round'/%3E%3C/svg%3E");
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none'%3E%3Cpath d='M3.5 8h9' stroke='black' stroke-width='2.5' stroke-linecap='round'/%3E%3C/svg%3E");
  }
</style>
```

#### src/components/ui/checkbox/index.ts

```ts
export { default as Checkbox } from "./checkbox.astro"
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import { Checkbox } from "@/components/ui/checkbox"
```

```astro
<Checkbox />
```

## Notes

- Pair `Checkbox` with `Label` for simple cases, or use
  [Field](/components/field) when you need descriptions, validation, or
  grouped controls.
- Use the native `disabled` attribute to prevent interaction and
  `aria-invalid` when the control should display an error state.
- For multi-line rows, wrap the label and supporting text together so the
  control stays easy to scan.

## API Reference

Read the [installed source](/r/checkbox.json) for the complete prop types.

## When to use Checkbox

Pair each Checkbox with a Label using matching `id` and `for` values. Give submitted controls a name and value. Use separate checkboxes when several choices can be selected together.

## Keyboard and behavior

The rendered input uses native form submission, focus, Space activation, and disabled behavior. Set the DOM `indeterminate` property from your script for a mixed selection; an HTML attribute alone cannot set that property. A parent “select all” checkbox does not manage child values automatically.

## Theme and layout

Checked, invalid, and disabled styles use the shared tokens. The check mark is a CSS mask on the input. Keep a text label visible and use FieldError for an explanation of invalid input.
