Flexbox: lining things up in a row or column
Use CSS Flexbox to build navigation bars, centred content and rows of cards: display flex, justify-content, align-items, gap, flex-wrap and flex-grow, with live examples.
🎯 By the end of this lesson you will be able to:
- Turn a container into a flex container and line its children up
- Space and align items with justify-content, align-items and gap
- Build wrapping rows and items that grow to fill space
Outline
What you'll learn and why it matters
Blocks stack on top of each other by default. But most designs need things side by side: menu links in a row, a logo on the left and a menu on the right, three cards next to each other. Flexbox is the CSS tool for lining things up along one direction, a row or a column. It is used on nearly every modern website.
display: flex
Flexbox works on a container and its direct children (the items). Put display: flex on the container and the children line up in a row.
.row {
display: flex;
gap: 1rem;
}
.item {
background-color: #dbeafe;
border: 2px solid #2563eb;
padding: 0.75rem 1rem;
}Show the HTML this CSS styles
<div class="row">
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three</div>
</div>Browser preview
gap puts space between items, but not around the outside. It is the cleanest way to space flex items.
justify-content: spacing along the main axis
| Value | Result |
|---|---|
flex-start (default) | Items packed at the start |
center | Items packed in the middle |
flex-end | Items packed at the end |
space-between | First item at the start, last at the end, equal space between |
space-around / space-evenly | Space shared around or evenly between all items |
.row {
display: flex;
justify-content: space-between;
background-color: #f1f5f9;
}
.item {
background-color: #dbeafe;
border: 2px solid #2563eb;
padding: 0.75rem 1rem;
}Show the HTML this CSS styles
<div class="row">
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three</div>
</div>Browser preview
align-items: lining up across
align-items works on the cross axis, which is vertical in a row. It matters when items have different heights.
.row {
display: flex;
align-items: center;
gap: 1rem;
min-height: 8rem;
background-color: #f1f5f9;
}
.item {
background-color: #dbeafe;
border: 2px solid #2563eb;
padding: 0.75rem 1rem;
}
.tall {
padding: 2rem 1rem;
}Show the HTML this CSS styles
<div class="row">
<div class="item">Short</div>
<div class="item tall">Tall</div>
<div class="item">Short</div>
</div>Browser preview
The default, stretch, makes every item as tall as the tallest one. Other values are flex-start, center and flex-end.
Worked example: a navigation bar
A classic pattern: the site name on the left and the menu on the right. The header is a flex container with space-between, and the list of links is a flex container too.
body {
margin: 0;
font-family: Arial, sans-serif;
}
.site-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
background-color: #14532d;
color: white;
}
.site-header ul {
display: flex;
gap: 1.25rem;
list-style: none;
margin: 0;
padding: 0;
}
.site-header a {
color: white;
}
.logo {
font-weight: 700;
font-size: 1.25rem;
}Show the HTML this CSS styles
<header class="site-header">
<span class="logo">Green Leaf</span>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="menu.html">Menu</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</header>Browser preview
list-style: none removes the bullets, and setting the list's margin and padding to 0 removes its default indent.
Wrapping and growing
By default, flex items squeeze onto one line. flex-wrap: wrap lets them move onto new lines when there is no room, which is what you want on phones. On the items, flex: 1 1 12rem means *grow to fill space, shrink if needed, and start from 12rem wide*.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 12rem;
padding: 1rem;
border: 2px solid #b45309;
border-radius: 0.75rem;
background-color: #fef3c7;
}Show the HTML this CSS styles
<div class="cards">
<div class="card">Masala chai</div>
<div class="card">Filter coffee</div>
<div class="card">Banana bread</div>
</div>Browser preview
Use the preview buttons to compare sizes. At Laptop (1024px) the three cards sit side by side and grow equally. At Phone (360px) they stack, because three 12rem cards no longer fit in a row. In between, two fit on the first line and the third wraps. A responsive layout with no media query at all.
Practice
Guided exercise
A footer with three columns
Lay out the footer's three groups side by side with space between them, and let them wrap on small screens.
- Select
.footerand adddisplay: flex. - Add
flex-wrap: wrapand agapof2rem. - Give each
.footer divaflexvalue like1 1 10remso they share the width.
Show the solution
.footer {
display: flex;
flex-wrap: wrap;
gap: 2rem;
padding: 1.5rem;
background-color: #1e293b;
color: white;
}
.footer div {
flex: 1 1 10rem;
}
.footer h2 {
font-size: 1rem;
}Show the HTML this CSS styles
<footer class="footer">
<div><h2>Visit</h2><p>12 Park Road, Pune</p></div>
<div><h2>Hours</h2><p>Every day, 8am to 8pm</p></div>
<div><h2>Contact</h2><p>020 1234 5678</p></div>
</footer>Browser preview
How to get there: The footer is the flex container and its three divs are the items. flex-wrap plus a starting size of 10rem means they sit in a row when there is room and stack on a phone.
Your turn
Centre a message
Centre the welcome box both across and down inside a 16rem-tall banner.
Show the solution
.banner {
display: flex;
justify-content: center;
align-items: center;
min-height: 16rem;
background-color: #ede9fe;
}
.message {
padding: 1rem 2rem;
background-color: white;
border-radius: 0.75rem;
}Show the HTML this CSS styles
<div class="banner">
<p class="message">Welcome to our new website!</p>
</div>Browser preview
How to get there: justify-content: center centres along the row (across), and align-items: center centres on the cross axis (down). The banner needs a height for vertical centring to be visible.
Common mistakes
Putting display: flex on the items instead of their parent
You see: Nothing lines up
Fix: Flex goes on the container; it arranges the container's direct children.
Expecting align-items: center to centre across a row
You see: Items move up and down, not left and right
Fix: In a row, justify-content is across and align-items is down.
No flex-wrap on a row of cards
You see: Cards squash or overflow on phones
Fix: Add flex-wrap: wrap and a starting size with flex.
Using margins to space every item
You see: Uneven gaps and extra space at the ends
Fix: Use gap on the container.
Recap
display: flexon a container lines up its direct children in a row.justify-contentspaces items along the main axis;align-itemsaligns them across it.gapspaces items;flex-wrap: wraplets them move to new lines.flex: 1 1 12remlets items grow, shrink and start from a chosen size.
Key terms
- Flex container
- An element with display: flex; it arranges its direct children.
- Main axis
- The direction flex items line up in: left to right in a row.
- Cross axis
- The direction at right angles to the main axis.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Where do you put display: flex to line up three cards in a row?
2. In a flex row, which property puts the logo at the far left and the menu at the far right?
3. What does gap: 1rem do in a flex container?
4. A row of cards overflows the screen on phones. Which property fixes it most simply?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.