CSS Grid
The CSS layout mode for rows and columns at once, which brought the designer's layout grid into the browser and lets responsive layouts adapt with no breakpoints at all.
CSS Grid lays out elements in rows and columns at the same time. Where Flexbox arranges items along one line, grid defines a two-dimensional structure first and then places items into it. It shipped in most major browsers within a few weeks of each other in March 2017, an unusually coordinated release, and finally gave the web the kind of layout grid print designers had used for decades; see Grid System.
A grid container defines its tracks:
grid-template-columns: 200px 1fr 1frmakes a fixed sidebar and two equal flexible columns. Thefrunit means a fraction of the remaining space.grid-template-rowsdoes the same vertically.gapsets the gutters.
Items flow into cells automatically, or can be placed explicitly by line number or by name. Named areas let a layout be drawn almost as text:
grid-template-areas: "header header" "nav main" "footer footer"
Each child then declares which area it occupies. Rearranging the page for a narrow screen means redrawing that map, not touching the markup.
Grid's most useful trick is responsive layout without breakpoints. repeat(auto-fill, minmax(240px, 1fr)) means "as many columns as fit, each at least 240 pixels, sharing extra space equally." A card gallery then shows one column on a phone and five on a wide monitor with no Responsive Breakpoint at all. Combined with Container Query, components adapt to the space they're given rather than to the window.
Subgrid, widely supported since 2023, lets a nested element adopt its parent's tracks. Cards in a row can then align their titles, bodies, and buttons across cards even when content lengths differ, a problem that previously needed fixed heights or JavaScript.
Things to watch:
- Source order. Placing items anywhere on the grid can make the visual order differ from the document order, which confuses keyboard and screen reader users; see Keyboard Navigation.
- Implicit tracks. Items placed beyond the defined grid create extra rows or columns automatically, sized by separate rules that often surprise people.
- Overflow. A
1frtrack has a minimum of its content's size by default, so a long unbroken string can blow out a column;minmax(0, 1fr)prevents it.
The common rule of thumb is that grid works layout-in: you design the structure and items fill it. Flexbox works content-out: items' sizes shape the result. Page scaffolding, dashboards, galleries, and forms with aligned labels suit grid; toolbars and button rows suit flexbox. Both rely on the same Box Model underneath.
Grids also support visual order beyond layout. Aligning elements to shared lines is one of the strongest grouping cues in Gestalt Principles, and repeated consistent grids are how small multiples stay comparable.
See also1
Linked from8
- Box ModelDesign & Interface
The rule that every element on a web page is a rectangle of content wrapped in padding, border, and margin, and the two quirks, box sizing and collapsing margins, behind most surprises in CSS spacing.
- Cascading Style SheetsWeb Platform
The language that describes how web pages look, proposed in 1994 to separate presentation from structure, nearly sunk by inconsistent browser support, and grown since into a layout and animation system that does work once reserved for scripts.
- Container QueryDesign & Interface
Styling based on the size of an element's container rather than the viewport.
- Data TableDesign & Interface
Rows and columns of values designed for looking things up and comparing exact figures, which read best with right-aligned numbers, light rules, and headers that stay put, and remain one of the hardest components to make work on a phone.
- FlexboxDesign & Interface
The CSS layout mode for arranging items along one axis, distributing space and aligning them without floats or hacks, which finally made vertical centering trivial and powers most component layouts.
- Gestalt PrinciplesDesign & Interface
A set of perceptual rules describing how the eye groups discrete elements into wholes before conscious analysis begins.
- Responsive BreakpointDesign & Interface
A viewport width at which a layout changes, and the assumptions that come with it.
- Tiling Window ManagerDesign & Interface
A window manager that arranges windows automatically into non-overlapping tiles and is driven mainly from the keyboard, trading freeform placement for zero layout effort.