checkbox
About Checkbox
The ability to style checkboxes with CSS varies across browsers. To ensure that checkboxes look the same everywhere, we use a custom DOM. Pay close attention to the markup, because all elements must exist for the styles to work.
Accessibility
Groups of checkboxes should be marked up using the fieldset and legend element. This helps someone using assistive technology to understand the question they're answering with the group of checkboxes. The fieldset is placed around the whole group and the legend contains the question.
Custom checkboxes are created by applying the .slds-checkbox
class to a <label>
element. To remain accessible to all user agents, place <input>
with type="checkbox"
inside the <label>
element. The <input>
is then visually hidden, and the styling is placed on a span with the .slds-checkbox_faux
class. The styling of the span changes based on whether the checkbox is selected or focused by using a pseudo-element. A second span with .slds-form-element__label
contains the label text.
When a single checkbox is required, <div class="slds-checkbox">
should get <abbr class="required" title="required">*</abbr>
added to the DOM, directly before the <input type="checkbox" />
for visual indication that the checkbox is required.
When a checkbox group is required, the <fieldset>
should receive the class .slds-is-required
. The <legend>
should then get <abbr class="required" title="required">*</abbr>
added to the DOM for visual indication that the checkbox group is required.
As SLDS checkboxes rely on the :checked
pseudo selector, and the indeterminate state is only accessible via JavaScript, the use of a CSS class on the input will be necessary to implement this in SLDS. Use JavaScript to add the class when the indeterminate property is set to true on the input.
Mobile
On mobile devices, such as phones and other devices that have touch as the primary method of interaction, checkboxes will have an increased size to accommodate tapping with a finger instead of the more precise mouse cursor, as well as having larger label text.
Below is a live example of what to expect in that context. No code changes are needed in the Salesforce platform context as this change occurs automatically in the appropriate context. For those users not on the Salesforce platform, these modifications will occur automatically when the secondary touch stylesheet is loaded and the device has touch as the primary method of interaction.
Base
The base variant of a checkbox is a single boolean value. The base checkbox is consumed by both a grouped checkbox and a checkbox within a form element.
The form element variant accommodates a top level label and additional functionality such as like help text and read only mode. Help text is not supported on a single checkbox.
<div class="slds-form-element">
<div class="slds-form-element__control">
<div class="slds-checkbox">
States
Required
<div class="slds-form-element">
<div class="slds-form-element__control">
<div class="slds-checkbox">
Error
<div class="slds-form-element slds-has-error">
<div class="slds-form-element__control">
<div class="slds-checkbox">
Disabled
<div class="slds-form-element">
<div class="slds-form-element__control">
<div class="slds-checkbox">
Checked
<div class="slds-form-element">
<div class="slds-form-element__control">
<div class="slds-checkbox">
Examples
Group
<fieldset class="slds-form-element">
<legend class="slds-form-element__legend slds-form-element__label">Checkbox Group Label</legend>
<div class="slds-form-element__control">
Required
<fieldset class="slds-form-element">
<legend class="slds-form-element__legend slds-form-element__label">
<abbr class="slds-required" title="required">*</abbr> Checkbox Group Label</legend>
Error
<fieldset class="slds-form-element slds-has-error">
<legend class="slds-form-element__legend slds-form-element__label">
<abbr class="slds-required" title="required">*</abbr> Checkbox Group Label</legend>
Disabled
<fieldset class="slds-form-element">
<legend class="slds-form-element__legend slds-form-element__label">Checkbox Group Label</legend>
<div class="slds-form-element__control">
RTL (right to left direction)
<div dir="rtl">
<div class="slds-form-element">
<div class="slds-form-element__control">
Form Element
<div class="slds-form-element">
<label class="slds-form-element__label" for="checkbox-unique-id-62">Form Element Label</label>
<div class="slds-form-element__control">
States
Checked
<div class="slds-form-element">
<label class="slds-form-element__label" for="checkbox-unique-id-65">Form Element Label</label>
<div class="slds-form-element__control">
Disabled
<div class="slds-form-element">
<label class="slds-form-element__label" for="checkbox-unique-id-68">Form Element Label</label>
<div class="slds-form-element__control">
Checked and Disabled
<div class="slds-form-element">
<label class="slds-form-element__label" for="checkbox-unique-id-71">Form Element Label</label>
<div class="slds-form-element__control">
Error
<div class="slds-form-element slds-has-error">
<label class="slds-form-element__label" for="checkbox-unique-id-74">
<abbr class="slds-required" title="required">* </abbr>Form Element Label</label>
Required
<div class="slds-form-element">
<label class="slds-form-element__label" for="checkbox-unique-id-77">
<abbr class="slds-required" title="required">* </abbr>Form Element Label</label>
View mode
View mode is the read only state of a checkbox form element.
Unchecked
<div class="slds-form-element slds-form-element_readonly">
<span class="slds-form-element__label">Form Element Label</span>
<div class="slds-form-element__control">
Checked
<div class="slds-form-element slds-form-element_readonly">
<span class="slds-form-element__label">Form Element Label</span>
<div class="slds-form-element__control">
Examples
With help text
<div class="slds-form-element">
<label class="slds-form-element__label" for="checkbox-unique-id-80">Form Element Label</label>
<div class="slds-form-element__icon">
Required with help text
<div class="slds-form-element">
<label class="slds-form-element__label" for="checkbox-unique-id-83">
<abbr class="slds-required" title="required">* </abbr>Form Element Label</label>
Required with help text with tooltip
<div style="padding-top:3rem">
<div class="slds-form-element">
<label class="slds-form-element__label" for="checkbox-unique-id-86">