Skip to content

Checkbox Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Add a checkbox and connect a label to it. A <label> whose for holds the id of the checkbox names it and toggles it, like on a native input.

--checked
State
<div class="checkbox-basic">
<sv-checkbox id="newsletter-checkbox"></sv-checkbox>
<label for="newsletter-checkbox">Subscribe to the newsletter</label>
</div>
<style>
.checkbox-basic {
display: flex;
gap: 0.5em;
align-items: center;
justify-content: center;
label {
cursor: pointer;
}
}
</style>

Select All

Check or uncheck a group of checkboxes with one parent checkbox. controls takes their IDs, and the parent turns indeterminate when only some are checked.

Sandwich condiments
--checked--indeterminate
<fieldset class="checkbox-group">
<legend>Sandwich condiments</legend>
<label class="row">
<sv-checkbox
controls="condiment-lettuce condiment-tomato condiment-mustard condiment-sprouts"
></sv-checkbox>
All condiments
</label>
<ul class="options">
<li>
<label class="row">
<sv-checkbox id="condiment-lettuce"></sv-checkbox>
Lettuce
</label>
</li>
<li>
<label class="row">
<sv-checkbox id="condiment-tomato" checked></sv-checkbox>
Tomato
</label>
</li>
<li>
<label class="row">
<sv-checkbox id="condiment-mustard"></sv-checkbox>
Mustard
</label>
</li>
<li>
<label class="row">
<sv-checkbox id="condiment-sprouts"></sv-checkbox>
Sprouts
</label>
</li>
</ul>
</fieldset>
<style>
.checkbox-group {
max-inline-size: 16rem;
padding: 0.5em 0 0;
margin-inline: auto;
border: none;
.row {
display: flex;
gap: 0.5em;
align-items: center;
cursor: pointer;
}
.options {
display: flex;
flex-direction: column;
gap: 0.5em;
padding-inline-start: 1.5em;
margin: 0;
margin-block-start: 0.5em;
list-style: none;
}
}
</style>

Icons

Replace the icon of a checkbox state with your own SVG. The unchecked-icon, checked-icon, and mixed-icon slots replace the icon of their state, and controls puts a parent checkbox in the mixed state when only some options are checked.

unchecked-icon
checked-icon
mixed-icon
<div class="checkbox-icons">
<div class="variant">
<sv-checkbox aria-label="Cross when unchecked">
<!-- Cross -->
<svg
aria-hidden="true"
slot="unchecked-icon"
viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="m336-280-56-56 144-144-144-143 56-56 144 144 143-144 56 56-144 143 144 144-56 56-143-144-144 144Z"
></path>
</svg>
</sv-checkbox>
<span>unchecked-icon</span>
</div>
<div class="variant">
<sv-checkbox aria-label="Heart when checked">
<!-- Heart -->
<svg
class="heart"
aria-hidden="true"
slot="checked-icon"
viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="m480-120-58-52q-101-91-167-157T150-447.5Q111-500 95.5-544T80-634q0-94 63-157t157-63q52 0 99 22t81 62q34-40 81-62t99-22q94 0 157 63t63 157q0 46-15.5 90T810-447.5Q771-395 705-329T538-172l-58 52Z"
></path>
</svg>
</sv-checkbox>
<span>checked-icon</span>
</div>
<div class="variant">
<div class="group">
<label>
<sv-checkbox controls="icons-option-1 icons-option-2">
<!-- Dot -->
<svg
aria-hidden="true"
slot="mixed-icon"
viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M480-360q50 0 85-35t35-85q0-50-35-85t-85-35q-50 0-85 35t-35 85q0 50 35 85t85 35Z"
></path>
</svg>
</sv-checkbox>
All formats
</label>
<label>
<sv-checkbox id="icons-option-1" checked></sv-checkbox>
PNG
</label>
<label>
<sv-checkbox id="icons-option-2"></sv-checkbox>
JPEG
</label>
</div>
<span>mixed-icon</span>
</div>
</div>
<style>
.checkbox-icons {
display: grid;
gap: 1.5em;
justify-items: center;
.variant {
display: grid;
gap: 0.4em;
justify-items: center;
span {
font-size: 0.8em;
color: var(--sv-gray-3);
}
}
.heart {
box-sizing: border-box;
padding: 0.2em;
}
.group {
display: grid;
gap: 0.5em;
justify-items: start;
label {
display: flex;
gap: 0.5em;
align-items: center;
cursor: pointer;
&:not(:first-child) {
padding-inline-start: 1.25em;
}
}
}
}
</style>