Flexbox
Flexbox is the go-to tool for laying out items in a row or a column — navigation bars, toolbars, card rows and centring things.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Visual explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 1, 2026
Learning objectives#
- Create a flex container
- Explain the main axis and cross axis
- Align items with justify-content and align-items
- Control sizing with flex-grow, flex-shrink and flex-basis
Explanation#
Set display: flex on a parent and its direct children become flex items laid out along the main axis (a row by default).
justify-content distributes items along the main axis. align-items aligns them on the cross axis (vertically, for a row).
flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0 — the item grows to fill free space equally with its siblings.
Visual explanation#
Syntax#
.container {
display: flex;
flex-direction: row; /* or column */
justify-content: space-between;
align-items: center;
gap: 16px;
flex-wrap: wrap;
}
.item { flex: 1 1 200px; }
Code example: A responsive navigation bar#
<style>
nav { display:flex; align-items:center; gap:16px; padding:12px 16px; background:#1e1b4b; font-family:sans-serif; }
.logo { color:#fff; font-weight:700; margin-right:auto; }
nav a { color:#c7d2fe; text-decoration:none; }
.btn { background:#2dd4bf; color:#042f2e; padding:6px 12px; border-radius:8px; }
</style>
<nav>
<span class="logo">Techie Babaji</span>
<a href="#">Tutorials</a>
<a href="#">Projects</a>
<a class="btn" href="#">Start</a>
</nav>
The logo sits on the left; the links and the button are grouped on the right, vertically centred.
Step-by-step explanation#
- display: flex turns <nav> into a flex container; the logo and links become flex items in a row.
- align-items: center centres all items vertically.
- gap: 16px adds consistent spacing between items without margins.
- margin-right: auto on the logo absorbs all free space, pushing the remaining items to the right.
Real-world use case#
Headers, button groups, form rows, media objects (image + text) and centring a single element are all classic Flexbox jobs.
Common mistakes#
justify-content and align-items go on the container; flex, align-self and order go on the items.
With flex-direction: column, justify-content works vertically and align-items works horizontally.
For two-dimensional layouts with rows and columns, CSS Grid is usually simpler.
Best practices#
- Use gap instead of margins between items
- Use flex-wrap: wrap with a flex-basis for simple responsive rows
- Use margin-left: auto to push items apart
- Reach for Grid when you need alignment in two dimensions
Remember this#
Flexbox = one direction (row or column).
justify-content = main axis; align-items = cross axis.
gap adds space between items.
Practice exercise#
Your task: Centre a 200×200 box both horizontally and vertically inside a container that is 100vh tall.
Show solution
.container { display: flex; justify-content: center; align-items: center; min-height: 100vh; }
.box { width: 200px; height: 200px; }
Mini quiz#
In a row flex container, which property centres items vertically?
Want more? Practise all CSS MCQs · CSS interview questions
My notes#
Saved in this browser.