Approach
Browser default checkboxes and radios are replaced with the help of FormCheck
, 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 FormCheckInput
s and FormCheckLabel
s are sibling elements as opposed to an FormCheckInput
within a FormCheckLabel
.
This is slightly more verbose as you must specify id
and for
attributes to relate the FormCheckInput
and FormCheckLabel
.
We use the sibling selector (~
) for all our FormCheckInput
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 FormCheckInput
's state.
Our checks use custom Bootstrap icons to indicate checked or indeterminate states.
Checks
- Checkbox 1
- false
- Checkbox 2
- true
<template>
<BFormCheck>
<BFormCheckInput v-model="checkbox1" />
<BFormCheckLabel>Default checkbox</BFormCheckLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput v-model="checkbox2" />
<BFormCheckLabel>Checked checkbox</BFormCheckLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput checked />
<BFormCheckLabel>Static checked checkbox</BFormCheckLabel>
</BFormCheck>
<b-dl margin="t-5">
<b-dt col="sm-3">
Checkbox 1
</b-dt>
<b-dd col="sm-9">
{{ checkbox1 }}
</b-dd>
<b-dt col="sm-3">
Checkbox 2
</b-dt>
<b-dd col="sm-9">
{{ checkbox2 }}
</b-dd>
</b-dl>
</template>
<script setup>
const checkbox1 = ref(false);
const checkbox2 = ref(true);
</script>
Custom value Model Binding
- Checkbox 1
- ng
- Checkbox 2
- ok
<template>
<BFormCheck>
<BFormCheckInput
v-model="checkbox1"
true-value="ok"
false-value="ng"
/>
<BFormCheckLabel>Default checkbox</BFormCheckLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput
v-model="checkbox2"
true-value="ok"
false-value="ng"
/>
<BFormCheckLabel>Checked checkbox</BFormCheckLabel>
</BFormCheck>
<b-dl margin="t-5">
<b-dt col="sm-3">
Checkbox 1
</b-dt>
<b-dd col="sm-9">
{{ checkbox1 }}
</b-dd>
<b-dt col="sm-3">
Checkbox 2
</b-dt>
<b-dd col="sm-9">
{{ checkbox2 }}
</b-dd>
</b-dl>
</template>
<script setup>
const checkbox1 = ref('ng');
const checkbox2 = ref('ok');
</script>
Indeterminate
Checkboxes can utilize the indeterminate
attributes when manually set.
<template>
<BFormCheck>
<BFormCheckInput indeterminate />
<BFormCheckLabel>
Indeterminate checkbox
</BFormCheckLabel>
</BFormCheck>
</template>
Disabled
Add the disabled
attribute and the associated FormCheckLabel
s are automatically styled to match with a lighter color to help indicate the input's state.
<template>
<BFormCheck>
<BFormCheckInput disabled />
<BFormCheckLabel>Disabled checkbox</BFormCheckLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput
checked
disabled
/>
<BFormCheckLabel>Disabled checked checkbox</BFormCheckLabel>
</BFormCheck>
</template>
Radios
- Selected Value
- two
<template>
<BFormCheck>
<BFormCheckInput
v-model="radioValue"
value="one"
type="radio"
/>
<BFormCheckLabel>Default radio</BFormCheckLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput
v-model="radioValue"
value="two"
type="radio"
/>
<BFormCheckLabel>Default checked radio</BFormCheckLabel>
</BFormCheck>
<b-dl margin="t-5">
<b-dt col="sm-3">
Selected Value
</b-dt>
<b-dd col="sm-9">
{{ radioValue }}
</b-dd>
</b-dl>
</template>
<script setup>
const radioValue = ref('two');
</script>
Disabled
Add the disabled
attribute and the associated FormCheckLabel
s are automatically styled to match with a lighter color to help indicate the input's state.
<template>
<BFormCheck>
<BFormCheckInput
type="radio"
name="flexRadioDisabled"
disabled
/>
<BFormCheckLabel>Disabled radio</BFormCheckLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput
type="radio"
name="flexRadioDisabled"
checked
disabled
/>
<BFormCheckLabel>Disabled checked radio</BFormCheckLabel>
</BFormCheck>
</template>
Switches
A switch has the markup of a custom checkbox but uses the 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.
- Checkbox 1
- false
- Checkbox 2
- true
- Checkbox 3
- false
<template>
<BFormCheck switch>
<BFormCheckInput v-model="checkbox1" />
<BFormCheckLabel>Default switch checkbox input</BFormCheckLabel>
</BFormCheck>
<BFormCheck switch>
<BFormCheckInput v-model="checkbox2" />
<BFormCheckLabel>Checked switch checkbox input</BFormCheckLabel>
</BFormCheck>
<BFormCheck switch>
<BFormCheckInput
v-model="checkbox3"
:disabled="checkbox1 != checkbox2"
/>
<BFormCheckLabel>Disabled switch checkbox input</BFormCheckLabel>
</BFormCheck>
<BFormCheck switch>
<BFormCheckInput
checked
disabled
/>
<BFormCheckLabel>Disabled checked switch checkbox input</BFormCheckLabel>
</BFormCheck>
<b-dl margin="t-5">
<b-dt col="sm-3">
Checkbox 1
</b-dt>
<b-dd col="sm-9">
{{ checkbox1 }}
</b-dd>
<b-dt col="sm-3">
Checkbox 2
</b-dt>
<b-dd col="sm-9">
{{ checkbox2 }}
</b-dd>
<b-dt col="sm-3">
Checkbox 3
</b-dt>
<b-dd col="sm-9">
{{ checkbox3 }}
</b-dd>
</b-dl>
</template>
<script setup>
const checkbox1 = ref(false);
const checkbox2 = ref(true);
const checkbox3 = ref(false);
</script>
Default (stacked)
By default, any number of checkboxes and radios that are immediate sibling will be vertically stacked and appropriately spaced with FormCheck
.
<template>
<BFormCheck>
<BFormCheckInput />
<BFormCheckLabel>Default checkbox</BFormCheckLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput disabled />
<BFormCheckLabel>Disabled checkbox</BFormCheckLabel>
</BFormCheck>
</template>
<template>
<BFormCheck>
<BFormCheckInput
type="radio"
name="exampleRadios"
value="option1"
checked
/>
<BFormCheckLabel>Default radio</BFormCheckLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput
type="radio"
name="exampleRadios"
value="option2"
/>
<BFormLabel>Second default radio</BFormLabel>
</BFormCheck>
<BFormCheck>
<BFormCheckInput
type="radio"
name="exampleRadios"
value="option3"
disabled
/>
<BFormCheckLabel>Disabled radio</BFormCheckLabel>
</BFormCheck>
</template>
Inline
Group checkboxes or radios on the same horizontal row by adding inline
to any FormCheck
.
<template>
<BFormCheck inline>
<BFormCheckInput value="option1" />
<BFormCheckLabel>1</BFormCheckLabel>
</BFormCheck>
<BFormCheck inline>
<BFormCheckInput value="option2" />
<BFormCheckLabel>2</BFormCheckLabel>
</BFormCheck>
<BFormCheck inline>
<BFormCheckInput
value="option3"
disabled
/>
<BFormCheckLabel>3 (disabled)</BFormCheckLabel>
</BFormCheck>
<BFormCheck inline>
<BFormCheckInput
type="radio"
name="inlineRadioOptions"
value="option1"
/>
<BFormCheckLabel>1</BFormCheckLabel>
</BFormCheck>
<BFormCheck inline>
<BFormCheckInput
type="radio"
name="inlineRadioOptions"
value="option2"
/>
<BFormCheckLabel>2</BFormCheckLabel>
</BFormCheck>
<BFormCheck inline>
<BFormCheckInput
type="radio"
name="inlineRadioOptions"
value="option3"
disabled
/>
<BFormCheckLabel>3 (disabled)</BFormCheckLabel>
</BFormCheck>
</template>
Reverse
Put your checkboxes, radios, and switches on the opposite side with the reverse
modifier class.
<template>
<BFormCheck reverse>
<BFormCheckInput />
<BFormCheckLabel>Reverse checkbox</BFormCheckLabel>
</BFormCheck>
<BFormCheck reverse>
<BFormCheckInput
disabled
/>
<BFormCheckLabel>Disabled reverse checkbox</BFormCheckLabel>
</BFormCheck>
<BFormCheck
switch
reverse
>
<BFormCheckInput value="option3" />
<BFormCheckLabel>Reverse switch checkbox input</BFormCheckLabel>
</BFormCheck>
</template>
Without labels
Omit the wrapping FormCheck
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
).
<template>
<b-div>
<BFormCheckInput
aria-label="..."
/>
</b-div>
<b-div>
<BFormCheckInput
type="radio"
name="radioNoLabel"
aria-label="..."
/>
</b-div>
</template>
Toggle buttons
Create button-like checkboxes and radio buttons by using .btn
styles rather than .form-check-label
on the FormCheckLabel,
elements.
These toggle buttons can further be grouped in a button group
if needed.
Checkbox toggle buttons
- Toggle 1
- false
- Toggle 2
- true
<template>
<CheckboxToggleButton
v-model="checkbox1"
color="primary"
>
Single toggle 1
</CheckboxToggleButton>
<CheckboxToggleButton
v-model="checkbox2"
color="primary"
>
Single toggle 2
</CheckboxToggleButton>
<CheckboxToggleButton
color="primary"
checked
>
Checked
</CheckboxToggleButton>
<CheckboxToggleButton
color="primary"
disabled
>
Disabled
</CheckboxToggleButton>
<b-dl margin="t-5">
<b-dt col="sm-3">
Toggle 1
</b-dt>
<b-dd col="sm-9">
{{ checkbox1 }}
</b-dd>
<b-dt col="sm-3">
Toggle 2
</b-dt>
<b-dd col="sm-9">
{{ checkbox2 }}
</b-dd>
</b-dl>
</template>
<script setup>
const checkbox1 = ref(false);
const checkbox2 = ref(true);
</script>
- Toggle 1
- false
- Toggle 2
- true
<template>
<CheckboxToggleButton v-model="checkbox1">
Single toggle 1
</CheckboxToggleButton>
<CheckboxToggleButton v-model="checkbox2">
Single toggle 2
</CheckboxToggleButton>
<CheckboxToggleButton
disabled
>
Disabled
</CheckboxToggleButton>
<b-dl margin="t-5">
<b-dt col="sm-3">
Toggle 1
</b-dt>
<b-dd col="sm-9">
{{ checkbox1 }}
</b-dd>
<b-dt col="sm-3">
Toggle 2
</b-dt>
<b-dd col="sm-9">
{{ checkbox2 }}
</b-dd>
</b-dl>
</template>
<script setup>
const checkbox1 = ref(false);
const checkbox2 = ref(true);
</script>
Radio toggle buttons
- Toggle 1
- one
<template>
<RadioToggleButton
v-model="radio1"
value="one"
color="secondary"
checked
>
Checked
</RadioToggleButton>
<RadioToggleButton
v-model="radio1"
value="two"
color="secondary"
>
Radio
</RadioToggleButton>
<RadioToggleButton
v-model="radio1"
value="three"
color="secondary"
disabled
>
Disabled
</RadioToggleButton>
<RadioToggleButton
v-model="radio1"
value="four"
color="secondary"
>
Radio
</RadioToggleButton>
<b-dl margin="t-5">
<b-dt col="sm-3">
Toggle 1
</b-dt>
<b-dd col="sm-9">
{{ radio1 }}
</b-dd>
</b-dl>
</template>
<script setup>
const radio1 = ref('one');
</script>
- Toggle 1
- one
<template>
<RadioToggleButton
v-model="radio1"
value="one"
checked
>
Checked
</RadioToggleButton>
<RadioToggleButton
v-model="radio1"
value="two"
>
Radio
</RadioToggleButton>
<RadioToggleButton
v-model="radio1"
value="three"
disabled
>
Disabled
</RadioToggleButton>
<RadioToggleButton
v-model="radio1"
value="four"
>
Radio
</RadioToggleButton>
<b-dl margin="t-5">
<b-dt col="sm-3">
Toggle 1
</b-dt>
<b-dd col="sm-9">
{{ radio1 }}
</b-dd>
</b-dl>
</template>
<script setup>
const radio1 = ref('one');
</script>
Outlined styles
Different variants of .btn
, such at the various outlined styles, are supported.
<template>
<CheckboxToggleButton color="outline-primary">
Single toggle
</CheckboxToggleButton>
<CheckboxToggleButton
color="outline-secondary"
checked
>
Checked
</CheckboxToggleButton>
<RadioToggleButton
name="options-outlined"
color="outline-success"
checked
>
Checked success radio
</RadioToggleButton>
<RadioToggleButton
name="options-outlined"
color="outline-danger"
>
Danger radio
</RadioToggleButton>
</template>