When the visitor must choose among a few options, checkboxes and radio buttons are clearer than a drop-down because every option is visible at once. Buttons then turn the filled-in form into an action.
Checkboxes: pick any number#
Each checkbox is independent. Users can tick none, one or several.
<fieldset>
<legend>Add-ons for your drink</legend>
<label>
<input type="checkbox" name="addons" value="extra-shot">
Extra shot
</label>
<label>
<input type="checkbox" name="addons" value="oat-milk" checked>
Oat milk
</label>
<label>
<input type="checkbox" name="addons" value="cinnamon">
Cinnamon
</label>
</fieldset>Sharing the same name sends every ticked box under that name. An unticked checkbox sends nothing at all. The checked attribute pre-ticks a box. A single checkbox, such as "I agree to the terms", works on its own and is often required.
Radio buttons: pick exactly one#
Radios are grouped by sharing the same name. Selecting one automatically deselects the others in the group.
<fieldset>
<legend>Cup size</legend>
<label>
<input type="radio" name="size" value="small">
Small
</label>
<label>
<input type="radio" name="size" value="medium" checked>
Medium
</label>
<label>
<input type="radio" name="size" value="large">
Large
</label>
</fieldset>Add required to any radio in the group to force a choice. Good practice is to pre-select the safest default or to leave all unselected only when a choice is truly needed, because users cannot deselect a radio once picked. Keyboard users move between radios in a group with the arrow keys.
Buttons#
The <button> element is the preferred way to create buttons. Its type attribute matters a great deal:
type | Behaviour |
|---|---|
submit | Sends the form. This is the default inside a form. |
reset | Restores every field to its initial value |
button | Does nothing by itself; used with JavaScript |
<button type="submit">Place order</button>
<button type="reset">Clear the form</button>
<button type="button" id="help">Need help?</button>Because the default is submit, a helper button inside a form that is not meant to send anything should be given type="button". A <button> can contain text, icons and images, unlike <input type="submit">, which only accepts a plain value. Both work, but <button> is more flexible.
Button good practice#
- Write clear text such as "Send message" or "Add to order", never just "Click" or "Submit".
- Avoid
resetbuttons. They are rarely wanted and easy to click by accident. - Disable a button only if you explain why, since a greyed-out button is hard to understand.
- Make sure the click target is large enough for fingers.
Common mistakes#
- Giving radios different
namevalues, so they behave like checkboxes. - Expecting an unticked checkbox to submit "false".
- Forgetting
value, so the server receives the default "on". - Omitting
type="button"on non-submit buttons inside forms. - Using a
divstyled like a button, which is not focusable by default.
Try it yourself#
Build an "Order a drink" form with a fieldset of radios for size, a fieldset of checkboxes for add-ons and a required checkbox for "I confirm my order". Add a submit button and a reset button. Use a GET form temporarily and read the query string for a ticked and an unticked checkbox to observe the difference.
Recap#
Checkboxes allow several independent choices, while radios sharing a name allow exactly one. Always provide a value and label them inside a fieldset. Use button with an explicit type, and choose descriptive text.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -