← All Exercises

Align Form Labels with CSS Subgrid

advancedcss~15min

Background

Subgrid lets nested grids reuse parent tracks, keeping labels aligned across groups without hard-coded widths.

Problem

Lay out a form so labels align in a narrow first column and controls fill a flexible second column, including fields nested inside a fieldset, without duplicating column widths.

Input

A form with two text fields and a radio group inside a fieldset.

Output

Parent grid tracks plus a subgrid fieldset and a fallback.

Sample tests

Sample 1

Input: Radio group inside the fieldset
Expected: the fieldset spans grid-column 1/-1 and uses grid-template-columns:subgrid so its labels align with the email label

Sample 2

Input: Browser without subgrid support
Expected: the @supports not fallback gives the fieldset its own max-content and 1fr columns

Your solution

Learning objective

Use subgrid to share column tracks across nested form groups and guard the layout with @supports.