Lesson 24 of 36
HTML Tutorial

Checkboxes, Radios and Buttons

Offer multiple or single choices with checkboxes and radios, and send forms with the right kind of button.

4 min readBeginnerWeb FundamentalsHTML Forms

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.

html
<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.

html
<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:

typeBehaviour
submitSends the form. This is the default inside a form.
resetRestores every field to its initial value
buttonDoes nothing by itself; used with JavaScript
html
<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 reset buttons. 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 name values, 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 div styled 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

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-