Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 58 ยท Forms and Buttons

Selects, Checks, Switches and Ranges

Style select menus, checkboxes, radios, switches and range sliders in Bootstrap 5, with inline layouts, button-style choices, datalists and accessible markup.

Text fields are only part of a form. People also pick from lists, tick options, choose one of several, flip settings on and off and drag sliders. Each of these has its own native control, and each one looks different in every browser. Bootstrap restyles them with a few classes while keeping the real HTML elements, so keyboard use, form submission and screen readers keep working.

This lesson covers form-select, checkboxes and radios (form-check), switches, inline and reversed layouts, button-style choices with btn-check, range sliders and datalists. It follows Form Controls.

Select menus

Put form-select on a <select>. It gets the same border, padding and focus style as a text input, plus a custom dropdown arrow. Sizes are form-select-lg and form-select-sm. A multiple select, or one with a size attribute above 1, shows a scrolling list instead of a dropdown.

Select menus

The first option in the first select is a prompt: selected disabled shows it as the starting text but prevents users from submitting it as a real answer.

Checkboxes and radios

Checkboxes and radios share one structure: a wrapper form-check, an <input> with form-check-input, and a <label> with form-check-label. Link label and input with for and id so that clicking the text toggles the control. The difference is only the input type:

  • Checkboxes (type="checkbox") allow any number of choices. Give each its own name.
  • Radios (type="radio") allow exactly one choice from a group. All radios in a group share the same name, and each has a different value.

Add disabled to dim a choice and checked to preselect one. A checkbox can also be in an indeterminate state ("some children selected"), but that can only be set from JavaScript (input.indeterminate = true), not with an HTML attribute.

Checkboxes and radios

For a group of related choices, semantic HTML says to wrap them in a <fieldset> with a <legend>, which screen readers announce as the group's name. The "paragraph as heading" above keeps the example short; in production, prefer the fieldset.

Switches

A switch is a checkbox that looks like a toggle. Add form-switch to the wrapper and keep role="switch" on the input so assistive technology announces it as a switch rather than a checkbox. Switches suit settings that take effect immediately ("Dark mode: on"); checkboxes suit choices that are submitted later with a form.

Inline and reversed layouts

By default each form-check is a separate line. Add form-check-inline to put them side by side. In right-aligned layouts, form-check-reverse puts the control on the opposite side of its label.

Switches, inline and reversed

Button-style choices

The btn-check trick from the buttons lesson also works for radios. Place a visually hidden btn-check input and a btn label right after it. The label's look changes when the input is checked. Together with a btn-group this makes a segmented control that is still a real radio group.

HTML
<div class="btn-group" role="group" aria-label="Billing period">
  <input type="radio" class="btn-check" name="period" id="monthly" autocomplete="off" checked>
  <label class="btn btn-outline-primary" for="monthly">Monthly</label>
  <input type="radio" class="btn-check" name="period" id="yearly" autocomplete="off">
  <label class="btn btn-outline-primary" for="yearly">Yearly</label>
</div>

Range sliders

<input type="range"> with the class form-range gives a slider. Set min, max and step; the default range is 0 to 100. The browser does not show the chosen value, so you either show it in a label or read it in JavaScript.

Datalist: suggestions on a text input

A <datalist> supplies suggestions to a normal form-control text input through the list attribute. Unlike a select, the user may still type any value. This is good for fields with common answers, such as a city or a job title.

Range and datalist
flowchart TD
  Q["What does the user choose?"] --> A["One of a long list"]
  Q --> B["One of a few"]
  Q --> C["Any number"]
  Q --> D["On or off now"]
  Q --> E["A number in a range"]
  A --> A1["form-select"]
  B --> B1["radios or btn-check"]
  C --> C1["checkboxes"]
  D --> D1["switch"]
  E --> E1["form-range"]

Choose the control by the kind of answer: long list, few options, many selections, immediate setting or a number.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which class goes on a select element?

    form-select styles the dropdown and adds its arrow; form-select-lg and form-select-sm change the size.
  2. 2. What must all radio buttons in one group share?

    The shared name is what lets the browser allow only one selection. Ids must stay unique.
  3. 3. How do you make a checkbox look like a toggle switch?

    A switch is still a checkbox. form-switch changes the look and role="switch" tells assistive technology.
  4. 4. How can you set a checkbox to the indeterminate state?

    There is no HTML attribute for it; the property exists only in the DOM.
  5. 5. What is the main difference between a select and an input with a datalist?

    A datalist is a list of suggestions, not a restriction.

Summary

  • form-select styles dropdowns; form-select-lg and -sm size them.
  • Checkboxes and radios use form-check, form-check-input and form-check-label; radios in a group share a name.
  • form-switch plus role="switch" makes a toggle; form-check-inline and form-check-reverse change the layout.
  • btn-check with btn labels builds button-style choices.
  • form-range makes sliders (show the value yourself); datalist adds suggestions to a text input.

Next lesson: Input Groups and Floating Labels.