Skip to content
Tutorials Center
My learning

Topics

Lesson 66 of 122 ยท Forms

Checkboxes and Radio Buttons

Learn how checkboxes and radio buttons submit their values, how to build groups, how to make a choice required, and how to restyle them with accent-color.

Checkboxes and radio buttons let people choose instead of type. A checkbox answers a yes/no question, or picks any number of options from a list. A radio button picks exactly one option from a group, like the preset buttons on an old car radio: pressing one pops the others out.

They look simple, but they follow submission rules that surprise many developers. In this lesson you will learn what they send, how grouping works, and how to make a choice required.

Checkboxes

A checkbox is an <input type="checkbox"> with a label. Put the label after the box, which is where people expect it:

HTML
<input type="checkbox" id="terms" name="terms" value="accepted">
<label for="terms">I accept the terms of service</label>

Three attributes control what it does:

  • name is the key that is submitted.
  • value is what is submitted when the box is checked. If you leave it out, the browser sends the word on.
  • checked makes the box start out checked. It is a boolean attribute, so its presence is enough.

Unchecked boxes send nothing

This is the rule that surprises people: an unchecked checkbox is not submitted at all. The server does not receive terms=off or terms= with an empty value. The key is simply missing.

So on the server, "missing" means "unchecked". When you need an explicit value either way, a common trick is a hidden input with the same name before the checkbox:

HTML
<input type="hidden" name="newsletter" value="no">
<input type="checkbox" id="newsletter" name="newsletter" value="yes">
<label for="newsletter">Email me the newsletter</label>

If the box is unchecked, only newsletter=no is sent. If it is checked, both pairs are sent, and many server frameworks keep the last value, yes. Check how your server handles repeated names before you rely on this.

A group of checkboxes

When people may pick several options from a list, give every checkbox the same name and a different value. Wrap the group in a <fieldset> with a <legend> that asks the question, as you learned in Labels, Fieldsets and Legends:

Checkbox groups

Check a few toppings and press Order. You get one topping= line per checked box, and nothing for the others. Tick "Extra cheese" and you will see extra_cheese=on, because that box has no value. Then uncheck everything: the output shows "(no data)".

On the server, repeated keys arrive as a list. Some frameworks, such as PHP, want the name to end in square brackets (name="topping[]") to collect them into an array. That is a server convention, not an HTML rule.

Radio buttons

Radio buttons always come in groups. The shared name is what makes them a group: within one form, only one radio button per name can be checked at a time. Checking one automatically unchecks the others.

HTML
<fieldset>
  <legend>Delivery speed</legend>

  <input type="radio" id="speed-standard" name="speed" value="standard" checked>
  <label for="speed-standard">Standard (3โ€“5 days)</label>

  <input type="radio" id="speed-express" name="speed" value="express">
  <label for="speed-express">Express (next day)</label>
</fieldset>

Only the checked radio button is submitted, so the server receives a single pair such as speed=express. Always give each radio button a meaningful value; without one, the server just receives speed=on and cannot tell which option was picked.

Keyboard behavior

Radio groups have their own keyboard pattern, and the browser provides it for free:

  • Tab moves into the group once, landing on the checked option (or the first one if none is checked), and Tab again leaves the group.
  • The arrow keys move between options in the group, checking each one as they go.

That is why the shared name matters so much. Radio buttons with different names are separate groups, and each one becomes its own stop in the tab order.

Should one option start checked?

A checkbox with no starting value is fine: unchecked means "no". A radio group with nothing checked is different, because once someone picks an option there is no way to go back to "nothing" without a reset. Consider these guidelines:

  • When there is a sensible default, such as standard delivery, check it with checked.
  • When choosing the default for people would be presumptuous or unsafe, such as their title or a medical answer, leave all options unchecked and make the group required.
  • When "none" is a valid answer, add an explicit option for it, such as "No preference".

Required choices

The required attribute works on both kinds of control, but it means different things:

  • On a checkbox, required means this box must be checked. That fits a single "I agree to the terms" box. It does not mean "at least one box in the group", because HTML has no built-in way to require one of several checkboxes. You need a short script for that, using the techniques from Form Validation.
  • On a radio button, required makes the whole group required: one option with that name must be checked. Putting required on one radio button in the group is enough, though many developers add it to all of them for clarity.
Required radio groups and checkboxes

Press Join straight away. The browser stops you at the radio group, then at the checkbox. Try the arrow keys inside the radio group, too.

Styling with accent-color

The default look of checkboxes and radio buttons comes from the browser and operating system. For years, changing their color meant hiding the real input and drawing a fake one with CSS. Today one property does most of the job:

HTML
<style>
  form {
    accent-color: #0f766e;
  }
</style>

accent-color sets the highlight color of checkboxes, radio buttons, range sliders and progress bars. Browsers automatically pick a contrasting color for the check mark. You can set it on a single input or, as here, on a parent so every control inside inherits it. You can learn more about colors in Colors.

Make the target bigger, not just prettier. Small checkboxes are hard to hit on a touch screen. Because a connected label is clickable, simply giving the label some padding enlarges the target. You can also scale the inputs with width and height in CSS.

The checked state in CSS and JavaScript

The checked attribute only sets the starting state. Once someone clicks, the live state lives in the element's checked property. You can read it in JavaScript (box.checked), and style it in CSS with the :checked pseudo-class:

HTML
<style>
  input:checked + label {
    font-weight: bold;
  }
</style>

Checkboxes also have a third visual state, indeterminate, shown as a dash. It is used for a "select all" box when only some items are selected. There is no HTML attribute for it; you set it from JavaScript with box.indeterminate = true. An indeterminate checkbox still submits based on its checked state.

Checkbox, radio or something else?

  • One independent yes/no question: a single checkbox.
  • Pick any number from a short list: a group of checkboxes with a shared name.
  • Pick exactly one from a short list (roughly two to seven options): radio buttons.
  • Pick one from a long list, such as a country: a drop-down, covered next in Drop-downs and Suggestions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A checkbox named gift_wrap is left unchecked. What does the server receive for it?

    Unchecked checkboxes are not submitted. The key is simply missing from the data.
  2. 2. A checked checkbox has a name but no value. What is sent?

    Without a value attribute, a checked checkbox or radio button sends the word on.
  3. 3. What turns several radio buttons into one group where only one can be checked?

    The shared name defines the group. A fieldset helps people understand the group but does not change the behavior.
  4. 4. You add required to one radio button in a group. What does it mean?

    On radio buttons, required applies to the whole group with that name. On a checkbox, by contrast, it means that specific box must be checked.

Summary

  • A checked checkbox or radio button sends name=value; without a value it sends on.
  • Unchecked boxes send nothing, so a missing key means "unchecked".
  • Checkboxes with a shared name let people pick several values; the server receives a list.
  • Radio buttons with a shared name form a group in which only one can be checked; arrow keys move within it.
  • required on a checkbox means "must be checked"; on a radio button it means "one in the group must be chosen".
  • Wrap groups in <fieldset> with a <legend>, and recolor them with accent-color.

Next up: Drop-downs and Suggestions, for choices from longer lists.