Sub topic 10
Grouping form fields and their labels
This chapter assumes you know basic HTML forms, CSS selectors, the box model, rem units, and the spacing scale and gap property from earlier chapters.
Why forms need spacing rules
A form is a list of small pairs: a label and an input. Users must see which label belongs to which input, and which fields belong together. Borders and boxes can show this, but they add visual noise. Spacing can do the same job.
The rule comes from the proximity principle. Space between two items should match how closely they are related. The weaker the relationship, the larger the space.
Three levels of spacing
A typical form has three levels of relationship:
- A label and its input are one unit. They get the smallest gap.
- Fields inside one group (for example, street and city) are related, but each is a separate unit. They get a medium gap.
- Groups (shipping and billing) are separate topics. They get the largest gap.
Each step up in the hierarchy uses a larger token from your scale. Skipping a step in the scale, for example --space-1 to --space-3 to --space-5, makes the difference easy to see.
Implementing it with nested grids
Give each level its own container and set gap on that container. The values come from the spacing scale.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Address form</title>
<style>
:root {
--space-1: 0.25rem;
--space-3: 1rem;
--space-5: 2.5rem;
}
.form { display: grid; gap: var(--space-5); max-width: 28rem; }
.group { display: grid; gap: var(--space-3); }
.field { display: grid; gap: var(--space-1); }
</style>
</head>
<body>
<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>
</body>
</html>The gaps are 0.25rem, 1rem and 2.5rem. Each is clearly larger than the one below it, so the groups are visible without a single border.
Grid gap has two advantages here. It only adds space between children, so you do not need :last-child overrides. It also never collapses, so you do not have to think about margin collapsing inside the form.
Common errors
- Equal gaps everywhere. If label-to-input and field-to-field gaps are both 1rem, a label floats between two inputs and the pairing is ambiguous. Keep the label-to-input gap clearly smaller than the field-to-field gap.
- Margin on the label's top instead of the field's gap. This puts the space on the wrong side of the relationship and breaks when the label order changes. Put spacing on the container.
- Similar ratios. Gaps of 1rem and 1.25rem look like mistakes, not structure. Use values at least about 1.5 times apart.
- Spacing as the only cue. For radio buttons and checkboxes that answer one question, use
<fieldset>with a<legend>. Assistive technology announces the legend with each option, which spacing cannot do. The same spacing rules still apply to the fieldset's contents.
Checking the result
Blur your eyes, or apply a blur filter in the browser's developer tools. You should still see two blocks, each with evenly stacked rows. If a label seems to attach to the wrong input, reduce the label gap or increase the field gap.
Exercise
A form has labels 0.5rem above their inputs, and 0.5rem between one field and the next. Users sometimes fill in the wrong box. What is the cause, and which change fixes it using the scale --space-1: 0.25rem, --space-3: 1rem, --space-5: 2.5rem?
Show answer
The label-to-input gap equals the field-to-field gap, so proximity does not show which label goes with which input. Set the .field gap to --space-1 (0.25rem) and the gap between fields to --space-3 (1rem), and use --space-5 between groups.