CSS Flexbox Explained with Real-World Layout Examples
Introduction
1 . What is Flexbox and When to Use It?
2 . Main Axis vs Cross Axis
3 . Properties for Containers
4 . Properties for Items
5 . Building Common Layouts with Flexbox
6 . Troubleshooting Flexbox Issues
7 . Essential Flex Container Properties
8 . Flex Item Properties
9 . Real-World Layout Examples
10 . Flexbox vs Grid – When to Use What?
Common Flexbox Gotchas
Browser Compatibility
Helpful Flexbox Tools
Summary
Conclusion
Flexbox is a powerful CSS layout module designed to arrange elements efficiently even when their size is unknown or dynamic. It helps create responsive and flexible layouts with less code than traditional methods. In this guide, we’ll break down Flexbox fundamentals and apply them to real-world examples like navbars, cards, and sidebars.
Flexbox (Flexible Box Layout) is a one-dimensional layout model for distributing space along a single axis — either horizontally or vertically. Use Flexbox when you need to align items, distribute space dynamically, or build complex layouts that adapt to different screen sizes.
Understanding these axes is essential for controlling item alignment and direction.
display: flex
Turns an element into a flex container, enabling Flexbox layout.
flex-direction
Controls the direction of the main axis. Values:
row
(default) — left to rightrow-reverse
column
— top to bottomcolumn-reverse
justify-content
Aligns items along the main axis:
flex-start
, flex-end
, center
, space-between
, space-around
, space-evenly
,align-items
Aligns items along the cross axis:
stretch
(default), flex-start
, flex-end
, center
, baseline
gap
Adds consistent spacing between flex items.
flex-grow
Defines how much a flex item will grow relative to others.
flex-shrink
Defines how much a flex item will shrink relative to others.
flex-basis
Sets the default size of an item before space distribution.
order
Controls the order of items independent of their source code.
Navbar
nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
background-color: #333;
color: white;
}
Cards Layout
.cards-container {
display: flex;
gap: 1rem;
flex-wrap: wrap;
}
.card {
flex: 1 1 200px;
padding: 1rem;
border: 1px solid #ccc;
}
Sidebar with Content
.container {
display: flex;
}
.sidebar {
flex: 0 0 250px;
background-color: #f4f4f4;
}
.content {
flex: 1;
padding: 1rem;
}
display: flex
flex-direction
Value | Description |
---|---|
row |
Left to right (default) |
row-reverse |
Right to left |
column |
Top to bottom |
column-reverse |
Bottom to top |
.container {
display: flex;
flex-direction: row; /* or column */
}
justify-content
Value | Description |
---|---|
flex-start |
Align to start of main axis |
center |
Center items |
flex-end |
Align to end |
space-between |
Equal spacing between items |
space-around |
Equal spacing around items |
space-evenly |
Equal spacing between and around |
align-items
Value | Description |
---|---|
stretch |
Stretch to fill container (default) |
flex-start |
Align to start of cross axis |
flex-end |
Align to end of cross axis |
center |
Center vertically |
baseline |
Align items based on text baseline |
flex-wrap
.container {
flex-wrap: wrap;
}
flex-grow
.item {
flex-grow: 1;
}
flex-shrink
.item {
flex-shrink: 1;
}
flex-basis
.item {
flex-basis: 200px;
}
flex
.item {
flex: 1 1 200px;
}
align-self
.item {
align-self: center;
}
1 . Navbar with Logo, Menu, and CTA
<nav class="navbar">
<div class="logo">Brand</div>
<ul class="menu">
<li>Home</li><li>About</li><li>Contact</li>
</ul>
<button class="cta">Sign Up</button>
</nav>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
.menu {
display: flex;
gap: 1rem;
}
2 . Card Grid that Wraps
<div class="cards">
<div class="card">A</div>
<div class="card">B</div>
<div class="card">C</div>
<div class="card">D</div>
</div>
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
}
3 . Responsive Sidebar Layout
<div class="layout">
<aside>Sidebar</aside>
<main>Main Content</main>
</div>
.layout {
display: flex;
flex-wrap: wrap;
}
aside {
flex: 1 1 20%;
}
main {
flex: 1 1 80%;
}
4 . Vertical Centering
<div class="centered">
<p>I'm centered!</p>
</div>
.centered {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
Feature | Flexbox | CSS Grid |
---|---|---|
Axis | One-dimensional | Two-dimensional |
Content flow | Dynamic rows/columns | Explicit layout |
Ideal use | Components, navbars | Page layouts |
Use Flexbox for UI components, and Grid for full-page layout structures.
Flexbox is fully supported on all modern browsers including:
For older browsers like IE11, use autoprefixer or fallback layouts.
Property | Purpose |
---|---|
display: flex |
Activate Flexbox |
flex-direction |
Define row/column layout |
justify-content |
Align items along the main axis |
align-items |
Align items along the cross axis |
flex |
Short-hand for grow/shrink/basis |
gap |
Adds space between items cleanly |
flex-wrap |
Allows items to move to new lines |
Flexbox simplifies layout design by providing intuitive control over alignment, spacing, and distribution. With practice and real-world examples, you can build responsive web layouts that work seamlessly across devices.
CSS Flexbox empowers developers to create dynamic and responsive layouts with ease. From horizontal navbars to card grids and centered modals, Flexbox handles it all without relying on complex float or positioning hacks.
Mastering Flexbox means mastering modern layout techniques, and with these examples and tips, you're well on your way.
Senior Frontend Engineer
Mark is a passionate software developer and author with expertise in JavaScript and Python. He enjoys simplifying complex programming concepts and sharing practical coding tips.