Skip to main content

Checkbox

stable

Overview

Checkbox is the control for a choice the user can turn on or off, alone or alongside other choices. A single checkbox can be required before a form submits, such as accepting terms. When that checkbox is invalid, error text says what to do next.

Usage

When to use

  • Letting a user select any number of options, including none
  • A single confirmation the form needs before it is submitted, such as accepting terms
  • Marking one field optional when the rest of the form is required

When not to use

  • Choosing one option from a mutually exclusive set — use Radio
  • Turning a setting on or off immediately — use Switch
  • Grouping several related checkboxes under one label — use Checkbox group

When a single checkbox is required

Require one checkbox when the user has to confirm it before the form is submitted. Accepting terms is the usual case. Set required and show-indicator together. The asterisk comes from the indicator, separate from the label string.

Use the required asterisk when most fields are optional and this one has to be completed. Use the optional indicator when most fields are required and this one is not. Leave preference and marketing checkboxes optional.

How to write error text

Say what is wrong and the next step. For a terms checkbox, write "Agree to the terms to continue." A message that only says "Required" leaves the next step out.

Alternatives

Checkbox group

Use when several related checkboxes share one label, indicator, and error.

Radio

Use when the user can select only one option in the set.

Switch

Use when the control takes effect immediately, with no separate submit.

Anatomy

The checkbox is made up of six elements.

Anatomy diagram of a checkbox. Callout 1 marks the checkbox input, 2 the helper text, 3 the status mark, 4 the label, 5 the indicator after the label, and 6 the error text.
  1. Checkbox input

    The control the user selects.

  2. Helper text

    Optional guidance under the label. Shown only when the label is set.

  3. Status mark

    The check or indeterminate mark inside the control.

  4. Label

    Names the checkbox. The first line stays vertically centered with the control when the label wraps.

  5. Indicator

    "(optional)" or a required asterisk, placed after the label with a leading space. Shown when `show-indicator` is set.

  6. Error text

    What is wrong, under the helper text. Shown when `invalid` is set. Wraps when the line is longer than the checkbox.

Variants

Label

Describes the purpose of the checkbox. The label can be omitted when an adjacent label describes the field and is referenced to it.

Helper text

Extra context for using the checkbox. Helper text is shown only when the label is set, and it wraps onto the next line.

Required

Indicates the checkbox has to be checked for the form to submit. required is a boolean and defaults to false.

The indicator is part of the component. When required is true and show-indicator is on, the label shows an asterisk. When required is false and the indicator is on, the label shows "(optional)".

Checkbox labeled Label with a required asterisk.

Pending code merge.

Invalid

Sets aria-invalid and shows the error text. invalid is a boolean and defaults to false.

Use it when a required checkbox is still clear, or when the value was not accepted. Error text can name the next step or give a more specific explanation. It sits under the label and any helper text.

The checkbox does not raise its own alert. Add one when the message has to be announced as soon as it appears.

Checkbox labeled Label with a required asterisk and error text underneath.

Pending code merge.

States

A checkbox can be unselected, selected, or indeterminate. Each of those has enabled, focus, hover, active, and disabled. Focus, hover, and active show up when you interact with the control.

Unselected

No selection.

Selected

Checked. Another interaction clears it.

Indeterminate

The next interaction selects the checkbox. The one after that clears it.

Disabled

Interaction is prevented. Some assistive technologies skip a disabled checkbox.

Configuration

Theming

Tune via style hook

HookDescriptionDefault
Container radius (1)
HookDescriptionDefault
--jh-checkbox-input-border-radiusThe checkbox border radius.--jh-border-radius-50
Focus ring (1)
HookDescriptionDefault
--jh-checkbox-color-focusThe checkbox outline when it receives keyboard focus.--jh-border-focus-color
Disabled opacity (1)
HookDescriptionDefault
--jh-checkbox-opacity-disabledThe checkbox opacity when disabled.--jh-opacity-disabled
4 Label text color (1)
HookDescriptionDefault
--jh-checkbox-label-color-textThe label text color.--jh-color-content-primary-enabled
4 Text color (1)
HookDescriptionDefault
--jh-checkbox-helper-color-textThe helper text color.--jh-color-content-secondary-enabled
Other (40)
HookDescriptionDefault
--jh-checkbox-input-color-background-unselected-enabledThe checkbox background-color when unselected.--jh-color-container-primary-enabled
--jh-checkbox-input-color-border-unselected-enabledThe checkbox border-color when unselected.--jh-border-control-color
--jh-checkbox-input-color-background-unselected-focusThe checkbox background-color when unselected and focused.--jh-color-container-primary-enabled
--jh-checkbox-input-color-border-unselected-focusThe checkbox border-color when unselected and focused.--jh-color-content-brand-hover
--jh-checkbox-input-color-background-unselected-hoverThe checkbox background-color when unselected and hovered.--jh-color-container-primary-enabled
--jh-checkbox-input-color-border-unselected-hoverThe checkbox border-color when unselected and hovered.--jh-color-content-brand-hover
--jh-checkbox-input-color-background-unselected-activeThe checkbox background-color when unselected and active.--jh-color-container-primary-enabled
--jh-checkbox-input-color-border-unselected-activeThe checkbox border-color when unselected and active.--jh-color-content-brand-active
--jh-checkbox-input-color-background-unselected-disabledThe checkbox background-color when unselected and disabled.--jh-color-container-primary-enabled
--jh-checkbox-input-color-border-unselected-disabledThe checkbox border-color when unselected and disabled.--jh-border-control-color
--jh-checkbox-input-color-background-selected-enabledThe checkbox background-color when selected.--jh-color-content-brand-enabled
--jh-checkbox-input-color-border-selected-enabledThe checkbox border-color when selected.--jh-color-content-brand-enabled
--jh-checkbox-status-color-border-selected-enabledThe status mark color when selected.--jh-color-content-on-brand-enabled
--jh-checkbox-input-color-background-selected-focusThe checkbox background-color when selected and focused.--jh-color-content-brand-hover
--jh-checkbox-input-color-border-selected-focusThe checkbox border-color when selected and focused.--jh-color-content-brand-hover
--jh-checkbox-status-color-border-selected-focusThe status mark color when selected and focused.--jh-color-content-on-brand-hover
--jh-checkbox-input-color-background-selected-hoverThe checkbox background-color when selected and hovered.--jh-color-content-brand-hover
--jh-checkbox-input-color-border-selected-hoverThe checkbox border-color when selected and hovered.--jh-color-content-brand-hover
--jh-checkbox-status-color-border-selected-hoverThe status mark color when selected and hovered.--jh-color-content-on-brand-hover
--jh-checkbox-input-color-background-selected-activeThe checkbox background-color when selected and active.--jh-color-content-brand-active
--jh-checkbox-input-color-border-selected-activeThe checkbox border-color when selected and active.--jh-color-content-brand-active
--jh-checkbox-status-color-border-selected-activeThe status mark color when selected and active.--jh-color-content-on-brand-active
--jh-checkbox-input-color-background-selected-disabledThe checkbox background-color when selected and disabled.--jh-color-content-brand-enabled
--jh-checkbox-input-color-border-selected-disabledThe checkbox border-color when selected and disabled.--jh-color-content-brand-enabled
--jh-checkbox-status-color-border-selected-disabledThe status mark color when selected and disabled.--jh-color-content-on-brand-enabled
--jh-checkbox-input-color-background-indeterminate-enabledThe checkbox background-color when indeterminate.--jh-color-content-brand-enabled
--jh-checkbox-input-color-border-indeterminate-enabledThe border-color when indeterminate checkbox.--jh-color-content-brand-enabled
--jh-checkbox-status-color-border-indeterminate-enabledThe status mark color when indeterminate.--jh-color-content-on-brand-enabled
--jh-checkbox-input-color-background-indeterminate-focusThe checkbox background-color when indeterminate and focused.--jh-color-content-brand-hover
--jh-checkbox-input-color-border-indeterminate-focusThe checkbox border-color when indeterminate and focused.--jh-color-content-brand-hover
--jh-checkbox-status-color-border-indeterminate-focusThe checkbox color when indeterminate status mark when focused.--jh-color-content-on-brand-hover
--jh-checkbox-input-color-background-indeterminate-hoverThe checkbox background-color when indeterminate and hovered.--jh-color-content-brand-hover
--jh-checkbox-input-color-border-indeterminate-hoverThe checkbox border-color when indeterminate and hovered.--jh-color-content-brand-hover
--jh-checkbox-status-color-border-indeterminate-hoverThe checkbox color when indeterminate status mark when hovered.--jh-color-content-on-brand-hover
--jh-checkbox-input-color-background-indeterminate-activeThe checkbox background-color when indeterminate and active.--jh-color-content-brand-active
--jh-checkbox-input-color-border-indeterminate-activeThe checkbox border-color when indeterminate and active.--jh-color-content-brand-active
--jh-checkbox-status-color-border-indeterminate-activeThe checkbox color when indeterminate status mark when active.--jh-color-content-on-brand-active
--jh-checkbox-input-color-background-indeterminate-disabledThe checkbox background-color when indeterminate and disabled.--jh-color-content-brand-enabled
--jh-checkbox-input-color-border-indeterminate-disabledThe checkbox border-color when indeterminate and disabled.--jh-color-content-brand-enabled
--jh-checkbox-status-color-border-indeterminate-disabledThe checkbox color when indeterminate status mark when disabled.--jh-color-content-on-brand-enabled

Fixed

  • Checkbox input — not customizable
  • Helper text — not customizable
  • Status mark — not customizable
  • Indicator — not customizable
  • Error text — not customizable

Examples

Required

A terms checkbox has to be checked before the form is submitted. The asterisk comes from the indicator.

Checkbox labeled Label with a required asterisk.
Pending code merge

Invalid, with error text

The error names the next step. The image is the spec placeholder, with the sample string "Error text".

Checkbox labeled Label with a required asterisk and red error text reading Error text.
Pending code merge

Accessibility

The label is the accessible name. When there is no visible label, set accessible-label.

invalid sets aria-invalid and shows the error text beside the control. That text is not announced on its own. Add an alert when the message has to be heard as soon as it appears.

A disabled checkbox may be skipped by assistive technologies.