Checkbox Toggle
- HTML/CSS:Dev Ready
- Layout:Responsive
Sections
About Checkbox Toggle#
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.
The following JavaScript demonstrates how to set a checkbox to be indeterminate:
var checkbox = document.getElementById('checkbox-indeterminate-01');
checkbox.indeterminate = true;
Mobile#
On mobile devices, such as phones and other devices that have touch as the primary method of interaction, checkbox toggles will have an increased label text size.
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 Salesforce native mobile applications. 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#
<div class="slds-form-element">
<label class="slds-checkbox_toggle slds-grid">
<span class="slds-form-element__label slds-m-bottom_none">Toggle Label</span>
<input type="checkbox" name="checkbox-toggle-4" value="checkbox-toggle-4" aria-describedby="checkbox-toggle-4" />
States#
Checked#
<div class="slds-form-element">
<label class="slds-checkbox_toggle slds-grid">
<span class="slds-form-element__label slds-m-bottom_none">Toggle Label</span>
<input type="checkbox" name="checkbox-toggle-7" value="checkbox-toggle-7" aria-describedby="checkbox-toggle-7" checked="" />
Disabled#
<div class="slds-form-element">
<label class="slds-checkbox_toggle slds-grid">
<span class="slds-form-element__label slds-m-bottom_none">Toggle Label</span>
<input type="checkbox" name="checkbox-toggle-10" value="checkbox-toggle-10" disabled="" aria-describedby="checkbox-toggle-10" />
Right to Left#
<div dir="rtl">
<div class="slds-form-element">
<label class="slds-checkbox_toggle slds-grid">
<span class="slds-form-element__label slds-m-bottom_none">Toggle Label</span>
Styling Hooks Overview#
Use these CSS Custom Properties as hooks to customize this SLDS component with your own style. For more information, read the technical documentation.
Category | Styling Hook Name | Value Type(s) | Fallback Value |
---|---|---|---|
Box Shadow | --slds-c-checkbox-toggle-shadow | String | |
Color | --slds-c-checkbox-toggle-color-background | Color | #aeaeae |
--slds-c-checkbox-toggle-color-background-checked-focus | Color | #014486 | |
--slds-c-checkbox-toggle-color-background-hover | Color | #939393 | |
--slds-c-checkbox-toggle-color-border | Color | #aeaeae | |
--slds-c-checkbox-toggle-color-border-hover | Color | ||
--slds-c-checkbox-toggle-mark-color-foreground | Color | white | |
--slds-c-checkbox-toggle-switch-color-background | Color | white | |
--slds-c-checkbox-toggle-switch-color-background-checked | Color | white | |
Radius | --slds-c-checkbox-toggle-radius-border | Dimension | 15rem |
Overview of CSS Classes#
- Selector
- The CSS class being referred to.
- Summary
- A description of what the class does.
- Support
- Whether the class name is dev-ready (meaning it's fully vetted and tested and safe to use) or prototype (which means it's not fully vetted yet).
- Restrict
- The selector that the class name is allowed to be used on.
- Variant
- The base level pattern for a component. A variant can be extended to create another variant of that component, for example, a stateful button is a derivative of the base button.
- Modifier
- A single class that can be added to an HTML element of a component to modify its output. Typically these will be colors, sizing and positioning.
Selector | .slds-checkbox_toggle |
---|---|
Summary | Initializes checkbox toggle |
Support | dev-ready |
Restrict | label |
Variant | True |
Selector | .slds-checkbox_faux |
---|---|
Summary | Creates a custom styled checkbox |
Restrict | .slds-checkbox_toggle span |
Selector | .slds-checkbox_faux_container |
---|---|
Summary | Container for faux checkbox element |
Restrict | .slds-checkbox_toggle span |
Selector | .slds-checkbox_off |
---|---|
Summary | Container for text to show when checkbox is toggle off |
Restrict | .slds-checkbox_faux_container span |
Selector | .slds-checkbox_on |
---|---|
Summary | Container for text to show when checkbox is toggle on |
Restrict | .slds-checkbox_faux_container span |
Checkbox Toggle Release Notes
2.17.0
Added
- Added
slds
as the default namespace withsds
fallbacks for Styling Hooks. - Added reassignment of Styling Hooks for component states.
Fixed
- Fixed spacing between checkbox toggle label and input on mobile.
2.14.0
Added
- Enabled styling hooks for checkbox toggle. See checkbox toggle's styling hooks overview table for a full listing of the currently available hooks.
2.11.6
Added
- Added documentation and examples for mobile/touch context
2.10.0
Changed
- For touch devices:
- Change
font-size
of label text to$font-size-5
(1rem) - Increase
font-size
of.slds-checkbox_faux_container
to$font-size-2
(0.75rem)
- Change