Sub topic 2
Grouping, ordering, and layout
This chapter assumes you know the basic HTML form elements (input, label, select, button), can read simple JavaScript or TypeScript, and have seen the anatomy of a single field: a visible label, a hint, and an error message. Here we move up a level. We look at how fields relate to each other, in what order they appear, and how they are arranged on the page.
Why grouping matters
A form with fifteen loose fields is hard to scan, hard to fill in, and hard to fix when something goes wrong. People do not read a form top to bottom like prose. They look for the next thing they need to answer. Grouping gives them landmarks.
Good grouping does three jobs:
- It shows which fields belong together, such as the parts of an address.
- It lets you split a long form into steps later without rewriting the markup.
- It tells assistive technology that a set of controls shares a purpose.
Chapter 7 covers deciding what goes in each step. This chapter covers the structure inside one step or one page.
Group related fields by task, not by database table
Group fields by what the user thinks they are doing. A developer's data model often differs from the user's mental model. A users table might hold email, password, display name, and billing country in one row. A user sees "Sign in details", "About you", and "Where you are billing from".
A useful test is to describe each group in a short phrase a user would say aloud. If you need a technical word to name it, the group is probably shaped by your schema.
| Grouping basis | Example | Result |
|---|---|---|
| Database table | "User record" | Unrelated questions sit together |
| Task or topic | "Delivery address" | Questions share a purpose and context |
| Order of data entry in a legacy system | "Screen 2" | The form copies old limits |
Use fieldset and legend for sets that need a shared question
HTML provides fieldset and legend for a set of controls that answer one shared question. The legend is the question, and each control inside has its own label.
The clearest case is a radio group. Each radio button's label ("Ship to me", "Pick up in store") makes sense only with the question ("How do you want to receive your order?"). Without a fieldset, a screen reader announces the option and loses the question.
<form>
<fieldset>
<legend>How do you want to receive your order?</legend>
<div>
<input type="radio" id="delivery-ship" name="delivery" value="ship" checked>
<label for="delivery-ship">Ship to me</label>
</div>
<div>
<input type="radio" id="delivery-pickup" name="delivery" value="pickup">
<label for="delivery-pickup">Pick up in store</label>
</div>
</fieldset>
</form>Use fieldset for:
- Radio button groups.
- Checkbox groups that answer one question, such as "Which topics interest you?"
- Multi-part answers, such as a date of birth split into day, month, and year, or an address block.
Do not wrap every group of fields in a fieldset. A fieldset adds a spoken prefix: a screen reader typically reads the legend before each control in the group. If the legend is "Billing address" and the field is "City", the user hears both. That is useful for an address and noisy for a group of three unrelated fields. When the fields do not share a question, use a heading and a plain container instead.
Common errors with fieldset
- A legend that is too long. The legend is repeated for every control, so keep it short. Put long explanations in a hint, not in the legend.
- Nested fieldsets that repeat context. Two levels can work, but screen readers may read both legends before each field. Prefer one level.
- Styling that removes the legend. Hiding the legend visually with
display: nonealso removes it from the accessibility tree. If you need it hidden on screen, use a visually hidden technique that keeps it readable by assistive technology. Showing it is almost always better. - Using
fieldsetonly for a border. Borders are a visual choice. Use CSS on adivif you want a box with no shared question.
Use headings for sections
For sections that are larger than a question, use headings. A long page with sections such as "Contact details" and "Payment" should have real heading elements (h2, h3) so that people can jump between sections with a screen reader and so that the document outline makes sense.
A section can contain several fieldset elements. For example, a "Delivery" section with an h2 might contain a fieldset for the delivery method and a second fieldset for the address.
Ordering fields
Order is part of the design. Three rules cover most cases.
- Follow the user's thinking, not your system's. Ask for the easy and familiar things first (name, email) and the effortful ones later (documents, long free text). Early success builds momentum.
- Put dependent fields right after what they depend on. A "Company name" field that appears only for business accounts belongs directly after the "Account type" question. Chapter 6 covers the logic for showing and hiding such fields.
- Keep the source order the same as the visual order. Keyboard users move through fields in DOM order. If CSS moves a field to a different place on screen, the Tab key jumps around in a way that looks random.
Order and sensitive questions
Place questions that feel intrusive after you have explained why you need them, and after simpler questions. Add a short hint explaining the reason ("We ask for your phone number only to contact you about delivery"). Do not hide required information at the end of a long form, because users who reach the end and fail validation on a late field often give up.
Avoid unnecessary fields
Every field has a cost. Before you place a field, ask whether the flow works without it. Remove it, make it optional, or move it to a later point in the user's relationship with the product. Reducing the number of fields is the most effective layout change available.
Layout: one column by default
Single-column layouts are usually faster to complete and easier to get right. The eye moves in one direction, and the order of the DOM matches the order on screen. Multi-column layouts raise questions: does the user read across or down? Which field comes next?
Use more than one column only when fields are naturally short and clearly related. Good examples are:
- City, state, and postal code on one row.
- Day, month, and year in a date.
- Expiry date and security code on a card.
Even then, make the width of each field match the length of the expected answer. A postal code field the width of the whole form suggests a long answer, and a very narrow name field hides the text the user typed.
Label placement
Put labels above fields. Labels above inputs work at every screen width, allow long labels to wrap, and keep the label and field close together. Labels to the left of fields take horizontal space and often break on small screens.
Do not use placeholder text in place of a label. A placeholder disappears when the user types, so it cannot be checked later, and it often has poor contrast.
Spacing as grouping
Spacing tells the reader what belongs together. Use a small gap between a label and its field, a medium gap between fields in a group, and a larger gap between groups. If every gap is the same, the page reads as one long list.
.field { margin-block-end: 1rem; }
fieldset { margin-block-end: 2rem; border: 0; padding: 0; }
.field-short { max-inline-size: 8rem; }
.field-medium { max-inline-size: 20rem; }Required and optional fields
Say clearly which fields are required. Pick one convention and apply it across the form. Mark the exception: if most fields are required, mark the optional ones with the word "(optional)" in the label. If most are optional, mark the required ones. A bare asterisk needs an explanation at the top of the form, and some users will not know what it means. Words work better.
Also set the required attribute on required controls so the browser and assistive technology know about it.
Putting it together: a grouped section
The following example shows a section with a heading, a fieldset for the address, short fields sized to their content, and labels above the inputs. Each input has an autocomplete token so the browser can fill it in.
<section aria-labelledby="delivery-heading">
<h2 id="delivery-heading">Delivery</h2>
<fieldset>
<legend>Delivery address</legend>
<div class="field">
<label for="street">Street address</label>
<input id="street" name="street" type="text"
autocomplete="street-address" required>
</div>
<div class="field field-medium">
<label for="city">City</label>
<input id="city" name="city" type="text"
autocomplete="address-level2" required>
</div>
<div class="field field-short">
<label for="postal">Postal code</label>
<input id="postal" name="postal" type="text"
autocomplete="postal-code" required>
</div>
<div class="field">
<label for="notes">Delivery notes (optional)</label>
<textarea id="notes" name="notes" rows="3"></textarea>
</div>
</fieldset>
</section>The diagram below shows how the pieces relate in the page structure.
How grouping connects to later chapters
The structure you choose here pays off later.
- State. Groups map cleanly onto sections of form state, so touched and error tracking can be done per group.
- Steps. A well-defined group is the natural unit to move into a step. If a section is already a self-contained set of fields with its own heading, splitting it off is cheap.
- Error summaries. When validation fails, you can link each error back to its group.
- Accessibility. Legends, headings, and DOM order are the foundation for screen reader navigation.
Common mistakes
- Grouping by data model instead of by the user's task.
- Wrapping every set of fields in a
fieldset, which makes screen readers repeat noisy legends. - Using CSS to reorder fields so the visual order differs from the DOM order.
- Using placeholders as labels.
- Making all fields the same width regardless of expected answer length.
- Multi-column layouts that make the reading order unclear.
- Marking required fields with an unexplained asterisk.
Exercise
A checkout form has the question "Preferred contact method" with three radio buttons labelled "Email", "Phone", and "Post". The radios sit in a plain div with a bold paragraph above them. What is wrong for a screen reader user, and how do you fix it?
Show answer
A screen reader announces only "Email, radio button" and so on, without the question, because nothing associates the paragraph with the radios. Wrap the radios in a fieldset and replace the bold paragraph with a legend reading "Preferred contact method". Each radio keeps its own label. The legend is then announced with each option.
Exercise
A designer uses CSS grid to show the "Postal code" field before "City" on screen, while the HTML lists "City" first. Why is this a problem, and what is the safer fix?
Show answer
Keyboard and screen reader users follow DOM order, so Tab goes to "City" first even though "Postal code" appears first on screen. The focus appears to jump. The safer fix is to change the HTML order to match the intended visual order, rather than reordering with CSS.
Exercise
You have a section with three unrelated fields: "Nickname", "Preferred language", and "Time zone". Should you wrap them in a fieldset? Explain.
Show answer
Probably not. The fields do not answer one shared question, so a legend would be forced and a screen reader would repeat it before each field. Use a heading such as "Preferences" and a plain container instead, and keep a fieldset for radio groups, checkbox groups, and multi-part answers like an address or a date.