CSS Grid Layout
While flexbox is great for arranging items in a single row or column, CSS Grid is built for true two-dimensional layouts rows AND columns at once, like a real grid or spreadsheet. Set display: grid on a parent to activate it, then grid-template-columns defines how many columns exist and how wide each is: grid-template-columns: 1fr 1fr 1fr creates exactly three equal-width columns, where fr (fraction unit) means 'one share of the available space' 2fr 1fr would make the first column twice as wide as the second. gap adds consistent spacing between both rows and columns at once, just like in flexbox. Grid shines for whole-page layouts (header, sidebar, main content, footer, all defined at once) or card galleries where items need to align neatly in both directions something flexbox alone struggles to do cleanly when rows don't have matching numbers of items.
Why this matters
- ·Grid handles true two-dimensional layouts (rows + columns) that flexbox struggles with alone
- ·fr units make flexible, proportional columns without manual percentage math
- ·gap keeps spacing consistent across both rows and columns in one property
Common mistakes
- ·Forgetting display: grid on the parent makes grid-template-columns do nothing
- ·Using grid for simple single-row layouts is often overkill compared to flexbox
- ·Mismatched column counts between grid-template-columns and actual items can cause uneven layouts
Frequently asked questions
Use Grid for true two-dimensional layouts (rows AND columns together, like a page layout or gallery). Use Flexbox for simpler, single-direction rows or columns.
Practice questions
Question 1
Build a 2-column grid with a gap of 16px.
Question 2
Make a grid with one fixed 150px sidebar column and one flexible main column.
Quick check
Just for this topic answer honestly, then check yourself.
1 / 10
