Checkbox
A checkbox with label, description, invalid and disabled states.
Installation
bash
npx bootcn add checkboxbash
pnpm dlx bootcn add checkboxbash
yarn dlx bootcn add checkboxbash
bunx --bun bootcn add checkboxThe CLI also installs the design tokens and these components: none. It prints the <link> and <script> tags to add to your layout.
Usage
html
<div class="form-check bootcn-checkbox">
<input
class="form-check-input bootcn-checkbox__input"
type="checkbox"
id="news"
name="newsletter"
aria-describedby="news-description"
/>
<label class="form-check-label bootcn-checkbox__label" for="news">Product updates</label>
<p class="bootcn-checkbox__description" id="news-description">One email a month.</p>
</div>States
- Checked / disabled / required: native attributes.
- Invalid:
aria-invalid="true"and a.bootcn-checkbox__errorelement referenced fromaria-describedby. - Indeterminate: set
input.indeterminate = truefrom your own script; there is no HTML attribute for it. Bootstrap styles the state.
Customizing
| Variable | Default |
|---|---|
--bootcn-checkbox-size | 1.125rem |
--bootcn-checkbox-gap | 0.625rem |
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.
No JavaScript.
Accessibility
- The whole label is clickable because it uses
<label for>. - Group related checkboxes in a
<fieldset>with a<legend>. - Use
aria-describedbyfor descriptions and errors, andaria-invalidonly while the field is invalid.
Source
The exact files the CLI installs (checkbox@1.1.0). View on GitHub · Report an issue
html
<div class="form-check bootcn-checkbox">
<input
class="form-check-input bootcn-checkbox__input"
type="checkbox"
id="bootcn-checkbox-terms"
name="terms"
/>
<label class="form-check-label bootcn-checkbox__label" for="bootcn-checkbox-terms"
>Accept terms and conditions</label
>
</div>
<div class="form-check bootcn-checkbox mt-3">
<input
class="form-check-input bootcn-checkbox__input"
type="checkbox"
id="bootcn-checkbox-news"
name="newsletter"
checked
aria-describedby="bootcn-checkbox-news-description"
/>
<label class="form-check-label bootcn-checkbox__label" for="bootcn-checkbox-news"
>Product updates</label
>
<p class="bootcn-checkbox__description" id="bootcn-checkbox-news-description">
One email a month. Unsubscribe at any time.
</p>
</div>
<div class="form-check bootcn-checkbox mt-3">
<input
class="form-check-input bootcn-checkbox__input"
type="checkbox"
id="bootcn-checkbox-required"
name="consent"
required
aria-invalid="true"
aria-describedby="bootcn-checkbox-required-error"
/>
<label class="form-check-label bootcn-checkbox__label" for="bootcn-checkbox-required"
>I agree to the data policy</label
>
<p class="bootcn-checkbox__error" id="bootcn-checkbox-required-error">
You must agree to continue.
</p>
</div>
<div class="form-check bootcn-checkbox mt-3">
<input
class="form-check-input bootcn-checkbox__input"
type="checkbox"
id="bootcn-checkbox-disabled"
disabled
/>
<label class="form-check-label bootcn-checkbox__label" for="bootcn-checkbox-disabled"
>Disabled option</label
>
</div>css
.bootcn-checkbox {
--bootcn-checkbox-size: 1rem;
--bootcn-checkbox-gap: 0.625rem;
display: grid;
grid-template-columns: var(--bootcn-checkbox-size) 1fr;
column-gap: var(--bootcn-checkbox-gap);
align-items: start;
padding-left: 0;
}
.bootcn-checkbox .bootcn-checkbox__input {
width: var(--bootcn-checkbox-size);
height: var(--bootcn-checkbox-size);
margin: 0.125rem 0 0;
border-color: var(--bootcn-primary);
border-radius: var(--bootcn-radius-sm);
background-color: transparent;
box-shadow: var(--bootcn-shadow-sm);
transition:
background-color var(--bootcn-transition),
border-color var(--bootcn-transition),
box-shadow var(--bootcn-transition);
}
.bootcn-checkbox .bootcn-checkbox__input:checked,
.bootcn-checkbox .bootcn-checkbox__input:indeterminate {
border-color: var(--bootcn-primary);
background-color: var(--bootcn-primary);
}
/* In dark mode the primary is light, so the check mark turns dark. */
[data-bs-theme='dark'] .bootcn-checkbox .bootcn-checkbox__input:checked {
--bs-form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%2318181b' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m6 10 3 3 6-6'/%3e%3c/svg%3e");
}
.bootcn-checkbox .bootcn-checkbox__input:focus-visible {
border-color: var(--bootcn-primary);
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-checkbox .bootcn-checkbox__input:focus:not(:focus-visible) {
box-shadow: none;
}
.bootcn-checkbox .bootcn-checkbox__input[aria-invalid='true'] {
border-color: var(--bs-danger);
}
.bootcn-checkbox__label {
grid-column: 2;
font-size: 0.875rem;
font-weight: 500;
line-height: 1.25rem;
}
.bootcn-checkbox__description,
.bootcn-checkbox__error {
grid-column: 2;
margin: 0.125rem 0 0;
font-size: 0.875rem;
}
.bootcn-checkbox__description {
color: var(--bs-secondary-color);
}
.bootcn-checkbox__error {
color: var(--bs-form-invalid-color);
}
