Skip to main content
Login

Sign in to access your account

Checkbox - National Design System

A checkbox turns one option on or off, alone or as one of several choices

Overview

A checkbox is a native <input type="checkbox"> drawn as a square tile, inside a form field with a label and optional info text. Use one alone for a yes-or-no answer, such as agreeing to the terms. Use several in a group under a legend when the user can pick any number of options. The forms script starts every checkbox, so it needs no script of its own.

Pick another component when:

  • the user can pick only one option: Radio Button
  • the choice is one setting that takes effect at once: Switch
  • the list of options is long: Multiselect

Markup

Options
Structure
Size
Validation
More
Preview
<div class="nds-form-container nds-checkbox"> <div class="nds-form-header" data-feedback-target> <label for="terms"> <span class="nds-label">I agree to the terms and conditions</span> <span class="nds-info">You must accept the terms to continue</span> </label> </div> <div class="nds-form-control"> <input type="checkbox" id="terms" name="agreement" value="accepted" class="nds-checkbox-input"> </div> </div>

Behavior

Readonly

data-state~="readonly" on the container shows the value and stops the change, by pointer or by keyboard. The tile shows a border only, and a checked tile keeps its mark, so the answer stays readable.

Group Rules

data-required, data-min-checked and data-max-checked on the group set how many options must be checked. Once the group shows an error, each change checks the rules again.

Indeterminate

NDS.Checkbox.setIndeterminate() shows a dash, for a “select all” checkbox. A click by the user clears it. Each change fires nds:checkbox:indeterminate.

Built-in Features

  • Form Integration

    The forms script starts every checkbox. A container follows the disabled state of its input, in both directions. data-required on a single checkbox makes its input required; a group checks itself.

  • Responsive Layout

    On a phone, the gaps between a checkbox, its label and the next option are smaller.

Best Practices

  • Put a group in a <fieldset class="nds-form-group nds-checkbox-group"> with a <legend> that names the choice. Screen readers read the legend with each option.
  • For many short options, such as times of day, put them in a <div class="nds-grid" style="--min-width: 6.5rem;"> inside the group, after the <legend>, so they line up in as many columns as fit. Do not put nds-grid on the <fieldset> itself: it stretches the legend.
  • Keep a group to 7 options or fewer. For a longer list, use a Multiselect.
  • When a group has a rule, say it in the legend, such as “Select 2 to 3 interests”. The error message appears only after a submit.
  • Put data-required, data-min-checked and data-max-checked on the group, not on its inputs. For a single checkbox, put data-required on its container.
  • Write the label of a single checkbox as the statement the user agrees to, such as “I agree to the terms and conditions”.
  • Use the indeterminate state only for a checkbox that controls other checkboxes, such as “select all”.
  • Use nds-neutral when the choice is not a brand action.
  • To lock a whole group, put data-state="readonly" on every option’s container.
  • For the label, info text and feedback, see Forms. They work the same on every field.

API

Data Attributes

Attribute Element Effect
data-required .nds-checkbox of a single checkbox, or .nds-checkbox-group The checkbox must be checked, or the group needs one option checked, before the form submits. It adds the required mark to the label or the legend
data-min-checked .nds-checkbox-group The fewest options that must be checked. The message reads “Please select at least 2 options”
data-max-checked .nds-checkbox-group The most options that may be checked. The message reads “Please select no more than 3 options”
data-error-message .nds-checkbox-group Replaces the message when nothing is checked. The data-min-checked and data-max-checked messages stay
data-state~="disabled" .nds-checkbox, .nds-checkbox-group Disables the option, or every option in the group. The group’s title takes the disabled color
data-state~="readonly" .nds-checkbox The option cannot change. The tile shows a border only
data-state~="indeterminate" .nds-checkbox Set by NDS.Checkbox.setIndeterminate(). Do not set it yourself
data-feedback-target .nds-form-header, or an element inside the group Receives the feedback message. Without one, the message goes at the end of the field or the group

CSS Custom Properties

Set these on .nds-checkbox. The size classes set the first one.

Property Default Controls
--nds-input-size 16px Tile size and label line height. nds-md and nds-lg set 20px and 24px
--checkbox-tile-bg var(--background-default) Tile background
--checkbox-tile-border var(--controls-border) Tile outline color
--checkbox-glyph-bg var(--background-default) Color of the check mark or the dash

Tokens

Set a token at :root, or on a wrapper to reach every checkbox inside it. Give a token with a Dark mode value a dark override too: see Tokens.

Source: the checkbox group in _sass/tokens/_components.scss.

TokenPreviewValue
--checkbox-primary-checkedvar(--controls-primary-checked)
--checkbox-primary-hovered
var(--controls-primary-hovered)
Dark mode var(--colors-primary-700)
--checkbox-primary-pressed
var(--controls-primary-pressed)
Dark mode var(--colors-primary-800)
--checkbox-neutral-checked
var(--controls-neutral-checked)
Dark mode var(--colors-neutral-600)
--checkbox-neutral-hovered
var(--controls-neutral-hovered)
Dark mode var(--colors-neutral-400)
--checkbox-neutral-pressed
var(--controls-neutral-pressed)
Dark mode var(--colors-neutral-400)

It also reads these shared tokens. A change to one also changes other components.

Source: the var() reads in _sass/components/_checkbox.scss.

TokenPreviewValue
--background-default
Read by background-color, background

var(--colors-base-white)
Dark mode var(--colors-neutral-800)
--background-primaryvar(--colors-primary-600)
--background-disabled
Read by --checkbox-tile-border, --checkbox-tile-bg

var(--colors-neutral-200)
Dark mode var(--colors-neutral-700)
--focus-outline
Read by outline

var(--colors-base-black)
Dark mode var(--colors-base-white)
--icon-oncolor
Read by --checkbox-glyph-bg
var(--colors-base-white)
--controls-neutral-hovered
Read by --checkbox-tile-border

var(--colors-neutral-600)
Dark mode var(--colors-neutral-400)
--controls-neutral-pressed
Read by --checkbox-tile-border

var(--colors-neutral-600)
Dark mode var(--colors-neutral-400)
--controls-border
Read by box-shadow

var(--colors-neutral-500)
Dark mode var(--colors-neutral-400)
--controls-icon-disabled
Read by --checkbox-glyph-bg

var(--colors-base-white)
Dark mode var(--colors-neutral-400)

JavaScript

Method Effect
NDS.Checkbox.setIndeterminate(checkbox, value) Shows or clears the dash, and fires nds:checkbox:indeterminate
NDS.Checkbox.getValue(el) One box. Returns true when it is checked and false when it is not, or '' when the box is not found. el is the .nds-checkbox field, its input, its id or a selector
NDS.Checkbox.setValue(el, value) One box. Checks or unchecks it as a user does, and returns true. It returns false, and changes nothing, when value is not true or false
NDS.Checkbox.getValues(el) A group. Returns the value of each checked box, or [] when the group is not found. el is the .nds-form-group, or an element in it
NDS.Checkbox.setValues(el, values) A group. Checks exactly the boxes with the values in the array values, and returns true. It returns false, and changes nothing, when a value has no box
NDS.Checkbox.clear(el) Unchecks one box, when el is a field or an input, or every box of the group
NDS.Checkbox.validate(group, options) Checks the group and returns { valid, checked, min, max, message }. Pass { showMessage: false } to check without a message. A group that shows an error is checked again on each change
NDS.Form.refresh(input) Updates the field after you set .checked from code. It fires no event
NDS.Form.setStatus(element, status, { message }) Shows a status and a message on a field or a group, such as an error from the server
NDS.Form.clearStatus(element) Removes the feedback message
Event Fired on Detail
nds:checkbox:indeterminate the <input> { indeterminate }, true or false
// A "select all" checkbox that shows a dash when only some options are checked var all = document.querySelector('#select-all'); var options = document.querySelectorAll('.nds-checkbox-group .nds-checkbox-input'); function update() { var on = Array.prototype.filter.call(options, function (cb) { return cb.checked; }).length; all.checked = on === options.length; NDS.Checkbox.setIndeterminate(all, on > 0 && on < options.length); } options.forEach(function (cb) { cb.addEventListener('change', update); }); // Check a group before a custom save var result = NDS.Checkbox.validate(document.querySelector('.nds-checkbox-group')); // { valid: false, checked: 1, min: 2, max: 3, message: 'Please select at least 2 options' }

The full API is in the banner of _js/nds-checkbox.js.

Last Modified Date: 05/11/2026 - 12:02 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks