CSS Flexbox vs Grid: A Nav Bar, a Card Row and a Dashboard
Flexbox lines things up in one direction, Grid lays out rows and columns at once. Build a nav bar, a wrapping card row and a five-part dashboard with named grid areas, and see what breaks without flex-wrap.
What you will learn
- Explain normal flow: block boxes and inline boxes
- Use display flex with justify-content, align-items and gap on the main and cross axes
- Make a card row wrap with flex-wrap and flex: 1 1 160px
- Lay out a page with grid-template-columns, rows and named areas, and pick flex or grid
Before you start
- CSS selectors and the box model
We want a top bar with the brand on the left and three links on the right, all in one row. Here's the HTML:
<header class="bar">
<strong class="brand">Team Tasks</strong>
<!-- the second item is a flex container too: three links -->
<nav class="links">
<a href="#">Today</a>
<a href="#">This week</a>
<a href="#">Done</a>
</nav>
</header>
With no layout CSS, the brand sits on one line and the links drop under it. Why?
What the browser does on its own
header and nav are block boxes. A block box always starts on a new line and takes the full width. a and strong are inline boxes: they stay in the line, side by side, like words in a sentence. This default is called normal flow, and Flexbox and Grid are the two tools CSS gives you to change it.
Two everyday pictures help. Flexbox is books on a shelf: one line of things that you push left, centre or spread out. Grid is a car park with marked bays: the rows and columns are painted first, and each car parks in the bay with its name. One direction means Flexbox; rows and columns at once means Grid.
The main axis and the cross axis
flex-direction: column and the two axes swap.The flex container is the shelf, and its children are the flex items. The main axis runs along the shelf, left to right, and justify-content moves items along it. The cross axis goes across it, up and down, and align-items moves items on that one.
display: flex on the container
/* flex goes on the container, never on the items */
.bar {
display: flex;
justify-content: space-between; /* main axis: ends apart */
align-items: center; /* cross axis: centred */
height: 64px;
padding: 0 24px;
background: #1E3A8A;
color: white;
}
.brand {
font-size: 22px;
}
/* a flex container inside a flex item */
.links {
display: flex;
gap: 16px; /* space between items only */
}
.links a {
color: #BFDBFE;
}
display: flex goes on .bar, the container, and now the brand and the links share one row. justify-content: space-between pushes them to the two ends, and align-items: center centres them up and down in the 64px bar.
The .links box is a flex container too, sitting inside a flex item. It lines up its three links, and gap: 16px puts space between them (only between, not at the ends).
Try the break the file suggests: move display: flex; from .bar to .brand. The brand and the links stack again, because flex only arranges the children of the box it's on.
Cards that grow and wrap
The second example is four cards in a section class="cards":
.cards {
display: flex;
flex-wrap: wrap; /* no room left? the next card starts a new line */
gap: 16px;
}
/* each card: may grow, may shrink, starts at 160px */
.card {
flex: 1 1 160px;
border: 1px solid #CBD5E1;
border-radius: 8px;
padding: 0 14px;
}
display: flex lines the four cards up in a row. flex-wrap: wrap says that when there's no room left, the next card starts a new line. And flex: 1 1 160px is three values in one: each card may grow, may shrink, and starts at 160 pixels, then grows to share the spare room.
At 900 pixels wide all four cards fit in one row. Shrink the window to 480 pixels and you get two lines of two cards, with no extra code. Now delete flex-wrap: wrap: all four cards squeeze into one line, only 96 pixels each. With wrap the cards move to a new line; without it they squeeze.
Grid: paint the bays, then park in them
The third example is a small orders dashboard. The HTML is five children of body: header, nav, main, aside and footer. The whole layout lives in CSS:
/* the whole page is one grid: 3 columns x 3 rows */
body {
margin: 0;
padding: 12px;
font-family: system-ui, sans-serif;
display: grid;
grid-template-columns: 160px 1fr 200px;
grid-template-rows: auto 1fr auto;
min-height: 320px;
gap: 12px;
/* one string per row, one name per column */
grid-template-areas:
"top top top"
"side main stats"
"foot foot foot";
}
/* each part says which area it fills */
header { grid-area: top; }
nav { grid-area: side; }
main { grid-area: main; }
aside { grid-area: stats; }
footer { grid-area: foot; }
header, nav, main, aside, footer {
padding: 8px 12px;
border-radius: 8px;
background: #E2E8F0;
}
nav a { display: block; }
First the columns are painted: 160px, then 1fr (the rest of the space), then 200px. Then the rows: auto 1fr auto, so the top and bottom rows are only as tall as their content.
Then the bays get names in grid-template-areas. The template is a map: one string per row, one name per column. top is written three times, so the header spans all three columns. Finally each part picks its bay with grid-area: header goes to top, nav to side, aside to stats. No row or column numbers anywhere.
The file's break is a good one: write "side main stat" (no s). No area is called stats any more, so the aside leaves its place.
Flexbox or Grid?
Ask one question: do the items line up in one direction, or in rows and columns at once?
| Use | When | Examples here |
|---|---|---|
display: flex | one direction, a shelf of items | the nav bar, the card row |
display: grid | rows and columns at once, marked bays | the dashboard, a whole page layout |
You'll often use both: a grid for the page, and a flex row inside one of its bays.
Common mistakes
- Flex on the wrong box.
display: flexgoes on the parent of the things you want to arrange. - No
flex-wrap. Items squeeze instead of moving to a new line on narrow screens. - A typo in an area name. The element silently leaves its bay. Area names in
grid-areamust match the template exactly. - Using margins between items.
gapis simpler and only adds space between items.
Try it yourself
Make a top bar with flex, your name on the left and two links on the right, then a row of three course cards that wraps on a narrow window. Resize the window to check. To make the same page adapt to phones and add a dark mode, continue with responsive design, themes and dark mode.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
One Request, Two Answers: the Same Post as an HTML Page and as JSON
Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.
A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.
How a Web Page Works: HTML Tags, Elements and Attributes
A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.
Semantic HTML: header, nav, main, aside and footer as Page Landmarks
The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.
