The booking form already asks for everything it needs, but if you look at it through a customer's eyes you will see a long column of fields with no apparent order, and two groups of radio buttons titled with a stray paragraph which, for a screen reader, has no relationship whatsoever with the buttons. A good form is not just a list of controls: each field must have a label that says clearly what it is asking for, and related fields must be grouped under a title. In this lesson you will look more closely at label (its two forms and why it is essential), learn to group with fieldset and legend, to disable entire groups and to offer suggestions with datalist, and you will understand why example text inside a field cannot replace a label. By the end, The Blue Cup's form will be organised into three blocks: "Your details", "Your booking" and "Preferences".

Contents

  1. Why every control needs a label
  2. label with for and id
  3. Wrapping label
  4. Which form to choose and the rules of label
  5. Grouping fields: fieldset and legend
  6. Groups of radio buttons and checkboxes
  7. Disabling a whole group: disabled on fieldset
  8. Suggestions: datalist
  9. placeholder is no substitute for label
  10. The output element (in passing)
  11. Project: an organised booking form

Why every control needs a label

In Creating a Basic Form you used label so that clicking the text made the cursor jump to the field. That is only one of its advantages. A properly associated label does three jobs:

Job Who benefits Example
Describes the field visually Everyone "Phone" next to the box
Enlarges the clickable area Mobile users, people with a tremor or limited precision Tapping the text "Terrace" selects the radio button, which is tiny
Gives the control an accessible name Screen reader and voice control users The screen reader announces "Phone, edit text"; with voice control you can say "click Phone"

The third is the most important and the least visible. When a screen reader reaches a field, it reads its accessible name. If the field has no associated label, the screen reader only says "edit text" or "checkbox, not checked": the person doesn't know what they are filling in. Having the text "Phone" visually next to the field is not enough: the association has to be in the code.

You can check this in DevTools: select a field in the Elements tab and open the Accessibility pane (you used it with tables in Accessible Tables: caption, scope and headers). Under "Name" you will see the text of its label, or nothing if it doesn't have one.

label with for and id

This is the form you already know. The label and the field are separate elements, linked by the field's id:

<label for="phone">Phone</label>
<input type="tel" id="phone" name="phone">
  • for contains the control's id, never its name.
  • The label can be anywhere on the page: before, after or in another cell of a table. The link is made by the id.
  • It requires the control to have a unique id.

Wrapping label

The second form consists of putting the control inside the label:

<label>
  <input type="checkbox" name="highchair" value="yes">
  I need a highchair for a baby
</label>
  • You need neither for nor id: the control inside is the associated one.
  • The label covers both the text and the control, so the whole area is clickable.
  • It is very convenient for checkboxes and radio buttons.

You can also combine both (wrap and also use for/id); it isn't necessary, but it does no harm, and some older accessibility tools appreciated it.

Which form to choose and the rules of label

for + id Wrapping
Needs an id on the control Yes No
Label and control can be separate in the code Yes No
Makes it easy to place label and field in columns (table, CSS Grid) Yes Less so
Convenience for checkboxes and radio buttons Good Very good
Risk of error for and id that don't match Putting two controls in the same label

Both are correct. At The Blue Cup you will use for + id on every field, for consistency and because, when the form is integrated with the site's design in Module 11, having the label and the field separate will give you more freedom.

Rules you must follow:

  • One label, one control. A label cannot contain or point to two controls.
  • Don't nest labels: a label cannot contain another label.
  • Avoid putting other interactive elements (links, buttons) inside a label: when they are clicked, it isn't clear whether you are activating the link or the control. If you need a link (for example, to the privacy policy), put it outside the label, in nearby text.
  • A control can have several labels, although it is rarely a good idea.
  • Buttons (button, input type="submit") don't need a label: their text is already their name. Neither do input type="hidden" fields: they aren't visible.
  • The label text should be short and clear. Long explanations go in separate text next to the field.

Grouping fields: fieldset and legend

fieldset groups related controls, and legend gives the group a title:

<fieldset>
  <legend>Your details</legend>
  <p>
    <label for="name">Name</label>
    <input type="text" id="name" name="name">
  </p>
  <p>
    <label for="email">Email</label>
    <input type="email" id="email" name="email">
  </p>
</fieldset>
  • By default, the browser draws a frame around the group and places the legend on the top line of the frame. You will be able to change that appearance with CSS.
  • legend must be the first child of fieldset, and there can only be one.
  • Screen readers announce the legend on entering the group ("Your details, group"), and some repeat it before each field. That is why it is best kept short.
  • fieldset doesn't change what is sent: the fields still build their name=value pairs as always. It is purely about organisation and accessibility.
  • fieldset elements can be nested: one group inside another, each with its own legend.

Grouping helps everyone: a long form divided into titled blocks feels shorter, and the user knows which part they are in.

Groups of radio buttons and checkboxes

This is the most important use of fieldset. Remember the problem you left pending:

<!-- Before: the text "Area:" is not associated with anything -->
<p>Area:</p>
<p>
  <input type="radio" id="area-indoor" name="area" value="indoor" checked>
  <label for="area-indoor">Indoors</label>
  <input type="radio" id="area-terrace" name="area" value="terrace">
  <label for="area-terrace">Terrace</label>
</p>

A screen reader reaching the first button will say "Indoors, radio button, checked, 1 of 2". Indoors for what? The question "Area" is lost. The solution:

<!-- After: the question is the group's legend -->
<fieldset>
  <legend>Area</legend>
  <input type="radio" id="area-indoor" name="area" value="indoor" checked>
  <label for="area-indoor">Indoors</label>
  <input type="radio" id="area-terrace" name="area" value="terrace">
  <label for="area-terrace">Terrace</label>
</fieldset>

Now the screen reader announces "Area, group. Indoors, radio button, checked, 1 of 2". Each button has its label (the option) and the group has its legend (the question):

graph TD
    F["fieldset"] --> L["legend: the question (Area)"]
    F --> R1["input radio + label: Indoors"]
    F --> R2["input radio + label: Terrace"]

Rule of thumb: every group of radio buttons, and every group of checkboxes answering the same question (such as the allergens in the previous lesson), goes inside its own fieldset with a legend. A single checkbox on its own, like the privacy one, doesn't need it: its label already says it all.

Disabling a whole group: disabled on fieldset

The boolean attribute disabled disables a control: it appears greyed out, it cannot be used or receive focus and it is not sent. If you put it on a fieldset, it disables all the controls it contains:

<fieldset disabled>
  <legend>January bread-making workshop (fully booked)</legend>
  <p>
    <label for="january-places">Number of places</label>
    <input type="number" id="january-places" name="january_places" min="1" max="4">
  </p>
  <p>
    <label for="level">Your level</label>
    <select id="level" name="level">
      <option value="beginner">Beginner</option>
      <option value="intermediate">Intermediate</option>
    </select>
  </p>
</fieldset>
  • The whole block appears greyed out and cannot be filled in.
  • Nothing it contains is sent.
  • A curious exception: controls inside the legend are not disabled.

In practice, adding or removing disabled depending on what the user chooses (for example, enabling "Area" only if they are booking a table) requires JavaScript. In a form without JavaScript, like the one in this course, it is more useful for fixed cases like the one in the example. In Form Attributes and Validation you will compare disabled with its relative readonly.

Suggestions: datalist

Sometimes you want to suggest answers without forcing the user to choose one of them. That is what datalist is for: a list of options associated with an input through the list attribute:

<p>
  <label for="occasion">Are you celebrating anything? (optional)</label>
  <input type="text" id="occasion" name="occasion" list="occasions">
  <datalist id="occasions">
    <option value="Birthday">
    <option value="Anniversary">
    <option value="Farewell party">
    <option value="Work meeting">
    <option value="Christening or first communion">
  </datalist>
</p>
  • The input's list attribute contains the id of the datalist (like for with label).
  • Each option carries the suggested text in value. Here the option elements need neither content nor a closing tag.
  • When the user clicks or starts typing, the browser shows the matching suggestions ("Bir…" → "Birthday").
  • The user can choose one or type anything else ("Retirement"). Whatever is in the field is sent: occasion=Work+meeting.
  • The datalist is not visible on the page and is not sent; it only feeds the suggestions.

datalist versus select

select input + datalist
The user can type their own value No Yes
What is sent The option's value The field's text
Guarantees a value from the list Yes No
Typical use Closed options Open suggestions (cities, event types)

datalist also works with other types, such as email, url, number, time or range (with the latter, some browsers draw tick marks on the slider). The appearance of the suggestion list is decided by each browser and is not very customisable.

placeholder is no substitute for label

The placeholder attribute displays example text, in grey, inside the empty field, which disappears as soon as you start typing:

<!-- Wrong: the placeholder acts as the label -->
<input type="tel" name="phone" placeholder="Phone">

<!-- Right: a visible label + an example of the format -->
<label for="phone">Phone</label>
<input type="tel" id="phone" name="phone" placeholder="600 000 000">

It is tempting to use it as a label because it saves space, but it is a mistake:

  • It disappears when you type: if you are interrupted or review the form before submitting, you no longer know what each field was asking for.
  • It looks like a field that has already been filled in, and some people skip it.
  • It usually has poor contrast (light grey), which is hard for many people to read.
  • It is not a reliable accessible name: not all screen readers or translation tools treat it the same way as a label.
  • It is not sent: if the user doesn't type anything, the field is sent empty.

Use placeholder, at most, for an example of the format that complements the label. You will come back to it in Form Attributes and Validation.

The output element (in passing)

output represents the result of a calculation or of a user action. For example, displaying the number chosen on a range slider, which the user can't see:

<form oninput="result.value = strength.value">
  <label for="strength">Coffee strength</label>
  <input type="range" id="strength" name="strength" min="1" max="5" value="3">
  <output name="result" for="strength">3</output>
</form>
  • for indicates which controls the result depends on (it accepts several id values separated by spaces).
  • For the number to update you need JavaScript; here, one line in the form's oninput attribute. JavaScript is outside the scope of this course.
  • output is not sent with the form, even if it has a name.
  • Screen readers announce its changes, which makes it preferable to a plain span for displaying results.

Project: an organised booking form

Reorganise the form in bookings.html into three blocks. Replace the whole form with this one:

    <!-- Booking form: method="get" is temporary so we can see the data in the URL; it will switch to post in lesson 05-06 -->
    <form action="https://forms.thebluecup.example/bookings" method="get">
      <input type="hidden" name="form" value="web-booking">

      <fieldset>
        <legend>Your details</legend>
        <p>
          <label for="name">Name</label>
          <input type="text" id="name" name="name">
        </p>
        <p>
          <label for="email">Email</label>
          <input type="email" id="email" name="email">
        </p>
        <p>
          <label for="phone">Phone</label>
          <input type="tel" id="phone" name="phone">
        </p>
      </fieldset>

      <fieldset>
        <legend>Your booking</legend>
        <fieldset>
          <legend>What would you like to book?</legend>
          <input type="radio" id="type-table" name="type" value="table" checked>
          <label for="type-table">A table</label>
          <input type="radio" id="type-workshop" name="type" value="workshop">
          <label for="type-workshop">A place on the bread-making workshop (first Saturday of every month, 10:00–13:00)</label>
        </fieldset>
        <p>
          <label for="date">Date</label>
          <input type="date" id="date" name="date">
        </p>
        <p>
          <label for="time">Time</label>
          <input type="time" id="time" name="time">
        </p>
        <p>
          <label for="guests">Number of guests</label>
          <input type="number" id="guests" name="guests" min="1" max="12" step="1" value="2">
        </p>
        <p>
          <label for="occasion">Are you celebrating anything? (optional)</label>
          <input type="text" id="occasion" name="occasion" list="occasions">
          <datalist id="occasions">
            <option value="Birthday">
            <option value="Anniversary">
            <option value="Farewell party">
            <option value="Work meeting">
            <option value="Christening or first communion">
          </datalist>
        </p>
      </fieldset>

      <fieldset>
        <legend>Preferences</legend>
        <fieldset>
          <legend>Area (table bookings only)</legend>
          <input type="radio" id="area-indoor" name="area" value="indoor" checked>
          <label for="area-indoor">Indoors</label>
          <input type="radio" id="area-terrace" name="area" value="terrace">
          <label for="area-terrace">Terrace</label>
        </fieldset>
        <p>
          <label for="comments">Comments (allergies, highchairs…)</label><br>
          <textarea id="comments" name="comments" rows="4" cols="40"></textarea>
        </p>
      </fieldset>

      <p>
        <input type="checkbox" id="privacy" name="privacy" value="accept">
        <label for="privacy">I have read and accept the privacy policy</label>
      </p>

      <p>
        <button type="submit">Send booking</button>
      </p>
    </form>

What has changed:

  • Three blocks with fieldset and legend: "Your details" (who you are), "Your booking" (what, when, how many) and "Preferences" (how). The customer understands the structure at a glance.
  • The two groups of radio buttons are in nested fieldset elements, with the question as the legend. The paragraphs "What would you like to book?" and "Area:", which weren't associated with anything, have gone.
  • The type of occasion is requested with a datalist: it suggests the usual answers without ruling out others. That is why the comments no longer mention "celebrations".
  • Every field has a label with for, and every for matches its id.
  • The privacy checkbox and the button stay outside the blocks: they affect the whole form.

Check your work:

  1. Click each label, including "Terrace" and "I have read and accept…": each one should activate its control.
  2. In DevTools, select the "Terrace" button and look at the Accessibility pane: its name should be "Terrace", and in the accessibility tree it should appear inside a group called "Area (table bookings only)".
  3. Go through the form with Tab: the focus moves through the fields in order. Within a group of radio buttons, you change option with the arrow keys, not with Tab.
  4. Submit and check that the URL contains the same pairs as before, plus occasion if you filled it in: the fieldset elements don't change what is sent.

Common Mistakes and Tips

  • A for that doesn't match the id: the label is displayed, but it isn't associated. Always do the click test.
  • Putting the name in for: for always points to the id.
  • Using placeholder as the only label: it disappears when you type and is not reliably accessible.
  • Groups of radio buttons without a fieldset: the screen reader announces the options without the question.
  • Very long legend elements: some screen readers repeat the legend for every field. Keep it short and clear.
  • A legend that isn't the first child of the fieldset: the browser won't treat it as a legend.
  • Links inside label: put them in separate text, close to the control.
  • Expecting datalist to restrict the answers: it is only a suggestion; if you need a closed list, use select.
  • fieldset for everything: a group for every single field doesn't help. Group what belongs together.
  • Tip: write labels thinking about how they will sound on their own, without seeing the page: "Date" is fine inside "Your booking"; "Yes" as the label of a checkbox says nothing.

Exercises

Exercise 1. Rewrite this snippet so that it is accessible, using the wrapping form of label for the checkboxes:

<p>Which allergens should we take into account?</p>
<input type="checkbox" name="allergens" value="gluten"> Gluten
<input type="checkbox" name="allergens" value="milk"> Milk
<input type="checkbox" name="allergens" value="nuts"> Nuts

Exercise 2. In contact.html you want to ask for the town or city the customer is writing from, suggesting "Fictional Town", "Example Village" and "Model Town", but allowing any other. Write the complete field with its label.

Exercise 3. This form has four labelling or grouping problems. Find them:

<fieldset>
  <p>Contact details</p>
  <legend>Contact</legend>
  <input type="text" id="name" name="name" placeholder="Name">
  <label for="email">Email</label>
  <input type="email" id="email-address" name="email">
  <label>
    <input type="radio" name="via" value="email"> Email
    <input type="radio" name="via" value="phone"> Phone
  </label>
</fieldset>

Solutions

Solution 1.

<fieldset>
  <legend>Which allergens should we take into account?</legend>
  <label><input type="checkbox" name="allergens" value="gluten"> Gluten</label>
  <label><input type="checkbox" name="allergens" value="milk"> Milk</label>
  <label><input type="checkbox" name="allergens" value="nuts"> Nuts</label>
</fieldset>
  • The question becomes the group's legend: now it is associated with the checkboxes.
  • Each checkbox, with its text, goes inside its own label: the text is clickable and is the accessible name. No id values are needed.

Solution 2.

<p>
  <label for="city">Town or city</label>
  <input type="text" id="city" name="city" list="cities">
  <datalist id="cities">
    <option value="Fictional Town">
    <option value="Example Village">
    <option value="Model Town">
  </datalist>
</p>

The field's list points to the id of the datalist. If the user types "Newtown", city=Newtown will be sent.

Solution 3.

  1. legend is not the first child of the fieldset (there is a p before it). Besides, the p "Contact details" is unnecessary: the legend is already the title.
  2. The name field has no label: only a placeholder. It needs <label for="name">Name</label>.
  3. label for="email" points to an id that doesn't exist: the field has id="email-address". They need to match.
  4. A single label wraps two radio buttons. Each one needs its own label, and the group needs its own fieldset with a legend such as "How would you prefer us to contact you?".

Corrected:

<fieldset>
  <legend>Contact</legend>
  <p>
    <label for="name">Name</label>
    <input type="text" id="name" name="name">
  </p>
  <p>
    <label for="email">Email</label>
    <input type="email" id="email" name="email">
  </p>
  <fieldset>
    <legend>How would you prefer us to contact you?</legend>
    <label><input type="radio" name="via" value="email"> Email</label>
    <label><input type="radio" name="via" value="phone"> Phone</label>
  </fieldset>
</fieldset>

Conclusion

In this lesson you have learnt that a label is not decoration: label, whether in its for/id form or wrapping the control, gives the control its accessible name, enlarges the clickable area and makes the form understandable without seeing it. You have grouped fields with fieldset and legend, especially groups of radio buttons, disabled entire blocks with disabled, offered open suggestions with datalist, seen why placeholder cannot replace a label and met output for displaying results. The booking form is now organised into "Your details", "Your booking" and "Preferences". But it still accepts anything: it can be submitted without a name, with a date from last year, with a three-digit phone number or without accepting the privacy policy. In the next lesson, Form Attributes and Validation, you will learn to set rules for each field so that the browser helps the user correct mistakes before submitting.

HTML Course

Module 1: Introduction to HTML

Module 2: Text Formatting in HTML

Module 3: Links and Media in HTML

Module 4: Tables in HTML

Module 5: Forms in HTML

Module 6: HTML5 Semantic Elements

Module 7: Advanced HTML Techniques

Module 8: Integrating HTML and CSS

Module 9: Responsive Web Design

Module 10: Best Practices and Accessibility

Module 11: Project: Building a Complete Website

© Copyright 2026. All rights reserved