
For most of the web's history, page layouts were built with tools that were never designed for them: HTML tables in the 1990s, then floats, then positioning tricks. CSS Grid Layout is the first part of CSS created specifically for laying out pages in two dimensions, rows and columns at the same time. It became available in all major browsers in 2017, when Chrome, Firefox, Safari and Edge shipped support within a few months of each other, and it is now a standard part of front-end development. With Grid, layouts that once needed dozens of lines and extra wrapper elements take a handful of declarations.
Set display: grid on the container, then describe the columns with grid-template-columns. A value of 200px 1fr 1fr creates three columns: one fixed at 200 pixels and two that share the remaining space equally.
The fr unit, short for "fraction", is Grid's most useful invention. It divides the free space after fixed sizes and gaps have been subtracted. 1fr 2fr gives the second column twice the share of the first. Unlike percentages, fr units never cause overflow when you add gaps, because the gaps are subtracted first.
For repeated columns, use repeat(): repeat(4, 1fr) creates four equal columns. Rows are created automatically as items are added, each just tall enough for its content, but you can set them explicitly with grid-template-rows.
The gap property sets the space between rows and columns, for example gap: 1.5rem, or separate values with row-gap and column-gap. Gaps appear only between tracks, never on the outer edges, which avoids the old problem of removing the margin from the last item in a row.
By default, items fill the cells in order, left to right and top to bottom. To control placement, refer to line numbers. grid-column: 1 / 3 makes an item start at line 1 and end at line 3, spanning two columns. grid-column: span 2 spans two columns from wherever the item would naturally start. A useful trick: negative numbers count from the end, so grid-column: 1 / -1 stretches an item across the full width of any grid, whatever its number of columns.
For whole-page layouts, grid-template-areas is the most readable approach. You draw the layout as text, giving each region a name:
Then assign each element to its area with grid-area: header, grid-area: main and so on. The CSS now looks like a picture of the page. Each name must form a rectangle, and a dot can be used for an empty cell.
Named areas make responsive design remarkably simple. Start with the mobile layout: one column, with areas stacked as header, main, sidebar and footer. Then add a media query, for example for screens at least 48em wide, which redefines grid-template-columns and grid-template-areas to place the sidebar beside the main content. You do not touch the HTML, and you do not change the rules for individual elements; only the template changes.
For galleries, product lists and cards, Grid can adapt by itself. The pattern grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) means: fit as many columns as possible, each at least 250 pixels wide, and stretch them to share any extra space. On a phone you get one column, on a tablet two or three, on a wide screen four or five, all from a single line of CSS.
The difference between auto-fit and auto-fill appears only when there are few items. With auto-fill, empty column tracks are kept, so a few cards stay at their minimum size. With auto-fit, empty tracks collapse, so the existing cards stretch to fill the row. For most card layouts, auto-fit is what you want.
To prevent the minimum width from causing overflow on very narrow screens, a refined version uses minmax(min(250px, 100%), 1fr), so a column is never wider than its container.
Grid shares the alignment properties of Flexbox. justify-items aligns items horizontally inside their cells and align-items aligns them vertically; justify-self and align-self do the same for a single item. place-items: center centres content both ways, which makes centring an element, once a famous CSS headache, a one-line task.
A common problem with card layouts is that titles, text and buttons inside neighbouring cards do not line up, because each card has its own internal layout. Subgrid solves this. When a card is itself a grid with grid-template-rows: subgrid and spans several rows of the parent, its children align to the parent's rows, so every card's button sits at the same height. Subgrid is supported in all major browsers since 2023.
The two are partners, not rivals. Flexbox is one-dimensional: it arranges items in a single row or column and is driven by the size of the content. It is ideal for navigation bars, button groups and aligning items inside a component. Grid is two-dimensional: you define the structure first and place content into it. It is ideal for page layouts, galleries and any design where items must line up both horizontally and vertically. A typical page uses Grid for the overall layout and Flexbox inside components.
Firefox, Chrome and Edge include a grid inspector in their developer tools. Clicking the "grid" badge next to a container overlays its lines, numbers, area names and gaps on the page. When an item lands in the wrong place, the overlay usually shows why at a glance.
The best way to learn Grid is to rebuild layouts you see every day: a news homepage, a photo gallery, a dashboard. Within a few attempts, you will find that you design in rows and columns naturally, and that responsive layouts become a matter of rearranging a template rather than rewriting your CSS.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions