Checkboxes and radio buttons let users pick options on a form. They help collect choices easily.
Checkboxes and radios in Bootsrap
<div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="flexCheckDefault"> <label class="form-check-label" for="flexCheckDefault"> Default checkbox </label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked> <label class="form-check-label" for="exampleRadios1"> First radio </label> </div>
Use form-check class to wrap each checkbox or radio for proper spacing.
Use form-check-input on the input and form-check-label on the label for Bootstrap styling.
for and id.<div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="check1"> <label class="form-check-label" for="check1"> Option 1 </label> </div>
<div class="form-check"> <input class="form-check-input" type="radio" name="group1" id="radio1" value="1" checked> <label class="form-check-label" for="radio1"> Radio 1 </label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="group1" id="radio2" value="2"> <label class="form-check-label" for="radio2"> Radio 2 </label> </div>
form-switch.<div class="form-check form-switch"> <input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault"> <label class="form-check-label" for="flexSwitchCheckDefault">Toggle switch</label> </div>
This page shows a form with checkboxes and radio buttons styled by Bootstrap. Users can pick multiple fruits with checkboxes and one fruit with radio buttons.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Checkboxes and Radios</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <main class="container py-4"> <h1>Choose your options</h1> <form> <section> <h2>Checkboxes</h2> <div class="form-check"> <input class="form-check-input" type="checkbox" value="apple" id="checkApple"> <label class="form-check-label" for="checkApple">Apple</label> </div> <div class="form-check"> <input class="form-check-input" type="checkbox" value="banana" id="checkBanana"> <label class="form-check-label" for="checkBanana">Banana</label> </div> </section> <section class="mt-4"> <h2>Radio buttons</h2> <div class="form-check"> <input class="form-check-input" type="radio" name="fruit" id="radioOrange" value="orange" checked> <label class="form-check-label" for="radioOrange">Orange</label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="fruit" id="radioGrape" value="grape"> <label class="form-check-label" for="radioGrape">Grape</label> </div> </section> <button type="submit" class="btn btn-primary mt-3">Submit</button> </form> </main> </body> </html>
Always link label to input using matching for and id for better accessibility.
Use the same name attribute for radio buttons in a group to allow only one selection.
Bootstrap's form-check classes handle spacing and alignment automatically.
Checkboxes let users select multiple options; radios let users select one.
Bootstrap styles checkboxes and radios with form-check, form-check-input, and form-check-label classes.
Always connect labels to inputs for easy clicking and accessibility.
Practice
Solution
Step 1: Understand checkbox behavior
Checkboxes let users pick more than one option at the same time.Step 2: Understand radio button behavior
Radio buttons let users pick only one option from a group.Final Answer:
Checkboxes allow multiple selections; radios allow only one selection. -> Option BQuick Check:
Checkboxes = multiple, Radios = single [OK]
- Confusing which input allows multiple selections
- Thinking radios can be unchecked by clicking again
- Assuming checkboxes behave like radios
Solution
Step 1: Check input and label classes
Bootstrap requiresform-check-inputon the input andform-check-labelon the label.Step 2: Verify input type and id-label connection
The input must be type checkbox and the label'sforattribute must match input'sid.Final Answer:
Option A has correct classes, input type, and label connection. -> Option AQuick Check:
Correct classes and matching id-for =Option 1 [OK]
- Swapping classes between input and label
- Using wrong input type for checkbox
- Not matching label's for attribute with input id
<div class='form-check'> <input class='form-check-input' type='radio' name='color' id='red' checked> <label class='form-check-label' for='red'>Red</label> </div> <div class='form-check'> <input class='form-check-input' type='radio' name='color' id='blue'> <label class='form-check-label' for='blue'>Blue</label> </div>
Solution
Step 1: Identify input types and attributes
Both inputs are radio buttons with the same name 'color', so only one can be selected.Step 2: Check which radio is checked by default
The input with id 'red' has thecheckedattribute, so it is selected initially.Final Answer:
Two radio buttons labeled Red and Blue; Red is selected by default. -> Option CQuick Check:
Radio with checked attribute = selected [OK]
- Confusing radio buttons with checkboxes
- Ignoring the checked attribute
- Assuming multiple radios can be selected
<div class='form-check'> <input class='form-check-input' type='checkbox' id='agree'> <label class='form-check-label' id='agree'>I agree</label> </div>
Solution
Step 1: Check label linkage to input
Labels must use theforattribute matching the input'sidfor accessibility and clicking.Step 2: Identify incorrect attribute usage
The label incorrectly usesid='agree'instead offor='agree'.Final Answer:
The label uses id instead of for attribute to link to input. -> Option DQuick Check:
Label uses for, not id, to link input [OK]
- Using id on label instead of for
- Missing form-check-input class on input
- Confusing input types checkbox vs radio
Solution
Step 1: Confirm input types for multiple selection
Checkboxes allow multiple selections, so input type must be checkbox.Step 2: Check label and input classes and attributes
Inputs must have classform-check-input, labels must haveform-check-label, and labels must useformatching inputid.Step 3: Verify unique ids and consistent names
Each checkbox has a unique id and the same name attribute to group them logically.Final Answer:
Option A correctly uses checkboxes with proper classes, unique ids, and accessible labels. -> Option AQuick Check:
Checkboxes + form-check-input + matching for/id =\n \n Apple\n\n\n \n Banana\n[OK]
- Using radio buttons instead of checkboxes for multiple choices
- Repeating ids for different inputs
- Swapping input and label classes
Ready to prove it?
Pass the test and this pattern moves unknown
