Checkbox & Radio Buttons
Master selection controls with checkboxes and radio buttons. Learn their differences, proper usage, custom styling, accessibility features, and advanced interaction patterns.
What is the difference between checkboxes and radio buttons?
Checkboxes let users pick any number of options, each on or off independently. Radio buttons let users pick exactly one option from a group; radios belong to the same group when they share a name. Wrap each one in a <label> so clicking the text toggles it, and group them in a <fieldset> with a <legend>.
<fieldset>
<legend>Plan</legend>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>
<label><input type="checkbox" name="terms" required> I accept the terms</label>Checkbox vs Radio Buttons
Checkboxes and radio buttons are essential form controls for user selections. Understanding when and how to use each type is crucial for creating intuitive and accessible forms.
Checkboxes
- Multiple selections allowed
- Independent choices - each can be checked/unchecked
- Optional selections - none required
- Use for: Preferences, features, agreements
Radio Buttons
- Single selection only (mutually exclusive)
- Grouped choices - same name attribute
- One must be selected in group
- Use for: Categories, levels, exclusive options
Checkbox Deep Dive
Checkboxes allow users to select multiple options from a list. They're perfect for preferences, feature toggles, and multi-selection scenarios.
Checkbox States & Behavior
Unchecked (default)
Default state. Value not submitted with form.
Checked
Selected state. Value included in form submission.
Indeterminate
JavaScript-only state for partial selections.
Key Attributes
Required Attributes:
- type="checkbox" - Defines checkbox input
- name - Form field name
- value - Value sent when checked
Optional Attributes:
- checked - Default to checked
- disabled - Disable interaction
- required - Must be checked
Checkbox Patterns
1. Single Checkbox (Agreement)
<label><input type="checkbox" required> I agree to the terms</label>2. Multiple Checkboxes (Features)
<input type="checkbox" name="features" value="notifications">
<input type="checkbox" name="features" value="darkmode">3. Select All Pattern
Master checkbox that controls all others in a group using JavaScript.
Radio Button Mastery
Radio buttons provide mutually exclusive choices. When one is selected, all others in the same group are automatically deselected.
Radio Button Rules
Must Do:
- • Use same
namefor grouped options - • Provide unique
valuefor each option - • Always provide a
<label> - • Pre-select one option with
checked - • Use
<fieldset>and<legend>
Avoid:
- • Single radio button (use checkbox instead)
- • Different names in same group
- • Missing labels or poor accessibility
- • Too many options (consider select instead)
- • No default selection
Proper Labeling & Grouping
Fieldset Method (Recommended)
<fieldset>
<legend>Choose size:</legend>
<label><input type="radio" name="size" value="s"> Small</label>
<label><input type="radio" name="size" value="m"> Medium</label>
</fieldset>Label Association
<input type="radio" id="small" name="size" value="s">
<label for="small">Small</label>Mobile Considerations
Touch Targets:
- • Minimum 44px touch target
- • Adequate spacing between options
- • Large, tappable label areas
- • Visual feedback on tap
UX Tips:
- • Stack vertically on small screens
- • Use clear, concise labels
- • Consider alternative UI patterns
- • Test with real users
Interactive Demo
Explore checkboxes and radio buttons with custom styling, proper labeling, and accessibility features. See both default and custom-styled implementations.
Code Editor
<fieldset>
<legend>Plan (pick one)</legend>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>
<fieldset>
<legend>Topics (pick any)</legend>
<label><input type="checkbox" name="topic" value="html" checked> HTML</label>
<label><input type="checkbox" name="topic" value="css"> CSS</label>
</fieldset>
<p id="out"></p>Preview
Accessibility & Best Practices
Accessibility Features
- • Always provide descriptive labels
- • Use fieldset/legend for groups
- • Ensure keyboard navigation works
- • Provide clear focus indicators
- • Use ARIA labels when needed
- • Test with screen readers
UX Best Practices
- • Logical order and grouping
- • Clear, descriptive labels
- • Consistent styling patterns
- • Immediate visual feedback
- • Mobile-friendly touch targets
Common Pitfalls
- • Missing or unclear labels
- • Inconsistent grouping
- • Poor custom styling accessibility
- • Forgetting keyboard users
- • Too many options in one group
Styling Tips
- • Hide default input, style label
- • Maintain focus indicators
- • Use CSS transitions for smooth interactions
- • Ensure sufficient color contrast
- • Test across different browsers
Critical Accessibility Note
When customizing checkbox and radio button styles, never remove focus indicators or break keyboard navigation. Always test with actual assistive technologies and follow WCAG guidelines for interactive elements.
Selection Controls Mastered!
Excellent! You now understand checkboxes and radio buttons inside and out. Ready to explore different button types and their uses?
Checkboxes and Radio Buttons — step by step
Use checkbox for independent choices
Checkboxes allow users to select zero or more options independently. Each checkbox should have a unique name or share a name with other related checkboxes. Use the checked attribute to set a default checked state.
🎯 Quick Check
What attribute makes radio buttons mutually exclusive within a group?
HTML Checkboxes and Radio Buttons: summary
Key points
- An unchecked checkbox isn't submitted at all; a checked one sends its
value(default "on"). - Arrow keys move between radio buttons in the same group.
- Style custom checkboxes with
accent-colororappearance: nonewhile keeping the real input for accessibility.
Frequently asked questions
›Why can I select multiple radio buttons?
They don't share the same name. All radios in one group must have an identical name attribute.
›How do I change the color of a checkbox?
The easiest way is accent-color: #3b82f6; on the input.