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
Variants
A row on .nds-checkbox-input (every <input>) or .nds-checkbox changes every option. Checked goes on one option: the builder uses #terms or #check-1, but on a page it goes on the option you mean. The indeterminate state is set only from JavaScript: see the API. .nds-checkbox:only-of-type is the single checkbox, not one inside a group.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Single (default) | — | — | One checkbox, for a yes-or-no answer such as agreeing to the terms |
| Structure | Group | canon #check-group |
— | Several options under one legend. The user can pick any number |
| Size | SM (default) | — | — | 16px tile. Dense forms and tables |
| Size | MD | .nds-md |
.nds-checkbox |
20px tile. Most forms |
| Size | LG | .nds-lg |
.nds-checkbox |
24px tile. Touch-first screens |
| Color | Neutral (demo: + checked) | .nds-neutral |
.nds-checkbox-input |
Put it on each <input>. The checked tile uses the neutral color, not the primary color. Use it when the choice is not a brand action. The color shows only when an option is checked, so the demo turns Checked on too |
| Checked | Checked (id: checked) | [checked] |
#terms |
The option is on when the page loads |
| Checked | Checked (id: checked) | [checked] |
#check-1 |
The same, in a group |
| Validation | None (default) | — | — | No rule |
| Validation | Required (hint: Press Validate with nothing checked) | [data-required] |
.nds-checkbox:only-of-type |
A single checkbox must be checked before the form submits. The forms script adds required to the input |
| Validation | Required (hint: Press Validate with nothing checked) | [data-required] |
.nds-checkbox-group |
A group needs at least one option checked |
| Validation | At least 2 | [data-min-checked="2"] |
.nds-checkbox-group |
At least 2 options must be checked. Say the rule in the legend |
| Validation | At most 3 (hint: Check all four options, then press Validate) | [data-max-checked="3"] |
.nds-checkbox-group |
No more than 3 options may be checked. Say the rule in the legend |
| Validation | At least 2 + At most 3 | [data-min-checked="2"] |
.nds-checkbox-group |
Both together: between 2 and 3 options must be checked |
| Validation | At least 2 + At most 3 | [data-max-checked="3"] |
.nds-checkbox-group |
The same |
| Disabled | Disabled | [disabled] |
.nds-checkbox-input |
The user cannot change these options now. Put it on one input to disable one option, or use data-state~="disabled" on the group. To keep one option disabled when the group is enabled again, put data-state="disabled" on that option’s container |
| Readonly | Readonly | [data-state~="readonly"] |
.nds-checkbox |
The options show their value but cannot change, by pointer or by keyboard. The tile shows a border only, and a checked tile keeps its mark |
| Layout | Row | .nds-horizontal |
.nds-checkbox-group |
The options sit side by side and wrap |
| Field states | Label, info, feedback | — | — | Shared by every form field. See Forms |
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-requiredon 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 putnds-gridon 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-checkedanddata-max-checkedon the group, not on its inputs. For a single checkbox, putdata-requiredon 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-neutralwhen 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.
| Token | Preview | Value |
|---|---|---|
--checkbox-primary-checked | var(--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.
| Token | Preview | Value |
|---|---|---|
--background-defaultRead by background-color, background | var(--colors-base-white)Dark mode var(--colors-neutral-800) | |
--background-primary | var(--colors-primary-600) | |
--background-disabledRead by --checkbox-tile-border, --checkbox-tile-bg | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) | |
--focus-outlineRead by outline | var(--colors-base-black)Dark mode var(--colors-base-white) | |
--icon-oncolorRead by --checkbox-glyph-bg | var(--colors-base-white) | |
--controls-neutral-hoveredRead by --checkbox-tile-border | var(--colors-neutral-600)Dark mode var(--colors-neutral-400) | |
--controls-neutral-pressedRead by --checkbox-tile-border | var(--colors-neutral-600)Dark mode var(--colors-neutral-400) | |
--controls-borderRead by box-shadow | var(--colors-neutral-500)Dark mode var(--colors-neutral-400) | |
--controls-icon-disabledRead 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 |
The full API is in the banner of _js/nds-checkbox.js.