Flexbox is a one-dimensional CSS layout system for arranging items in a row or column. Add display: flex to a container, then use justify-content to align items along the main axis, align-items along the cross axis, gap for spacing, flex-wrap to wrap items and flex: 1 to let items grow. It is ideal for navigation bars, button groups, centring and card rows.
Before CSS flexbox, centring something vertically on a web page was a running joke among developers. It involved tables, negative margins or odd hacks. Flexbox changed that: a few lines of CSS now handle centring, spacing and alignment that used to take hours.
The key to flexbox is understanding two directions — the main axis and the cross axis. Once that clicks, the properties make sense, and you will find yourself reaching for flexbox in almost every layout.
Main axis and cross axis
With flex-direction: row (the default), the main axis runs left to right and the cross axis top to bottom. With column, they swap.

CSS flexbox container properties
| Property | Common values |
|---|---|
flex-direction |
row, column, row-reverse, column-reverse |
justify-content |
flex-start, center, flex-end, space-between, space-around, space-evenly |
align-items |
stretch, flex-start, center, flex-end, baseline |
flex-wrap |
nowrap, wrap |
gap |
Spacing between items, e.g. 16px |
Item properties
| Property | What it does |
|---|---|
flex-grow |
How much an item grows to fill space |
flex-shrink |
How much an item shrinks when space is tight |
flex-basis |
Starting size before growing or shrinking |
flex |
Shorthand, e.g. flex: 1 |
align-self |
Override alignment for one item |
order |
Change visual order (use carefully for accessibility) |
Common flexbox layouts
Centre anything
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
Navigation bar
.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 24px;
}
.nav ul { display: flex; gap: 20px; list-style: none; }
Wrapping card row
.cards { display: flex; flex-wrap: wrap; gap: 20px; }
.card { flex: 1 1 260px; }
Sticky footer
body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex: 1; }
Warning: Changing visual order with order or row-reverse does not change the reading or tab order. Keep the HTML order logical for keyboard and screen reader users.
Practice exercises
- Centre a cardPut a card in the middle of the screen horizontally and vertically.
- Build a navbarLogo on the left, links on the right, all vertically centred.
- Make a pricing rowThree cards of equal height that wrap on small screens.
- Create a media objectImage on the left, text on the right, image fixed width.
- Push a footer downKeep the footer at the bottom of short pages.
Common flexbox mistakes
| Problem | Why it happens | Fix |
|---|---|---|
| Items will not shrink and overflow | Long text or images have a minimum content width | Add min-width: 0 to the item |
| Items do not wrap | flex-wrap defaults to nowrap |
Add flex-wrap: wrap |
| Uneven card heights | Content differs; align-items set to start | Keep default stretch, or use grid |
justify-content seems to do nothing |
No free space or wrong axis | Check flex-direction and widths |
Tip: Turn on the flexbox overlay in your browser’s developer tools. Seeing the axes and free space makes debugging much faster.
For two-dimensional layouts, learn CSS grid. Practise with HTML projects.
Frequently asked questions
What is flexbox in CSS?
Flexbox is a CSS layout module for arranging items in a single row or column with flexible sizing and alignment.
How do I centre a div with flexbox?
Set the parent to display: flex with justify-content: center and align-items: center.
What does flex: 1 mean?
It lets an item grow and shrink to share available space equally with other flex: 1 items.
When should I use flexbox instead of grid?
Use flexbox for one-dimensional layouts such as navbars, toolbars and simple rows; use grid for two-dimensional layouts.
Is flexbox supported in all browsers?
Yes, flexbox is supported in all modern browsers, including on mobile devices.
Can I nest flex containers?
Yes. A flex item can itself be a flex container, which is common for cards, headers and complex layouts.
What is the gap property in flexbox?
gap adds space between flex items without margins, making spacing simpler and more consistent.
Further reading: MDN’s web development learning area.







