Sub topic 10
Grouping form fields and their labels
This chapter assumes you know basic HTML form markup (label, input, fieldset), the CSS box model, rem units, and the spacing scale from the earlier chapter. The examples use the custom properties --space-1 through --space-6 defined there.
Why forms need visible structure
A form is a list of questions. Users read it by scanning, so each label has to attach to its own input, each field has to be separate from the next, and each block of related fields has to stand apart from other blocks. If the gaps are all the same, the page reads as one long stack and the user has to read every word to find the structure.
The proximity principle gives you a way out. Distance signals relationship, so you can use three different distances for three different relationships. You don't need borders or background colors to show the groups.
Three levels of spacing
A typical form has three nested relationships. Each one gets a larger gap than the one inside it.
| Relationship | Example | Spacing token |
|---|---|---|
| Label to its input | "Street" and the street text box | `--space-1` (0.25rem) |
| Field to field | Street field and City field | `--space-3` (1rem) |
| Group to group | Shipping address and Billing address | `--space-5` (2.5rem) |
The ratio matters more than the exact values. Moving from one level to the next should be a visible jump, roughly 2x to 4x. If the label gap is 0.25rem and the field gap is 0.5rem, the eye can't tell them apart. The values above give ratios of 4x and 2.5x.
The markup and CSS
The example below applies the three levels with nested grids. Each container sets only the gap for its own children, so no element needs a margin.
<form class="form">
<section class="group">
<h2>Shipping address</h2>
<div class="field">
<label for="street">Street</label>
<input id="street" name="street">
</div>
<div class="field">
<label for="city">City</label>
<input id="city" name="city">
</div>
</section>
<section class="group">
<h2>Billing address</h2>
<div class="field">
<label for="billing-street">Street</label>
<input id="billing-street" name="billing-street">
</div>
</section>
</form>
<style>
.form { display: grid; gap: var(--space-5); }
.group { display: grid; gap: var(--space-3); }
.field { display: grid; gap: var(--space-1); }
</style>Read the CSS from the outside in. .form spaces its groups 2.5rem apart. Inside each .group, the heading and the fields are 1rem apart. Inside each .field, the label sits 0.25rem above the input. A user sees three clusters of tightness without any lines drawn.
Because the spacing lives on the containers, adding a field means adding one div.field. The new field picks up the right gap automatically.
Why gap and not margins
Margins on label and input would work, but they couple each element to its context. A label with margin-bottom: 0.25rem also carries that margin into a sidebar or a table cell. A grid gap belongs to the layout container and only applies between children, so there is no stray space after the last item. It also avoids margin collapsing, which the earlier chapter covered.
The label must be closer to its own input
The most common form spacing error is a label that sits equidistant from the input above and the input below. The user then has to guess which label goes with which input.
Check this rule: the gap between a label and its own input must be clearly smaller than the gap between that input and the next label. In the example, 0.25rem is smaller than 1rem, so each label visibly belongs to the input beneath it.
Typical mistakes:
- Giving every element a uniform
margin-bottom: 1rem. This puts the label as far from its input as from the previous field. - Putting a large
padding-topon the input. This pushes the input away from its label. - Using
<br>elements or empty paragraphs for spacing. The gaps can't be tuned from one place, and screen readers may announce the empty elements.
Grouping with fieldset and legend
Spacing shows groups to sighted users. It doesn't tell assistive technology anything. For a set of related controls, especially radio buttons and checkboxes that answer one question, use fieldset and legend. A screen reader announces the legend before each control in the group, so the user hears the question with each option.
<fieldset class="group">
<legend>Delivery speed</legend>
<div class="choice">
<input type="radio" id="speed-standard" name="speed" value="standard">
<label for="speed-standard">Standard (5 days)</label>
</div>
<div class="choice">
<input type="radio" id="speed-express" name="speed" value="express">
<label for="speed-express">Express (2 days)</label>
</div>
</fieldset>
<style>
fieldset.group {
display: grid;
gap: var(--space-3);
border: 0;
margin: 0;
padding: 0;
min-inline-size: 0;
}
.choice {
display: flex;
gap: var(--space-2);
align-items: center;
}
</style>The reset removes the default border, margin and padding that browsers apply to fieldset, so your spacing scale controls the layout. min-inline-size: 0 fixes an old quirk: a fieldset defaults to a minimum width based on its content, which can make it overflow a narrow container.
Two other points. legend historically resisted being laid out as a grid child, so it is safest to treat it as a heading inside the fieldset and keep the container's gap in charge of the space below it. And for the choice rows above, the gap of --space-2 keeps each radio button close to its own label, closer than the gap between choices.
Use a fieldset only when it fits
Not every cluster of fields needs a fieldset. Use it when the controls share one question or one purpose, such as a set of radio buttons or a billing address. For the shipping address example earlier, a section with a heading works, but a fieldset with a legend would also be correct and gives screen reader users the group name on each control. Use fieldset for radios and checkboxes in all cases.
Spacing around related help text and errors
Hints and error messages belong to one field, so they should sit inside the .field container, closer to the input than the next field is.
<div class="field">
<label for="email">Email</label>
<input id="email" name="email" type="email" aria-describedby="email-hint">
<p class="hint" id="email-hint">We only use this for order updates.</p>
</div>
<style>
.field { display: grid; gap: var(--space-1); }
.hint { margin: 0; font-size: 0.875rem; }
</style>The .field gap of 0.25rem applies between label and input and between input and hint. The hint stays tied to its field. The aria-describedby attribute makes the same link for assistive technology. The margin: 0 on .hint overrides the default paragraph margins, which would otherwise add space on top of your gap.
Actions at the end of the form
The submit button is not another field. Separate it from the last group by at least the group gap, so it reads as the result of the whole form. Because .form is a grid with gap: var(--space-5), adding the button as a direct child of .form does this for free.
<form class="form">
<!-- groups from the earlier example -->
<div class="actions">
<button type="submit">Place order</button>
</div>
</form>
<style>
.actions { display: flex; gap: var(--space-3); }
</style>If you have a primary and a secondary action, keep their gap at field level (--space-3), since they are peers.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Label looks closer to the input above | Same margin above and below the label | Put the gap on `.field`, with a smaller gap than between fields |
| Extra space after the last field | Margin-bottom on every field | Use `gap` on the container |
| Hint text drifts away from its input | Hint placed outside the `.field` | Move it inside the same container |
| Fieldset overflows on a phone | Default `min-width: min-content` | Set `min-inline-size: 0` |
| Groups still unclear | Group gap too close to field gap | Make the group gap at least 2x the field gap |
Check your work
Apply the squint test from the final chapter now. Blur your eyes, or apply filter: blur(4px) to the form in the browser's dev tools. You should still see blocks: tight pairs of label and input, loose columns of fields, and wide channels between groups. If the form blurs into an even texture, increase the gap between levels.
Exercise
A form uses .field { display: grid; gap: 1rem; } and .form { display: grid; gap: 1rem; }. Users say they can't tell which label belongs to which input. Which values would you change, and why?
Show answer
Both the label-to-input gap and the field-to-field gap are 1rem, so the label is as far from its own input as from the previous field. Reduce .field to var(--space-1) (0.25rem) so the label sits close to its input. Keep the field-to-field gap at var(--space-3) (1rem) so the fields are clearly separate. If the form also has groups, set the group gap to var(--space-5) so it is clearly larger than the field gap.
Exercise
You add a p.hint below an input, and the hint appears about 1.25rem from the input even though .field has gap: 0.25rem. What is the likely cause, and how do you fix it?
Show answer
The browser's default margin on p (typically 1em) is added on top of the grid gap. Set margin: 0 on .hint so the container's gap alone controls the spacing.
Exercise
When should you use fieldset and legend rather than only a div with spacing?
Show answer
Use them when several controls answer one question, such as radio buttons or checkboxes, or when they form one named unit such as an address. Spacing shows the grouping to sighted users. The legend gives screen reader users the group name as they move through the controls.