What Is Flexbox and How Does It Actually Work?
Hello there, box wrangler!
Flexbox explained in one sentence: it is how CSS lines boxes up in a row or a column and decides who gets the leftover space, and once that clicks, a huge chunk of everyday layout stops being a fight.
Navbars, button groups, cards sitting side by side, a footer pinned to the bottom of a card, that’s all flexbox territory.
If you like learning from a book next to your editor, there are good the CSS Visual Dictionary paperback on Amazon, but this one picture and a few lines of code will get you most of the way today.
Short answer: Flexbox is a CSS layout mode that lines up a box’s direct children in one direction, a row or a column, and shares out the free space between them. You turn it on with display: flex on the parent; then justify-content places items along the main axis and align-items along the cross axis.
What the diagram shows, in plain text:
- Left,
flex-direction: row(the default): the flex container holds items 1, 2 and 3 side by side. The main axis runs left to right, from main-start to main-end;justify-contentworks along it. The cross axis runs top to bottom, from cross-start to cross-end;align-itemsworks along it. The unused room at the end is the free space. - Right,
flex-direction: column: the same three items stack top to bottom. Now the main axis points down and the cross axis runs left to right. - The rule: change
flex-directionand the two axes swap;justify-contentalways follows the main axis.
What is flexbox in CSS?
Flexbox (the CSS Flexible Box Layout Module) is a one-dimensional layout system: it arranges a parent’s children along a single line, either a row or a column, and lets them grow or shrink to fit.
The W3C specification, written by the CSS Working Group, sums it up in its first line:
In the flex layout model, the children of a flex container can be laid out in any direction, and can “flex” their sizes, either growing to fill unused space or shrinking to avoid overflowing the parent.
Before flexbox, people centered and spaced things with floats, inline-block and margin hacks. Flexbox replaced most of that with a handful of properties that say what you mean: put these in a row
, “center them”, give the extra room to this one
.
How do you turn on flexbox?
You turn flexbox on by giving the parent element display: flex. That parent becomes the flex container, and its direct children become flex items. Nothing else is needed — the items line up in a row straight away.
<div class="toolbar">
<button>Save</button>
<button>Share</button>
<button>Delete</button>
</div>
.toolbar {
display: flex; /* the three buttons now sit in a row */
gap: 8px; /* 8px of space between them */
}
There’s also display: inline-flex. It works the same inside, but the container itself sits inline in the text like a word, instead of taking a full line like a block.
The W3C spec describes it as a flex container that is inline-level when placed in flow layout
.
Which elements become flex items?
Only the direct children of a flex container become flex items — grandchildren don’t.
If a card inside the container has its own title and text, those stay normal block content inside the card unless you give the card display: flex too. That’s how nested layouts are built: a row of cards, each card a column.
What are the main axis and the cross axis in flexbox?
Every flex container has two axes: the main axis, the direction the items are laid out in, and the cross axis, at a right angle to it.
The W3C spec defines the main axis as the primary axis along which flex items are laid out
. Learning which axis is which is the single biggest step in understanding flexbox, because every alignment property works on one axis or the other.
- Main axis: the flow of the items. In a row it runs left to right (in English and other left-to-right languages); in a column it runs top to bottom.
- Cross axis: across the flow. In a row it runs top to bottom; in a column, left to right.
What does flex-direction change?
flex-direction picks the main axis, and the cross axis turns with it. Its four values are row (the default), row-reverse, column and column-reverse.
Switch a flex container from row to column and the items stack, justify-content now moves them up and down, and align-items now moves them left and right — exactly the swap in the diagram above.
How do you line up and space flex items?
Flexbox has one property for each axis: justify-content places items along the main axis and align-items places them along the cross axis.
With both set to center, the items sit dead center in the container — the famous two-line answer to how do I center a div
. Our justify-content vs align-items guide draws every value to scale and lists the six reasons they “do nothing”.
.hero {
display: flex;
justify-content: center; /* main axis: center left-right (in a row) */
align-items: center; /* cross axis: center top-bottom */
min-height: 300px;
}
What does justify-content do?
justify-content shares out the free space along the main axis. In the Level 1 spec its values are flex-start (the default: pack items at the start), flex-end, center, space-between (first and last items touch the edges, equal gaps between) and space-around.
Browsers today also accept space-evenly and plain start and end from the newer CSS Box Alignment module. If there’s no free space — the items already fill the row — justify-content has nothing to move, which is the most common reason it seems to “do nothing”.
What does align-items do?
align-items lines items up on the cross axis. Its default is stretch, which is why flex items in a row grow to the same height as the tallest one — handy for equal-height cards.
The other values are flex-start, flex-end, center and baseline (line up the first line of text in each item). To move just one item, give that item align-self.
How do you add space between flex items?
Use gap on the flex container: gap: 16px puts 16 pixels between every pair of items, never on the outside edges. It replaced the old trick of adding margins and then removing the last one.
According to Can I use, gap for flexbox works in Chrome and Edge 84+, Firefox 63+ and Safari 14.1+, about 97.3% of users worldwide.
How do flex items grow and shrink?
Each flex item has three sizing settings: flex-basis (its starting size), flex-grow (how much of the leftover space it takes) and flex-shrink (how much it gives up when space runs out).
By default, per the W3C spec, flex-grow is 0, flex-shrink is 1 and flex-basis is auto — so items keep their natural size, never grow, but do shrink when the row gets too tight. Our flex-grow, flex-shrink and flex-basis tutorial draws exactly how the space is shared, to scale.
.sidebar { flex: 0 0 240px; } /* fixed 240px: never grows, never shrinks */
.content { flex: 1; } /* takes all the space that's left */
What does flex: 1 mean?
flex: 1 is shorthand for flex-grow: 1, flex-shrink: 1 and flex-basis: 0: the item starts from zero and takes an equal share of the free space.
Give three items flex: 1 and you get three equal columns; give one of them flex: 2 and it gets twice the share.
The other shorthand keywords from the spec are flex: initial (0 1 auto, the default), flex: auto (1 1 auto) and flex: none (0 0 auto, fully rigid).
What happens when flex items don’t fit on one line?
By default a flex container never wraps (flex-wrap: nowrap): the items shrink to squeeze onto one line, and if they can’t shrink any more they overflow.
Honestly, the axes are the whole game. Every flexbox why won’t this move?!
moment I can think of comes down to pushing on the wrong axis — so if you remember one thing from this page, make it the picture at the top.
Set flex-wrap: wrap and items that don’t fit move onto a new line instead — the basis of many responsive card rows. Once there’s more than one line, align-content controls how those lines are spaced on the cross axis.
.cards {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.card { flex: 1 1 220px; } /* at least about 220px wide, then share the rest */
Which flexbox properties go on the container, and which on the items?
Flexbox properties split into two groups: six you set on the flex container and four you set on the flex items.
Mixing them up (say, justify-content on a child) is one of the most common reasons a flexbox layout won’t budge. Here is the full map with each property’s default value and the axis it acts on.
| Property | Set it on | Default (initial value) | Works along |
|---|---|---|---|
flex-direction | container | row | sets the main axis |
flex-wrap | container | nowrap | main axis (new lines stack on the cross axis) |
justify-content | container | flex-start | main axis |
align-items | container | stretch | cross axis |
align-content | container (with more than one line) | stretch | cross axis (the lines) |
gap | container | normal (no gap) | between items and lines |
flex-grow | item | 0 | main axis |
flex-shrink | item | 1 | main axis |
flex-basis | item | auto | main axis |
align-self | item | auto (uses the container’s align-items) | cross axis |
A quick memory trick from the table: everything with “justify” or “flex-grow/shrink/basis” lives on the main axis, everything with “align” lives on the cross axis. (gap and order are defined in other CSS modules but work on flex containers and items all the same.)
When should you use flexbox instead of CSS Grid?
Use flexbox when you’re lining things up in one direction — a row of buttons, a navbar, items in a card — and CSS Grid when you need rows and columns at the same time, like a whole page layout or a photo gallery with aligned columns.
Google’s web.dev Learn CSS course calls flexbox a layout model designed for one-dimensional content
. The two work well together: a Grid for the page, flexbox inside each piece. Our flexbox vs Grid guide draws the decision as a three-question diagram.
Does flexbox work in every browser?
Yes — flexbox works in every modern browser, and has for years.
The Can I use data for flexbox, read on November 10, 2026, shows full support for 97.4% of users worldwide, with the unprefixed version supported since Chrome 29, Firefox 28, Safari 9 and every version of Microsoft Edge. You don’t need -webkit- prefixes for flexbox in new projects.
How can you see a flexbox layout in your browser?
Open your browser’s developer tools: both Chrome and Firefox draw the flex container, the items and the free space right on the page.
In Chrome DevTools, a flex badge appears next to the element in the Elements panel — click it to show the overlay — and a flexbox editor button sits next to display: flex in the Styles pane, letting you click through justify-content and align-items values.
Firefox’s Flexbox Inspector does the same, and also explains why each item ended up the size it did.
Go make a row of three boxes, slap display: flex on the parent, and poke at justify-content and align-items until it feels obvious. Ten minutes of playing beats an hour of reading — and you’ll never look at a navbar the same way again.
Sources and further reading
- W3C: CSS Flexible Box Layout Module Level 1 — the specification (Candidate Recommendation Draft, 14 October 2025): property defaults, axes, flex keywords
- MDN Web Docs: Basic concepts of flexbox — the main axis, cross axis and flex container explained
- MDN Web Docs: the flex shorthand — flex-grow, flex-shrink and flex-basis in one property
- Can I use: CSS Flexible Box Layout Module — browser support, 97.4% global full support on November 10, 2026
- Can I use: gap property for flexbox — Chrome/Edge 84+, Firefox 63+, Safari 14.1+
- web.dev Learn CSS: Flexbox — Google’s free CSS course module on flexbox
- Chrome DevTools: inspect and debug CSS flexbox layouts — the flex badge and flexbox editor
- Firefox DevTools: examine flexbox layouts — the Flexbox Inspector
Questions and answers
What is flexbox used for?
Flexbox is used to line elements up in a single row or column and share space between them: navigation bars, toolbars and button groups, centering content, equal-height cards, and footers pushed to the bottom of a card or page. It handles one direction at a time; for rows and columns together, CSS Grid is the better fit.
Is flexbox one-dimensional or two-dimensional?
Flexbox is one-dimensional: it lays items out along one main axis, a row or a column. With flex-wrap it can spill onto extra lines, but each line is sized on its own, so columns won’t line up across lines. CSS Grid is the two-dimensional layout system.
What is the difference between display: flex and display: inline-flex?
Both make the element a flex container and lay out its children the same way. The difference is outside: display: flex makes the container block-level (it takes a full line), while display: inline-flex makes it inline-level, so it sits in a line of text next to other content.
Why is justify-content not working in flexbox?
The three usual reasons: the parent is missing display: flex, justify-content was put on a child instead of the container, or there is no free space on the main axis because the items already fill it (often because of flex: 1 or width: 100%). In a column, remember justify-content works top to bottom.
Should I learn flexbox or grid first?
Most people learn flexbox first, because it covers everyday jobs like rows, centering and spacing with only a few properties, and it introduces the main axis and cross axis ideas that CSS Grid alignment reuses. Learn CSS Grid next for full page layouts.