Sub topic 9
Aligning Form Labels and Controls Across Fields
This chapter assumes you know CSS Grid basics (grid-template-columns, fr, gap, grid-column) and the subgrid keyword from earlier chapters. Here you will use subgrid to build a form where every label column and every input column lines up, with no fixed widths.
The problem
A form usually has one row per field: a label and a control. You want all labels to share one width and all controls to start at the same horizontal position. If each field is wrapped in its own element, each wrapper is a separate grid. A max-content label column then sizes itself to that field's own label. A short "Name" label gives a narrow column, and a long "Email address" label gives a wide one. The inputs end up staggered.
Giving every label a fixed width fixes this, but the number is a guess. It breaks with longer translations, larger user font sizes, or a new field with a longer label.
The solution: one set of columns, shared by every field
Define the columns once on the form. Each field spans both columns and uses them as its own.
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}<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="msg">Message</label>
<textarea id="msg" rows="3"></textarea>
</div>
</form>The max-content column is sized from the content of every field. The widest label ("Email address") sets the width, and all labels get that width. The 1fr column takes the remaining space, so every control starts at the same position and stretches to the same right edge.
Why each piece is needed
grid-column: 1 / -1makes the field span both parent columns. Without it, auto-placement puts the field in a single column. The subgrid would then inherit only one track, and the label and input would stack in it.grid-template-columns: subgridreplaces the field's own column definitions with the two parent tracks. The label lands in track 1 and the input in track 2, in source order.- The row axis is not subgridded. Each field keeps its own implicit row, and the form's
row-gapseparates the fields. - A subgrid inherits the parent's gap by default. Here the 1rem column gap sits between label and control. To change it for one field, set
column-gapon.field.
Common mistakes
- Forgetting the span. The symptom is a label and input squeezed into the first column. Add
grid-column: 1 / -1. - Using
display: contentson the wrapper. The children join the form grid directly, which also aligns them. But the wrapper can no longer take a background, border, padding, or a per-field gap, and older browsers had accessibility bugs with it. Subgrid keeps the wrapper as a real box. - Extra children. A field with a hint paragraph has three children, but only two inherited tracks. The third child wraps to a new implicit row. Give it
grid-column: 2, or place the hint inside the control's column in the markup.
Fallback
In a browser without subgrid, grid-template-columns: subgrid is invalid and ignored. The field then has no column definition and the label and input stack. Declare a usable layout first, then upgrade it.
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: 9rem 1fr;
}
@supports (grid-template-columns: subgrid) {
.field {
grid-template-columns: subgrid;
}
}The fallback uses a fixed 9rem label column. It is less flexible, but the form still lines up.
Exercise
A form uses grid-template-columns: max-content 1fr. Each .field has display: grid; grid-template-columns: subgrid; but no grid-column rule. The label and input appear stacked inside the first column. Why, and what is the fix?
Show answer
Without a span, each field is auto-placed into a single parent column (the first one), so the subgrid inherits only one track, and both children share it. Add grid-column: 1 / -1 to .field so it spans both columns and inherits both tracks.