Flexbox solves layout problems that used to need hacks: centring something vertically, making columns the same height, spreading navigation links across a bar, or letting cards wrap onto new lines as the screen narrows. Yet many developers still guess at the properties, swapping justify-content and align-items until something looks right.
This guide explains the mental model behind flexbox, the two axes, what each container property does, how items share space with flex-grow, flex-shrink and flex-basis, and a handful of layouts you will use again and again.
The flex container and its items
Flexbox starts when you set display: flex on an element. That element becomes the flex container, and its direct children become flex items. Grandchildren are not affected unless their own parent is also a flex container.
.toolbar {
display: flex;
gap: 12px;
}
That single line already changes a lot: items sit side by side in a row, they stretch to the same height, and they shrink if space runs out.
Two axes
Everything in flexbox is relative to two axes:
- The main axis runs in the direction set by
flex-direction. Forrow(the default) it is horizontal; forcolumnit is vertical. - The cross axis is perpendicular to it.
This is why flexbox properties are named after axes rather than "horizontal" and "vertical":
| Property | Works along | Controls |
|---|---|---|
justify-content |
Main axis | How items and free space are distributed |
align-items |
Cross axis | How items are positioned within their line |
align-content |
Cross axis | How multiple wrapped lines are spaced |
align-self |
Cross axis | Override of align-items for one item |
If you change flex-direction to column, justify-content now works vertically and align-items horizontally. Remembering "justify = main, align = cross" resolves most confusion.
Container properties
flex-direction:row,row-reverse,columnorcolumn-reverse.flex-wrap:nowrap(default) squeezes everything onto one line;wraplets items flow onto new lines;wrap-reversewraps upwards.justify-content:flex-start,center,flex-end,space-between(first and last items at the edges),space-around(equal space around each item, so edge gaps are half-size) andspace-evenly(identical gaps everywhere).align-items:stretch(default, fills the cross axis),flex-start,center,flex-endandbaseline(aligns text baselines).align-content: distributes wrapped lines. It has no effect when everything fits on one line or wrapping is off.gap: space between items, without margins on the outer edges.
How items share space
Each item has three sizing properties, usually written with the flex shorthand: flex: grow shrink basis.
flex-basisis the starting size before free space is shared, such as200px,30%,0orauto(use the content or width).flex-growdecides how leftover space is distributed. An item withflex-grow: 2gets twice as much of the extra space as an item withflex-grow: 1.flex-shrinkdecides how items give up space when there is not enough.0means "never shrink below the basis".
A worked example: a 900-pixel container holds two items, both with flex-basis: 200px. That leaves 500 pixels free. If item A has flex-grow: 1 and item B has flex-grow: 4, A gets 100 pixels extra and B gets 400, so they end up 300 and 600 pixels wide.
Common shorthands:
| Shorthand | Means | Typical use |
|---|---|---|
flex: 1 |
1 1 0% |
Equal columns regardless of content |
flex: auto |
1 1 auto |
Grow and shrink, starting from content size |
flex: none |
0 0 auto |
Fixed at content size |
flex: 0 0 240px |
Fixed 240 px | Sidebars |
Two more item properties: order changes the visual position without changing the HTML (use it sparingly, because keyboard and screen reader order still follow the source), and align-self overrides the cross-axis alignment for one item.
Experiment in the Flexbox Playground
Our Flexbox Playground lets you change every property and see the result instantly, with the CSS written for you.
- Open the Flexbox Playground. Four numbered items sit in a container.
- Under Container (flex parent), change
flex-direction,flex-wrap,justify-content,align-itemsandalign-content, and drag the gap slider. A note appears if you setalign-contentwhile wrapping is off, because it will have no effect. - Under Items, click Add (up to 12 items) or Remove, then select an item by its number and set its flex-grow, flex-shrink, flex-basis (for example
auto,0,120pxor30%), order and align-self. - Try the Presets: Center everything, Navbar, Equal columns, Sidebar layout, Wrapping cards and Vertical stack. Each one is a real-world pattern you can study and adjust. Reset returns to the defaults.
- Watch the Live preview, and change the Preview height to see cross-axis alignment more clearly.
- Copy the Generated CSS and the matching HTML structure. Item rules are written as
.item:nth-child(n)only for items you changed.
Layouts you will use often
Centre anything
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
Navbar with a logo left and links right
.nav {
display: flex;
align-items: center;
justify-content: space-between;
}
Push one item to the end
Give that item margin-left: auto. The auto margin absorbs all free space, which is handy for a "Log in" button at the end of a menu.
Wrapping cards
.cards {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.cards > * {
flex: 1 1 220px;
}
Cards are at least around 220 pixels wide, grow to fill each row, and wrap when there is no room.
Flexbox or grid?
Flexbox is one-dimensional: it arranges items along one axis and wraps lines independently. CSS grid is two-dimensional: rows and columns line up with each other. Use flexbox for toolbars, navigation, button groups and content-sized components; use grid when items must align in both directions, such as a page layout or an image gallery with fixed columns. The CSS Grid Generator covers the grid side. Frameworks build on the same ideas: Tailwind's flex, justify-between and items-center classes map directly to these properties, which you can try in the Tailwind CSS Playground.
Troubleshooting
Items overflow instead of shrinking
Flex items have a default minimum size of their content (min-width: auto). Long words or wide images stop them shrinking. Add min-width: 0 to the item, or allow wrapping.
align-items: center does nothing
The container is only as tall as its content, so there is nothing to centre within. Give the container a height or min-height.
Gaps look uneven with space-around
That is how space-around works: edge gaps are half the size of inner gaps. Use space-evenly or gap for equal spacing.
Styling the items
Once the layout works, finish the look with the Box Shadow Generator and Border Radius Generator.
Frequently asked questions
What is the difference between justify-content and align-items?
justify-content distributes items along the main axis, horizontal in a row. align-items positions them along the cross axis, vertical in a row. Switching to a column swaps the directions.
What does flex: 1 mean?
It is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%. Items with flex: 1 share the available space equally, whatever their content.
How do I centre a div horizontally and vertically?
Set the parent to display: flex; justify-content: center; align-items: center; and give it a height, such as min-height: 100vh.
Why does align-content have no effect?
It only works when items wrap onto more than one line. Set flex-wrap: wrap and make sure there are enough items to wrap.
Does the order property affect accessibility?
Yes. It changes only the visual order, while keyboard focus and screen readers follow the HTML order. Reorder the HTML itself when the sequence matters.