Radio group
A labeled group of radio options, as a list or as selectable cards.
Installation
bash
npx bootcn add radio-groupbash
pnpm dlx bootcn add radio-groupbash
yarn dlx bootcn add radio-groupbash
bunx --bun bootcn add radio-groupThe CLI also installs the design tokens and these components: none. It prints the <link> and <script> tags to add to your layout.
Usage
html
<fieldset class="bootcn-radio-group">
<legend class="bootcn-radio-group__legend">Notify me about</legend>
<div class="form-check bootcn-radio-group__item">
<input
class="form-check-input"
type="radio"
name="notify"
id="notify-all"
value="all"
checked
/>
<label class="form-check-label" for="notify-all">All new messages</label>
</div>
<!-- more options with the same name -->
</fieldset>Add bootcn-radio-group--cards to the fieldset for a card layout. Add a .bootcn-radio-group__description (referenced with aria-describedby) under any label.
The selected card highlight uses :has(); in browsers without it the radio button itself still shows the selection.
Customizing
| Variable | Default |
|---|---|
--bootcn-radio-size | 1.125rem |
--bootcn-radio-gap | 0.625rem |
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.
No JavaScript.
Accessibility
<fieldset>and<legend>name the group for screen readers.- Native radios give arrow-key navigation within the group for free.
- In the card layout the label is stretched over the card, so clicking anywhere selects the option without extra JavaScript.
Source
The exact files the CLI installs (radio-group@1.1.0). View on GitHub · Report an issue
html
<fieldset class="bootcn-radio-group">
<legend class="bootcn-radio-group__legend">Notify me about</legend>
<div class="form-check bootcn-radio-group__item">
<input
class="form-check-input"
type="radio"
name="bootcn-notify"
id="bootcn-notify-all"
value="all"
checked
/>
<label class="form-check-label" for="bootcn-notify-all">All new messages</label>
</div>
<div class="form-check bootcn-radio-group__item">
<input
class="form-check-input"
type="radio"
name="bootcn-notify"
id="bootcn-notify-mentions"
value="mentions"
/>
<label class="form-check-label" for="bootcn-notify-mentions"
>Direct messages and mentions</label
>
</div>
<div class="form-check bootcn-radio-group__item">
<input
class="form-check-input"
type="radio"
name="bootcn-notify"
id="bootcn-notify-none"
value="none"
disabled
/>
<label class="form-check-label" for="bootcn-notify-none">Nothing (disabled)</label>
</div>
</fieldset>
<fieldset class="bootcn-radio-group bootcn-radio-group--cards mt-4">
<legend class="bootcn-radio-group__legend">Plan</legend>
<div class="form-check bootcn-radio-group__item">
<input
class="form-check-input"
type="radio"
name="bootcn-plan"
id="bootcn-plan-starter"
value="starter"
checked
aria-describedby="bootcn-plan-starter-description"
/>
<label class="form-check-label" for="bootcn-plan-starter">Starter</label>
<p class="bootcn-radio-group__description" id="bootcn-plan-starter-description">
For personal projects.
</p>
</div>
<div class="form-check bootcn-radio-group__item">
<input
class="form-check-input"
type="radio"
name="bootcn-plan"
id="bootcn-plan-team"
value="team"
aria-describedby="bootcn-plan-team-description"
/>
<label class="form-check-label" for="bootcn-plan-team">Team</label>
<p class="bootcn-radio-group__description" id="bootcn-plan-team-description">
Up to 20 members.
</p>
</div>
</fieldset>css
.bootcn-radio-group {
--bootcn-radio-size: 1rem;
--bootcn-radio-gap: 0.625rem;
display: grid;
gap: 0.625rem;
min-width: 0;
margin: 0;
padding: 0;
border: 0;
}
.bootcn-radio-group__legend {
float: none;
width: auto;
margin-bottom: 0.25rem;
padding: 0;
font-size: 0.875rem;
font-weight: 600;
}
.bootcn-radio-group__item {
display: grid;
grid-template-columns: var(--bootcn-radio-size) 1fr;
column-gap: var(--bootcn-radio-gap);
align-items: start;
margin: 0;
padding-left: 0;
}
.bootcn-radio-group__item > .form-check-input {
width: var(--bootcn-radio-size);
height: var(--bootcn-radio-size);
margin: 0.125rem 0 0;
border-color: var(--bootcn-primary);
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-radio-group__item > .form-check-input:checked {
border-color: var(--bootcn-primary);
background-color: var(--bootcn-primary);
}
/* In dark mode the primary is light, so the dot turns dark. */
[data-bs-theme='dark'] .bootcn-radio-group__item > .form-check-input:checked {
--bs-form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='2' fill='%2318181b'/%3e%3c/svg%3e");
}
.bootcn-radio-group__item > .form-check-input:focus-visible {
border-color: var(--bootcn-primary);
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-radio-group__item > .form-check-input:focus:not(:focus-visible) {
box-shadow: none;
}
.bootcn-radio-group__item > .form-check-label {
grid-column: 2;
font-size: 0.875rem;
line-height: 1.25rem;
}
.bootcn-radio-group__description {
grid-column: 2;
margin: 0.125rem 0 0;
color: var(--bs-secondary-color);
font-size: 0.875rem;
}
/* Card layout */
.bootcn-radio-group--cards {
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
}
.bootcn-radio-group--cards > .bootcn-radio-group__legend {
grid-column: 1 / -1;
}
.bootcn-radio-group--cards .bootcn-radio-group__item {
position: relative;
padding: 1rem;
border: var(--bootcn-border-width) solid var(--bootcn-border-color);
border-radius: var(--bootcn-radius-lg);
transition:
border-color var(--bootcn-transition),
background-color var(--bootcn-transition);
}
.bootcn-radio-group--cards .bootcn-radio-group__item > .form-check-label {
font-weight: 500;
}
/* The label covers the whole card so any click selects the option. */
.bootcn-radio-group--cards .bootcn-radio-group__item > .form-check-label::after {
content: '';
position: absolute;
inset: 0;
cursor: pointer;
}
.bootcn-radio-group--cards .bootcn-radio-group__item:has(> .form-check-input:checked) {
border-color: var(--bootcn-primary);
background-color: var(--bootcn-accent);
}
.bootcn-radio-group--cards .bootcn-radio-group__item:has(> .form-check-input:disabled) {
opacity: 0.65;
}
