Radio buttons and checkboxes let users pick options on a form. They help collect choices easily.
Radio buttons and checkboxes in HTML
<input type="radio" name="group1" value="option1"> Option 1 <input type="checkbox" name="check1" value="optionA"> Option A
Radio buttons with the same name belong to one group; only one can be selected.
Checkboxes work independently; users can select many or none.
<input type="radio" name="color" value="red"> Red <input type="radio" name="color" value="blue"> Blue
<input type="checkbox" name="fruit" value="apple"> Apple <input type="checkbox" name="fruit" value="banana"> Banana
checked.<input type="radio" name="size" value="small" checked> Small <input type="radio" name="size" value="large"> Large
<input type="checkbox" name="subscribe" value="yes" checked> Subscribe to newsletter
This page shows a form with two groups: one for picking a single favorite color using radio buttons, and one for selecting multiple hobbies using checkboxes. Labels make it easy to click the option text. The form is accessible and responsive.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Radio and Checkbox Example</title> <style> body { font-family: Arial, sans-serif; padding: 1rem; max-width: 400px; margin: auto; } fieldset { margin-bottom: 1.5rem; border: 1px solid #ccc; padding: 1rem; border-radius: 0.5rem; } legend { font-weight: bold; padding: 0 0.5rem; } label { display: block; margin-bottom: 0.5rem; cursor: pointer; } </style> </head> <body> <main> <form aria-label="Favorite options form"> <fieldset> <legend>Choose your favorite color</legend> <label><input type="radio" name="color" value="red"> Red</label> <label><input type="radio" name="color" value="green"> Green</label> <label><input type="radio" name="color" value="blue"> Blue</label> </fieldset> <fieldset> <legend>Select your hobbies</legend> <label><input type="checkbox" name="hobby" value="reading"> Reading</label> <label><input type="checkbox" name="hobby" value="sports"> Sports</label> <label><input type="checkbox" name="hobby" value="music"> Music</label> </fieldset> <button type="submit">Submit</button> </form> </main> </body> </html>
Always use <label> tags with inputs for better accessibility and easier clicking.
Use the same name attribute for radio buttons in one group to link them.
Checkboxes can have the same or different name depending on how you want to collect data.
Radio buttons let users pick only one option from a group.
Checkboxes let users pick any number of options, including none.
Labels improve usability and accessibility for both controls.
Practice
<input type="?">Solution
Step 1: Understand input types
Theradioinput type is designed for selecting one option from many.Step 2: Compare with other types
checkboxallows multiple selections,textis for typing, andbuttontriggers actions.Final Answer:
radio -> Option CQuick Check:
Single choice = radio [OK]
- Confusing checkbox with radio
- Thinking text input allows selection
- Using button for selection
Solution
Step 1: Check input and label usage
The correct checkbox uses<input type="checkbox">and a<label>with matchingforattribute.Step 2: Identify invalid syntax
The other options use invalid syntax: A uses a non-existent<checkbox>tag, B incorrectly closes the input with text inside, and D uses an invalidcheckboxattribute.Final Answer:
<input type="checkbox" id="sub"><label for="sub">Subscribe</label> -> Option BQuick Check:
Checkbox + label with for = correct syntax [OK]
- Using non-existent <checkbox> tag
- Missing type attribute
- Not linking label with for attribute
<form> <input type="radio" name="color" id="red" value="Red"><label for="red">Red</label> <input type="radio" name="color" id="blue" value="Blue" checked><label for="blue">Blue</label> <input type="radio" name="color" id="green" value="Green"><label for="green">Green</label> </form>
Solution
Step 1: Identify checked attribute
The Blue radio button has thecheckedattribute, so it is selected by default.Step 2: Understand radio group behavior
Only one radio button in a group (same name) can be selected at a time.Final Answer:
Only the Blue radio button is selected by default -> Option AQuick Check:
Checked attribute sets default selection [OK]
- Assuming multiple radios can be selected
- Ignoring the checked attribute
- Confusing radio with checkbox behavior
<input type="checkbox" id="news"><label for="news">News</label> <input type="checkbox" id="updates"><label>Updates</label>
Solution
Step 1: Check label associations
The first label usesfor="news"linking it to the checkbox with id "news".Step 2: Identify missing for attribute
The second label lacks aforattribute, so it is not linked to the checkbox with id "updates".Final Answer:
The second label is missing the for attribute linking it to the checkbox -> Option AQuick Check:
Label needs for attribute to link input [OK]
- Assuming name attribute is required for labels
- Thinking labels can't be used with checkboxes
- Placing id on label instead of input
Solution
Step 1: Understand selection rules
Users can pick multiple fruits, so checkboxes are suitable for fruits.Step 2: Single choice for size
Only one size can be selected, so radio buttons are suitable for sizes.Final Answer:
Checkboxes for fruits, Radio buttons for sizes -> Option DQuick Check:
Multiple choice = checkbox, single choice = radio [OK]
- Using radio buttons for multiple selections
- Using checkboxes for single selection
- Not grouping radio buttons by name
Ready to prove it?
Pass the test and this pattern moves unknown
