Flexbox, Grid and Position
Level 4 · Page layout
Three ways CSS places boxes on a page. When Flexbox fits a row, when CSS Grid fits rows and columns, and when position pins one box.
Course contentsLesson 9 of 14
Level 1 · How a website works
Level 3 · CSS basics
Level 4 · Page layout
Level 5 · JavaScript in the page
After this lesson you can
- Lay out a row with Flexbox
- Build a grid of cards with CSS Grid
- Pin one box in place with position
14 mincovers 3 glossary entries
Worth reading first: The CSS Box Model
Which layout tool fits which job
CSS has three main tools for placing boxes. Flexbox lines boxes up in one direction, a row or a column. CSS Grid places them in rows and columns at once. The position property takes one box and pins it to a spot. It has 5 values, and static, the normal flow, is the default.
Flexbox starts with display: flex on the parent box. Its children then sit in a row. justify-content spreads them along the row, and align-items lines them up across it. The gap property sets the space between them. A menu bar, a row of buttons or a card's top line is a Flexbox job.
Flexbox shines when the boxes decide the size. Each item takes the room its content needs, and flex: 1 tells one item to take whatever space is left. If the row is too narrow, flex-wrap: wrap lets items drop to a new line.
CSS Grid starts with display: grid. You then draw the columns, and the children fill them in order. grid-template-columns: repeat(3, 1fr) makes three equal columns, where fr means a share of the free space. A page frame, a photo wall or a price table is a job for CSS Grid.
CSS Grid shines when the layout decides the size. The columns are set first, and the content fits into them. Items can also span, so a large photo can take two columns while the rest take one. Rows line up across the whole grid, which Flexbox cannot do.
Position is for the odd box out. Relative nudges a box from where it would be. Absolute lifts it out of the flow and places it inside the nearest box that has a position set. Fixed pins it to the window, and sticky lets it scroll until it reaches an edge, then holds it there.
People often think CSS Grid has replaced Flexbox. Most pages use both: CSS Grid for the big frame, and Flexbox inside each part. And position: absolute is not a layout tool. A page built from it falls apart on the first screen size the author did not test.
The three layout tools side by side
| Tool | Places boxes in | Good for | Starts with |
|---|---|---|---|
| Flexbox | One row or one column | Menus, button rows | display: flex |
| CSS Grid | Rows and columns at once | Page frames, card walls | display: grid |
| Position | One spot, out of the row | Sticky headers, badges | position: sticky |
Which one to pick
.menu { display: flex; justify-content: space-between; gap: 1rem; }
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; }
.top { position: sticky; top: 0; }Worked example
Lucy lays out a shop page
Lucy's shop page shows 12 products. She uses CSS Grid with 3 columns, so they fall into 4 neat rows.
Inside each card, Flexbox puts the price on the left and the buy button on the right. The top bar uses position: sticky, so it stays in view as the 12 cards scroll past.
The words for this lesson3
Check yourself
When should you use CSS Grid instead of Flexbox?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.