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
- Why every control needs a label
labelwithforandid- Wrapping
label - Which form to choose and the rules of
label - Grouping fields:
fieldsetandlegend - Groups of radio buttons and checkboxes
- Disabling a whole group:
disabledonfieldset - Suggestions:
datalist placeholderis no substitute forlabel- The
outputelement (in passing) - 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:
forcontains the control'sid, never itsname.- 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:
- You need neither
fornorid: 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
labelcannot contain or point to two controls. - Don't nest labels: a
labelcannot contain anotherlabel. - 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 alabel: their text is already their name. Neither doinput 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.
legendmust be the first child offieldset, 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.
fieldsetdoesn't change what is sent: the fields still build theirname=valuepairs as always. It is purely about organisation and accessibility.fieldsetelements can be nested: one group inside another, each with its ownlegend.
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
legendare 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'slistattribute contains theidof thedatalist(likeforwithlabel). - Each
optioncarries the suggested text invalue. Here theoptionelements 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
datalistis 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>forindicates which controls the result depends on (it accepts severalidvalues separated by spaces).- For the number to update you need JavaScript; here, one line in the form's
oninputattribute. JavaScript is outside the scope of this course. outputis not sent with the form, even if it has aname.- Screen readers announce its changes, which makes it preferable to a plain
spanfor 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
fieldsetandlegend: "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
fieldsetelements, with the question as thelegend. 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
labelwithfor, and everyformatches itsid. - The privacy checkbox and the button stay outside the blocks: they affect the whole form.
Check your work:
- Click each label, including "Terrace" and "I have read and accept…": each one should activate its control.
- 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)".
- 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 withTab. - Submit and check that the URL contains the same pairs as before, plus
occasionif you filled it in: thefieldsetelements don't change what is sent.
Common Mistakes and Tips
- A
forthat doesn't match theid: the label is displayed, but it isn't associated. Always do the click test. - Putting the
nameinfor:foralways points to theid. - Using
placeholderas 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
legendelements: some screen readers repeat the legend for every field. Keep it short and clear. - A
legendthat isn't the first child of thefieldset: the browser won't treat it as a legend. - Links inside
label: put them in separate text, close to the control. - Expecting
datalistto restrict the answers: it is only a suggestion; if you need a closed list, useselect. fieldsetfor 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"> NutsExercise 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. Noidvalues 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.
legendis not the first child of thefieldset(there is apbefore it). Besides, thep"Contact details" is unnecessary: the legend is already the title.- The name field has no
label: only aplaceholder. It needs<label for="name">Name</label>. label for="email"points to anidthat doesn't exist: the field hasid="email-address". They need to match.- A single
labelwraps two radio buttons. Each one needs its own label, and the group needs its ownfieldsetwith alegendsuch 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
- What Is HTML?
- Setting Up Your Work Environment
- Basic Structure of an HTML Document
- Tags, Elements and Attributes
- Creating Your First HTML Page
- File Organisation and Paths
Module 2: Text Formatting in HTML
- Headings and Paragraphs
- Text Formatting Tags
- Lists: Ordered, Unordered and Description Lists
- Block Quotes and Preformatted Text
- Block and Inline Elements: div and span
Module 3: Links and Media in HTML
- Creating Hyperlinks
- Special Links: Anchors, Email, Phone and Downloads
- Adding Images
- Embedding Videos
- Using the Audio Element
Module 4: Tables in HTML
- Basic Table Structure
- Table Head, Body and Foot
- Merging Cells: colspan and rowspan
- Accessible Tables: caption, scope and headers
- Styling Tables
Module 5: Forms in HTML
- Creating a Basic Form
- Form Elements: input, textarea and select
- HTML5 Input Types
- Labels and Grouping: label, fieldset, legend and datalist
- Form Attributes and Validation
- Submitting Forms
Module 6: HTML5 Semantic Elements
- Introduction to Semantic HTML
- header, nav and footer
- article, section and aside
- main and figure
- Other Semantic Elements: time, address, details and dialog
Module 7: Advanced HTML Techniques
- HTML Entities
- Using iframes
- Data Attributes
- Metadata: meta, SEO and Open Graph
- Microdata and Structured Data
- Graphics in HTML: SVG and canvas
Module 8: Integrating HTML and CSS
- Linking CSS to HTML
- Inline, Internal and External CSS
- Basic CSS Selectors
- The Box Model
- Styling HTML Elements
- Introduction to Layout with Flexbox and Grid
Module 9: Responsive Web Design
Module 10: Best Practices and Accessibility
- HTML Best Practices
- Validating HTML Code
- Web Accessibility Basics
- Using ARIA Roles and Attributes
- Accessibility Testing
- Performance and Resource Loading
