CSS Flexbox arranges items in a row or column with powerful alignment controls. It is the go-to layout tool for navbars, centered content, card rows, and responsive one-dimensional designs.
01
Container
display: flex.
02
Items
Direct children.
03
Main axis
justify-content.
04
Cross axis
align-items.
05
Direction
row / column.
06
Patterns
Nav & center.
Fundamentals
Introduction
The CSS Flexible Box Layout Module — commonly called Flexbox — is a layout model for arranging elements in one dimension: a row or a column. It distributes space along a main axis and aligns content along a cross axis.
What Is Flexbox?
Flexbox simplifies aligning and spacing elements inside a container, even when item sizes are unknown or dynamic. Before Flexbox, centering content vertically or building responsive navbars required hacks with floats and positioning.
Today, Flexbox is a core skill for every web developer. It works alongside CSS Grid: Flexbox for one-dimensional flows, Grid for two-dimensional layouts.
💡
Beginner Tip
Add display: flex to a parent, then use gap for spacing between children. Start with justify-content: center and align-items: center to center content perfectly.
Basic Concepts
Flex container — The parent with display: flex or display: inline-flex.
Flex items — The direct children inside the flex container.
Main axis — The direction items flow (row = horizontal, column = vertical).
Cross axis — Perpendicular to the main axis.
Foundation
📝 Syntax
Enable Flexbox on a container and align its children:
space-between pushes the first and last groups to opposite edges. Nested flex on .links keeps nav links evenly spaced.
Tips
💬 Usage Tips
Use gap — Prefer gap over margins between flex items.
Min-height for centering — Give the container a height so vertical centering is visible.
Nested flex — Nav link groups often need their own display: flex.
flex: 1 — Let one item grow to fill remaining space (great for sidebars).
DevTools — Chrome and Firefox show flex overlay lines to debug alignment.
Watch Out
⚠️ Common Pitfalls
Flex only direct children — Only immediate children become flex items.
Confusing axes — justify-content follows the main axis; it switches when you change flex-direction.
Forgetting container height — Vertical centering needs a defined height on the container.
Using Flexbox for full page grids — CSS Grid is better for two-dimensional layouts.
min-width: 0 — Flex items can overflow; add min-width: 0 on text children when needed.
A11y
♿ Accessibility
DOM order matters — Screen readers follow HTML order, not visual order property.
Focus order — Do not use order in ways that confuse keyboard navigation.
Touch targets — Flex nav links still need adequate padding for mobile taps.
Responsive wrapping — Use flex-wrap: wrap so items do not overflow on small screens.
Semantic HTML — Use <nav> for navigation even when styled with Flexbox.
🧠 How Flexbox Works
1
display: flex on parent
The container becomes a flex formatting context. Direct children become flex items.
Container
2
Main & cross axes set
flex-direction defines the main axis. The cross axis is perpendicular.
Axes
3
Items aligned & spaced
justify-content and align-items position items. gap adds space between them.
Align
=
📐
Flexible layout
Items adapt to available space with clean alignment.
Compatibility
🖥 Browser Compatibility
CSS Flexbox has universal support in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile browsers.
✓ Baseline · Universal support
CSS Flexbox Module
Flexbox is production-ready in every current browser. No vendor prefixes needed.
100%Modern browsers
Google ChromeFull support
Full support
Mozilla FirefoxFull support
Full support
Apple SafariFull support
Full support
Microsoft EdgeFull support
Full support
OperaFull support
Full support
CSS Flexbox100% supported
Bottom line: Flexbox is safe for production. Legacy IE issues are irrelevant for modern development.
Wrap Up
🎉 Conclusion
CSS Flexbox is essential for modern web layout. With display: flex, justify-content, and align-items, you can build navbars, center content, and create responsive one-dimensional layouts with minimal code.
Practice the four examples above, then explore individual properties like flex-grow and flex-wrap as your layouts grow more complex. Combine Flexbox with CSS Grid for full-page designs.
Center with justify-content + align-items together
Use semantic <nav> for navigation bars
Add flex-wrap: wrap for responsive rows
Combine Flexbox with CSS Grid on complex pages
❌ Don’t
Expect flex to affect nested grandchildren
Rely on order for accessibility-critical sequence
Use Flexbox for full two-dimensional page grids
Forget container height when vertically centering
Use margins when gap is cleaner
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Flexbox
Use these points when building flexible layouts.
5
Core concepts
flex01
Container
display: flex.
Start
jc02
Main axis
justify-content.
Align
ai03
Cross axis
align-items.
Align
dir04
Direction
row / column.
Flow
1D05
One axis
Not full grid.
Scope
❓ Frequently Asked Questions
Flexbox (Flexible Box Layout) is a CSS layout model for arranging items in a row or column. A flex container distributes space and aligns its children along a main axis and cross axis — ideal for navbars, centering, and responsive one-dimensional layouts.
Set display: flex or display: inline-flex on the parent element. That parent becomes a flex container, and its direct children become flex items.
justify-content aligns items along the main axis (horizontal in a row, vertical in a column). align-items aligns items along the cross axis (perpendicular to the main axis).
Use Flexbox for one-dimensional layouts — a row of nav links or a vertical stack. Use CSS Grid when you need rows and columns together — full page layouts or card grids.
Yes. Flexbox is supported in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile browsers. It is safe for production use.