Sub topic 9
Aligning form labels and controls across fields
This chapter assumes you know CSS Grid basics (grid-template-columns, fr, gap, spanning with grid-column) and the subgrid keyword from earlier chapters. Here you apply it to forms. The same technique works for any list of rows that should share column widths, such as settings pages or key-value tables.
The problem: every field sizes its own columns
A typical form has a label and a control per field. You want all labels in one column and all controls starting at the same horizontal position.
A common first attempt wraps each label and control in a .field element and makes each .field a grid:
.field {
display: grid;
grid-template-columns: max-content 1fr;
gap: 1rem;
}Each .field sizes its own max-content column from its own label. "Name" gets a narrow column and "Email address" gets a wide one, so the controls start at different positions. The fields do not know about each other.
You could hard-code a label width such as 10rem. That works until a translation or a larger font makes a label wider than the column. Subgrid removes the need to guess.
The solution: one parent grid, subgrid fields
Define the columns once on the <form>. Each .field spans all of them and inherits them:
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}Three declarations on .field do the work:
grid-column: 1 / -1makes the field span every explicit column of the parent. The subgrid inherits as many tracks as it spans, so this gives it two.display: gridmakes.fielda grid container, which is required forsubgridto apply.grid-template-columns: subgridtells it to use the parent's two column tracks instead of defining new ones.
The label is the first child, so it lands in the parent's first column. The control is the second child and lands in the second column. Because every field shares those two tracks, labels and controls line up.
A complete example
This page runs as written in any browser that supports subgrid:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Subgrid form</title>
<style>
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
max-width: 36rem;
font-family: system-ui, sans-serif;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
align-items: center;
}
.field input,
.field select {
min-width: 0;
padding: 0.4rem;
}
.actions {
grid-column: 2;
}
</style>
</head>
<body>
<form>
<div class="field">
<label for="name">Name</label>
<input id="name" type="text">
</div>
<div class="field">
<label for="email">Email address</label>
<input id="email" type="email">
</div>
<div class="field">
<label for="country">Country of residence</label>
<select id="country">
<option>Norway</option>
<option>Japan</option>
</select>
</div>
<div class="actions">
<button type="submit">Send</button>
</div>
</form>
</body>
</html>The first column is as wide as "Country of residence", the longest label. Every control starts right after it. Add a longer label and the column grows for all fields at once.
Why the column sizes itself correctly
A max-content track on the parent has to be wide enough for its widest item. Items inside a subgrid count as items of the parent's tracks they occupy. The label in each .field therefore contributes its width to the parent's first column, and the widest one wins.
That is the sizing contribution rule: a subgrid does not hide its content from the parent. This is what separates subgrid from the fixed-width workaround. The parent's tracks react to the content of every subgrid.
Gaps in the form subgrid
Gap handling in a subgrid depends on the axis. In the column axis, where the subgrid is active, the subgrid inherits the parent's column-gap (1rem in the example). The gap between labels and controls is therefore the same in every field and comes from one place.
In the row axis, .field is an ordinary grid. The parent's row-gap of 0.75rem separates the fields from each other, because the fields are the parent's items. A gap inside a single field would be a different property, set on .field itself.
You can override the inherited column gap on the subgrid:
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
column-gap: 0.5rem;
}The track sizes stay shared. The gap only changes the spacing between the two columns inside that field. A different gap in one field shifts where its tracks start relative to the others, so overriding it usually breaks the alignment you built the subgrid to get. Leave it inherited unless a field has a good reason to differ.
Items that do not fit the two-column pattern
Real forms have more than label-control pairs. Handle each case by placement, not by adding more wrapper elements.
Hint text and error messages
Put help text inside the .field as a third child and place it in the control column:
.field .hint {
grid-column: 2;
font-size: 0.875rem;
color: #555;
}The subgrid has two columns, so the third child auto-places into a new row of the subgrid, and grid-column: 2 keeps it under the control. The row axis of .field is not a subgrid, so the new row sizes itself from the hint's content.
Line numbers are local to the subgrid. Inside .field, line 1 is the start of the label column and line 2 is the start of the control column, even if the field itself sits further along the parent grid.
Full-width items
Fieldsets, headings and notices usually span the whole form. Give them grid-column: 1 / -1:
fieldset {
grid-column: 1 / -1;
}If the fieldset contains fields, make it a subgrid as well so that its contents still use the parent's columns:
fieldset {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
gap: 0.75rem 1rem;
}
fieldset .field {
grid-column: 1 / -1;
}Subgrids can nest. The fieldset inherits the parent's two columns, and each .field inside it inherits them from the fieldset. Both levels still resolve to the form's tracks.
Note the explicit gap on the fieldset. The row gap is not inherited because that axis is not a subgrid, so you set it to keep the same vertical rhythm. The column gap is inherited, and the shorthand's second value simply restates it.
Buttons
The .actions wrapper in the example uses grid-column: 2. It is a direct child of the form, not of a .field, so it uses the parent's columns directly. This places the button under the controls.
Narrow screens
Two columns can be too tight on a phone. Change the parent's track definition once and every subgrid follows:
@media (max-width: 30rem) {
form {
grid-template-columns: 1fr;
}
.actions {
grid-column: 1;
}
}The .field elements still span 1 / -1, which is now one column. They inherit one track. Their two children auto-place into two rows instead of two columns, so the label sits above the control. One edit in one rule covers every field.
Fallback for browsers without subgrid
Subgrid is widely supported in current browsers, but older ones ignore grid-template-columns: subgrid as an invalid value. The field would then have a single implicit column, with the label stacked above the control. That is usable but not aligned.
Give .field a working layout first, then switch to subgrid when it is supported:
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: 10rem 1fr;
column-gap: 1rem;
}
@supports (grid-template-columns: subgrid) {
.field {
grid-template-columns: subgrid;
}
}Without subgrid, labels get a fixed 10rem column and still align. With subgrid, the fixed width is replaced by the shared max-content column. Because both layouts are aligned, no browser gets a broken form.
Pitfalls specific to forms
- Wrapper elements around the label. If the HTML is
<label>Name <input></label>, the label is one child, not two, and the text and input stay together in one column. Keep the label and the control as separate siblings and connect them withforandid. - Extra children. A third child without explicit placement wraps to a new row, starting at column 1. Place hints and errors explicitly.
- Forgetting the span. A
.fieldwithoutgrid-column: 1 / -1occupies one parent column, so it inherits one track and its two children stack in two rows. This is the most common bug. - Wide controls overflowing. Inputs have an intrinsic width. In a
1frcolumn they can push the track wider than expected. Settingmin-width: 0on the control, as in the example, lets the track shrink. - Padding on the subgrid. Padding, borders and margins on
.fieldact as extra space at the edges of its first and last tracks, and the parent's track sizing accounts for them. A field withpadding-inline: 1remmakes the label column wider for every field. - Replacing wrappers with
display: contents. It can flatten the structure so labels and controls become direct children of the form. It has a history of accessibility bugs and removes the per-field box you may want for styling errors. Subgrid keeps the wrapper.
Summary of the technique
| Concern | Where it is set | Behavior |
|---|---|---|
| Column widths | `form` | Defined once, shared by all fields |
| Field spans the columns | `.field { grid-column: 1 / -1 }` | Subgrid inherits two tracks |
| Column gap between label and control | Inherited from `form` | Override only with a reason |
| Row gap between fields | `form` | Applies between `.field` items |
| Hint under a control | `.hint { grid-column: 2 }` | Local line numbers inside the subgrid |
| Older browsers | `@supports` | Fixed-width columns as the base layout |
Exercise
A form uses form { display: grid; grid-template-columns: max-content 1fr; } and .field { display: grid; grid-template-columns: subgrid; }. The labels and controls still do not line up, and each field's label sits above its control. What is missing, and why does the fix work?
Show answer
.field needs grid-column: 1 / -1. Without it, each field occupies only one parent column, so the subgrid inherits one track and the label and control stack in two rows. Spanning both columns gives the subgrid two inherited tracks, so the label goes in column 1 and the control in column 2, shared by every field.
Exercise
You add <p class="hint">…</p> as a third child of a .field that uses a two-column subgrid. Without extra CSS it appears under the label. How do you place it under the control, and which line number do you use?
Show answer
Add .hint { grid-column: 2; }. Line numbers inside a subgrid are local, so line 2 is the start of the second inherited column (the control column), regardless of where the field sits in the parent. The hint auto-places into a new row of the field.