Flexbox is the CSS layout system for arranging items in a single row or column. It makes jobs that used to take hacks simple: putting a logo on the left and a menu on the right, centring something vertically, or making cards share the available space and wrap on small screens. You switch it on with one line, display: flex, on the parent element.
In this lesson you will learn
- Flex containers and flex items
- The main axis and cross axis
- Spacing with
justify-content,align-itemsandgap - How
flex: 1shares space - Wrapping and column layouts
Containers, Alignment and Centring
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flexbox</title>
<style>
.row { display: flex; gap: 8px; background: #eceff1; padding: 8px; margin-bottom: 8px; }
.row div { background: #00695c; color: white; padding: 8px 12px; border-radius: 6px; }
.between { justify-content: space-between; }
.center { justify-content: center; align-items: center; height: 70px; }
</style>
</head>
<body>
<div class="row"><div>1</div><div>2</div><div>3</div></div>
<div class="row between"><div>Logo</div><div>Menu</div></div>
<div class="row center"><div>Centred both ways</div></div>
</body>
</html>
Result
display: flexmakes an element a flex container; its direct children become flex items and line up in a row.- The main axis runs along the row.
justify-contentspaces items along it:flex-start,center,space-between,space-around,space-evenly. - The cross axis runs across it.
align-itemspositions items on it:stretch(the default),center,flex-start,flex-end. justify-content: centerplusalign-items: centercentres content both ways.gapadds space between items without margins.
Growing, Wrapping and Columns
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flex grow and wrap</title>
<style>
.row { display: flex; gap: 8px; background: #eceff1; padding: 8px; margin-bottom: 8px; }
.row div { background: #6a1b9a; color: white; padding: 8px; border-radius: 6px; }
.grow { flex: 1; } /* takes the spare space */
.wrap { flex-wrap: wrap; }
.wrap div { flex: 1 1 120px; } /* at least 120px, then wrap */
.col { flex-direction: column; }
</style>
</head>
<body>
<div class="row"><div>Sidebar</div><div class="grow">flex: 1 fills the rest</div></div>
<div class="row wrap"><div>Card A</div><div>Card B</div><div>Card C</div><div>Card D</div><div>Card E</div></div>
<div class="row col"><div>Column 1</div><div>Column 2</div></div>
</body>
</html>
Result
flex: 1tells an item to take all the spare space; the sidebar keeps its natural width.flex: 1 1 120pxmeans grow, shrink, and start from a base width of 120px.flex-wrap: wrapmoves items to a new line when they no longer fit, which gives a responsive card layout without media queries. Narrow your window to see the cards wrap.flex-direction: columnturns the main axis vertical, stacking items.
Flexbox Cheat Sheet
| On the container | On the items |
|---|---|
display: flex | flex: grow shrink basis |
flex-direction: row | column | align-self (override align-items for one item) |
justify-content | order (change visual order) |
align-items | margin-left: auto (push an item to the end) |
flex-wrap, gap |
Common Mistakes
- Putting
display: flexon the items instead of their parent. - Mixing up the axes: after
flex-direction: column,justify-contentworks vertically. - Using
orderto rearrange content, which confuses keyboard and screen-reader users because they follow the HTML order. - Expecting two-dimensional control. For rows and columns at once, use CSS Grid.
Practice Exercises
Exercise 1. Build a navigation bar with the logo on the left and links on the right, vertically centred.
Show answer
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
Exercise 2. Make three equal-width columns that share a row.
Show answer
.row { display: flex; gap: 16px; }
.row > div { flex: 1; }
Frequently Asked Questions
When should I use Flexbox instead of Grid?
Use Flexbox for one-dimensional layouts such as toolbars, menus and rows of buttons. Use Grid when you need rows and columns together, such as a whole page layout or a photo gallery.
How do I centre a div in CSS?
Give its parent display: flex; justify-content: center; align-items: center; and a height.
Summary
display: flexon the parent lines up its children.justify-contentworks along the main axis,align-itemsacross it.flex: 1shares space;flex-wrapmakes rows responsive.- Flexbox handles one direction; Grid handles two.
Next: CSS Grid.
