Checks and radios
Create consistent cross-browser and cross-device checkboxes and radios with our completely rewritten checks component.
Approach
Browser default checkboxes and radios are replaced with the help of .form-check
, a series of classes for both input types that improves the layout and behavior of their HTML elements, that provide greater customization and cross browser consistency. Checkboxes are for selecting one or several options in a list, while radios are for selecting one option from many.
Structurally, our <input>
s and <label>
s are sibling elements as opposed to an <input>
within a <label>
. This is slightly more verbose as you must specify id
and for
attributes to relate the <input>
and <label>
. We use the sibling selector (~
) for all our <input>
states, like :checked
or :disabled
. When combined with the .form-check-label
class, we can easily style the text for each item based on the <input>
’s state.
Our checks use custom Boosted icons to indicate checked or indeterminate states.
Checks
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">
Default checkbox
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="flexCheckChecked" checked>
<label class="form-check-label" for="flexCheckChecked">
Checked checkbox
</label>
</div>
Indeterminate
Checkboxes can utilize the :indeterminate
pseudo class when manually set via JavaScript (there is no available HTML attribute for specifying it).
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="flexCheckIndeterminate">
<label class="form-check-label" for="flexCheckIndeterminate">
Indeterminate checkbox
</label>
</div>
Disabled
Add the disabled
attribute and the associated <label>
s are automatically styled to match with a lighter color to help indicate the input’s state.
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="flexCheckIndeterminateDisabled" disabled>
<label class="form-check-label" for="flexCheckIndeterminateDisabled">
Disabled indeterminate checkbox
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="flexCheckDisabled" disabled>
<label class="form-check-label" for="flexCheckDisabled">
Disabled checkbox
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="flexCheckCheckedDisabled" checked disabled>
<label class="form-check-label" for="flexCheckCheckedDisabled">
Disabled checked checkbox
</label>
</div>
Radios
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
<label class="form-check-label" for="flexRadioDefault1">
Default radio
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault2" checked>
<label class="form-check-label" for="flexRadioDefault2">
Default checked radio
</label>
</div>
Disabled
Add the disabled
attribute and the associated <label>
s are automatically styled to match with a lighter color to help indicate the input’s state.
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDisabled" id="flexRadioDisabled" disabled>
<label class="form-check-label" for="flexRadioDisabled">
Disabled radio
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDisabled" id="flexRadioCheckedDisabled" checked disabled>
<label class="form-check-label" for="flexRadioCheckedDisabled">
Disabled checked radio
</label>
</div>
Switches
A switch has the markup of a custom checkbox but uses the .form-switch
class to render a toggle switch. Consider using role="switch"
to more accurately convey the nature of the control to assistive technologies that support this role. In older assistive technologies, it will simply be announced as a regular checkbox as a fallback. Switches also support the disabled
attribute.
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Default switch checkbox input</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Checked switch checkbox input</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDisabled" disabled>
<label class="form-check-label" for="flexSwitchCheckDisabled">Disabled switch checkbox input</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckCheckedDisabled" checked disabled>
<label class="form-check-label" for="flexSwitchCheckCheckedDisabled">Disabled checked switch checkbox input</label>
</div>
Default (stacked)
By default, any number of checkboxes and radios that are immediate sibling will be vertically stacked and appropriately spaced with .form-check
.
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Default checkbox
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2" disabled>
<label class="form-check-label" for="defaultCheck2">
Disabled checkbox
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
<label class="form-check-label" for="exampleRadios1">
Default radio
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
<label class="form-check-label" for="exampleRadios2">
Second default radio
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
<label class="form-check-label" for="exampleRadios3">
Disabled radio
</label>
</div>
Inline
Group checkboxes or radios on the same horizontal row by adding .form-check-inline
to any .form-check
.
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
<label class="form-check-label" for="inlineCheckbox1">1</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
<label class="form-check-label" for="inlineCheckbox2">2</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled>
<label class="form-check-label" for="inlineCheckbox3">3 (disabled)</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
<label class="form-check-label" for="inlineRadio1">1</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
<label class="form-check-label" for="inlineRadio2">2</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled>
<label class="form-check-label" for="inlineRadio3">3 (disabled)</label>
</div>
Reverse
Put your checkboxes, radios, and switches on the opposite side with the .form-check-reverse
modifier class.
<div class="form-check form-check-reverse">
<input class="form-check-input" type="checkbox" value="" id="reverseCheck1">
<label class="form-check-label" for="reverseCheck1">
Reverse checkbox
</label>
</div>
<div class="form-check form-check-reverse">
<input class="form-check-input" type="checkbox" value="" id="reverseCheck2" disabled>
<label class="form-check-label" for="reverseCheck2">
Disabled reverse checkbox
</label>
</div>
<div class="form-check form-switch form-check-reverse">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckReverse">
<label class="form-check-label" for="flexSwitchCheckReverse">Reverse switch checkbox input</label>
</div>
Without labels
Omit the wrapping .form-check
for checkboxes and radios that have no label text. Remember to still provide some form of accessible name for assistive technologies (for instance, using aria-label
). See the forms overview accessibility section for details.
<div>
<input class="form-check-input" type="checkbox" id="checkboxNoLabel" value="" aria-label="...">
</div>
<div>
<input class="form-check-input" type="radio" name="radioNoLabel" id="radioNoLabel1" value="" aria-label="...">
</div>
Toggle buttons
Create button-like checkboxes and radio buttons by using .btn
styles rather than .form-check-label
on the <label>
elements. These toggle buttons can further be grouped in a button group if needed.
Checkbox toggle buttons
<input type="checkbox" class="btn-check" id="btn-check" autocomplete="off">
<label class="btn btn-toggle" for="btn-check">Single toggle</label>
<input type="checkbox" class="btn-check" id="btn-check-2" checked autocomplete="off">
<label class="btn btn-toggle" for="btn-check-2">Checked</label>
<input type="checkbox" class="btn-check" id="btn-check-3" autocomplete="off" disabled>
<label class="btn btn-toggle" for="btn-check-3">Disabled</label>
Radio toggle buttons
Boosted requires to group its radio toggle buttons in a button group to display properly.
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="options" id="option1" autocomplete="off" checked>
<label class="btn btn-toggle" for="option1">Checked</label>
<input type="radio" class="btn-check" name="options" id="option2" autocomplete="off">
<label class="btn btn-toggle" for="option2">Radio</label>
<input type="radio" class="btn-check" name="options" id="option3" autocomplete="off" disabled>
<label class="btn btn-toggle" for="option3">Disabled</label>
<input type="radio" class="btn-check" name="options" id="option4" autocomplete="off">
<label class="btn btn-toggle" for="option4">Radio</label>
</div>
With icon
Add .btn-icon
with an embedded icon to get consistent squared icon buttons working as toggle.
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="icons" id="option5" autocomplete="off" checked>
<label class="btn btn-icon btn-toggle" for="option5">
<svg width="1.25rem" height="1.25rem" fill="currentColor">
<use xlink:href="/docs/5.3/assets/img/boosted-sprite.svg#calendar-day"/>
</svg>
<span class="visually-hidden">Day</span>
</label>
<input type="radio" class="btn-check" name="icons" id="option6" autocomplete="off">
<label class="btn btn-icon btn-toggle" for="option6">
<svg width="1.25rem" height="1.25rem" fill="currentColor">
<use xlink:href="/docs/5.3/assets/img/boosted-sprite.svg#calendar-week"/>
</svg>
<span class="visually-hidden">Week</span>
</label>
<input type="radio" class="btn-check" name="icons" id="option7" autocomplete="off">
<label class="btn btn-icon btn-toggle" for="option7">
<svg width="1.25rem" height="1.25rem" fill="currentColor">
<use xlink:href="/docs/5.3/assets/img/boosted-sprite.svg#calendar-month"/>
</svg>
<span class="visually-hidden">Month</span>
</label>
</div>
Drop borders using .btn-no-outline
, too.
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="iconsNoOutline" id="option8" autocomplete="off" checked>
<label class="btn btn-icon btn-no-outline" for="option8">
<svg width="1.25rem" height="1.25rem" fill="currentColor">
<use xlink:href="/docs/5.3/assets/img/boosted-sprite.svg#calendar-day"/>
</svg>
<span class="visually-hidden">Day</span>
</label>
<input type="radio" class="btn-check" name="iconsNoOutline" id="option9" autocomplete="off">
<label class="btn btn-icon btn-no-outline" for="option9">
<svg width="1.25rem" height="1.25rem" fill="currentColor">
<use xlink:href="/docs/5.3/assets/img/boosted-sprite.svg#calendar-week"/>
</svg>
<span class="visually-hidden">Week</span>
</label>
<input type="radio" class="btn-check" name="iconsNoOutline" id="option10" autocomplete="off">
<label class="btn btn-icon btn-no-outline" for="option10">
<svg width="1.25rem" height="1.25rem" fill="currentColor">
<use xlink:href="/docs/5.3/assets/img/boosted-sprite.svg#calendar-month"/>
</svg>
<span class="visually-hidden">Month</span>
</label>
</div>
Star rating
Added in v5.2.0
Star rating system is built on top of radios. Simply add .star-rating
to a <fieldset>
element to use predefined glyphs and compose your star rating system with as much stars as needed.
<form>
<fieldset class="star-rating">
<legend class="visually-hidden">Results relevance</legend>
<input type="radio" id="terrible" name="rating" value="1" class="visually-hidden">
<label for="terrible" title="Terrible"><span class="visually-hidden">Terrible</span></label>
<input type="radio" id="bad" name="rating" value="2" class="visually-hidden">
<label for="bad" title="Bad"><span class="visually-hidden">Bad</span></label>
<input type="radio" id="mixed" name="rating" value="3" class="visually-hidden">
<label for="mixed" title="Mixed"><span class="visually-hidden">Mixed</span></label>
<input type="radio" id="good" name="rating" value="4" class="visually-hidden" checked>
<label for="good" title="Good"><span class="visually-hidden">Good</span></label>
<input type="radio" id="excellent" name="rating" value="5" class="visually-hidden">
<label for="excellent" title="Excellent"><span class="visually-hidden">Excellent</span></label>
</fieldset>
</form>
Sizes
Star ratings come with a smaller variant: .star-rating-sm
.
<form>
<fieldset class="star-rating star-rating-sm">
<legend class="visually-hidden">Results relevance</legend>
<input type="radio" id="terrible2" name="rating" value="1" class="visually-hidden">
<label for="terrible2" title="Terrible"><span class="visually-hidden">Terrible</span></label>
<input type="radio" id="bad2" name="rating" value="2" class="visually-hidden">
<label for="bad2" title="Bad"><span class="visually-hidden">Bad</span></label>
<input type="radio" id="mixed2" name="rating" value="3" class="visually-hidden">
<label for="mixed2" title="Mixed"><span class="visually-hidden">Mixed</span></label>
<input type="radio" id="good2" name="rating" value="4" class="visually-hidden" checked>
<label for="good2" title="Good"><span class="visually-hidden">Good</span></label>
<input type="radio" id="excellent2" name="rating" value="5" class="visually-hidden">
<label for="excellent2" title="Excellent"><span class="visually-hidden">Excellent</span></label>
</fieldset>
</form>
Dark variant
Deprecated in v5.3.3
Heads up! Dark variants for components are deprecated in Boosted v5.3.3. They are replaced by our contextual dark mode.
Add data-bs-theme="dark"
to the .star-rating
or any ancestor element to enable a component-specific color mode. Learn more about our color modes.
Readonly
Make star ratings readable but non-editable by using <span>
s instead of <input>
elements.
<div class="star-rating">
<p class="visually-hidden">Star rating: rated 3 out of 5</p>
<div aria-hidden="true">
<span></span>
<span></span>
<span class="checked"></span>
<span></span>
<span></span>
</div>
</div>
Disabled
Make star ratings look inactive inside or outside a form by adding the disabled
boolean attribute to the <fieldset>
element and the checked
boolean attribute to any <input>
element.
<form>
<fieldset class="star-rating" disabled aria-hidden="true">
<legend class="visually-hidden">Disabled star rating</legend>
<input type="radio" id="terrible4" name="rating" value="1" class="visually-hidden">
<label for="terrible4" title="Terrible"><span class="visually-hidden">Terrible</span></label>
<input type="radio" id="bad4" name="rating" value="2" class="visually-hidden">
<label for="bad4" title="Bad"><span class="visually-hidden">Bad</span></label>
<input type="radio" id="mixed4" name="rating" value="3" class="visually-hidden" checked>
<label for="mixed4" title="Mixed"><span class="visually-hidden">Mixed</span></label>
<input type="radio" id="good4" name="rating" value="4" class="visually-hidden">
<label for="good4" title="Good"><span class="visually-hidden">Good</span></label>
<input type="radio" id="excellent4" name="rating" value="5" class="visually-hidden">
<label for="excellent4" title="Excellent"><span class="visually-hidden">Excellent</span></label>
</fieldset>
<p class="visually-hidden">Disabled star rating: rated 3 out of 5</p>
</form>
CSS
Sass variables
Variables for checks:
$form-check-input-width: 1em;
$form-check-min-height: $font-size-base * $input-btn-line-height;
$form-check-padding-start: $form-check-input-width + .5em;
$form-check-margin-bottom: .125rem;
$form-check-label-padding-top: .4375rem; // Boosted mod
$form-check-label-color: null;
$form-check-label-cursor: null;
$form-check-transition: null;
$form-check-filter: $invert-filter; // Boosted mod
$form-check-input-active-filter: null;
$form-check-input-active-bg-color: $component-active-bg; // Boosted mod
$form-check-input-bg: $input-bg;
$form-check-input-border: var(--#{$prefix}border-width) solid $input-border-color; // Boosted mod: instead of `var(--#{$prefix}border-width) solid var(--#{$prefix}border-color)`
$form-check-input-border-radius: 0;
$form-check-radio-border-radius: 50%;
$form-check-input-focus-border: null;
$form-check-input-focus-box-shadow: $focus-ring-box-shadow;
$form-check-input-checked-color: $component-active-color;
$form-check-input-checked-bg-color: $component-active-bg;
$form-check-input-checked-border-color: $form-check-input-checked-bg-color;
$form-check-input-checked-bg-image: var(--#{$prefix}check-icon);
$form-check-input-disabled-color: $gray-900; // Boosted mod
$form-check-input-disabled-filter: var(--#{$prefix}form-check-filter); // Boosted mod
$form-check-radio-checked-bg-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'><circle r='2' fill='#{$form-check-input-checked-color}'/></svg>");
$form-check-input-indeterminate-color: $form-check-input-checked-color;
$form-check-input-indeterminate-bg-color: $form-check-input-checked-bg-color;
$form-check-input-indeterminate-border-color: $form-check-input-indeterminate-bg-color;
$form-check-input-indeterminate-bg-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 3'><path fill='#{$form-check-input-indeterminate-color}' d='M0 0h10v3H0z'/></svg>");
$form-check-input-disabled-opacity: null;
$form-check-label-disabled-opacity: null;
$form-check-btn-check-disabled-opacity: null;
$form-check-inline-margin-end: 1rem;
// Boosted mod: Star rating
$form-star-size: 1.5625rem;
$form-star-size-sm: 1.25rem;
$form-star-margin-between: -.125rem;
$form-star-rating-checked-color: var(--#{$prefix}primary);
$form-star-rating-unchecked-color: var(--#{$prefix}secondary-color);
$form-star-rating-hover-color: var(--#{$prefix}highlight-bg);
$form-star-rating-disabled-color: var(--#{$prefix}disabled-color);
$form-star-rating-checked-icon: escape-svg(url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 25 25'><path fill='#{$black}' stroke='#{$black}' d='m12.5 4.523 2.016 6.227 6.542-.005-5.296 3.843 2.027 6.224L12.5 16.96l-5.289 3.852 2.027-6.224-5.296-3.843 6.542.005L12.5 4.523Z'/></svg>"));
$form-star-rating-unchecked-icon: escape-svg(url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 25 25'><path fill='transparent' stroke='#{$black}' d='m12.5 4.523 2.016 6.227 6.542-.005-5.296 3.843 2.027 6.224L12.5 16.96l-5.289 3.852 2.027-6.224-5.296-3.843 6.542.005L12.5 4.523Z'/></svg>"));
$form-star-rating-sm-checked-icon: escape-svg(url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><path fill='#{$black}' stroke='#{$black}' d='M10 3.943 11.54 8.7l4.998-.004-4.046 2.936 1.548 4.755L10 13.444l-4.04 2.943 1.548-4.755-4.046-2.936L8.46 8.7 10 3.943Z'/></svg>"));
$form-star-rating-sm-unchecked-icon: escape-svg(url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><path fill='transparent' stroke='#{$black}' d='M10 3.943 11.54 8.7l4.998-.004-4.046 2.936 1.548 4.755L10 13.444l-4.04 2.943 1.548-4.755-4.046-2.936L8.46 8.7 10 3.943Z'/></svg>"));
//fusv-disable
$form-star-focus-color: $black; // Deprecated in v5.2.3
$form-star-focus-outline: var(--#{$prefix}border-width) solid $form-star-focus-color; // Deprecated in v5.2.3
$form-star-focus-color-dark: $white; // Deprecated in v5.2.3
$form-star-focus-outline-dark: var(--#{$prefix}border-width) solid $form-star-focus-color-dark; // Deprecated in v5.2.3
$form-star-focus-box-shadow: $input-btn-focus-box-shadow; // Deprecated in v5.2.3
//fusv-enable
// End mod
Variables for switches:
// Boosted mod: no $form-switch-color
$form-switch-width: $spacer * 3; // Boosted mod
$form-switch-padding-start: $form-switch-width + .625rem; // Boosted mod
$form-switch-bg-image: var(--#{$prefix}close-icon); // Boosted mod
$form-switch-bg-position: right .5rem top 50%; // Boosted mod
$form-switch-bg-size: .75rem; // Boosted mod
$form-switch-bg-square-size: add(1rem, $spacer * .5); // Boosted mod
$form-switch-border-radius: null; // Boosted mod
$form-switch-transition: background-position .15s ease-in-out, $transition-focus; // Boosted mod
$form-switch-square-bg: $black; // Boosted mod
$form-switch-bg: $white; // Boosted mod
$form-switch-border-color: $white; // Boosted mod
$form-switch-filter: var(--#{$prefix}form-check-filter); // Boosted mod
$form-switch-focus-visible-inner: $black; // Boosted mod
$form-switch-focus-visible-outer: $white; // Boosted mod
// Boosted mod: no $form-switch-focus-color
// Boosted mod: no $form-switch-focus-bg-image
// Boosted mod: no $form-switch-checked-color
$form-switch-checked-bg-image: $form-check-input-checked-bg-image; // Boosted mod
$form-switch-checked-bg-size: add(map-get($spacers, 2), map-get($spacers, 1)); // Boosted mod
// stylelint-disable-next-line function-disallowed-list
$form-switch-checked-bg-position: calc(var(--#{$prefix}border-width) * 3) 50%; // Boosted mod
$form-switch-checked-square-bg: var(--#{$prefix}body-bg); // Boosted mod
$form-switch-checked-bg: $supporting-orange; // Boosted mod
$form-switch-checked-border-color: $supporting-orange; // Boosted mod
$form-switch-checked-filter: none; // Boosted mod
$form-switch-checked-focus-inner: var(--#{$prefix}focus-visible-inner-color); // Boosted mod
$form-switch-checked-focus-outer: var(--#{$prefix}focus-visible-outer-color); // Boosted mod
Sass mixins
Deprecated in v5.3.2
@mixin form-star-rating($color) {
background-image: escape-svg(url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 25 25'><path fill='#{$color}' d='m12.5 2.9 2.379 7.35 7.721-.007-6.25 4.536 2.392 7.346-6.242-4.547-6.242 4.547 2.392-7.346-6.25-4.536 7.721.007Z'/></svg>"));
}