Your first flex row
Three divs normally stack on top of each other. One line on their parent changes that:
The parent with display: flex is the flex container, and its direct children are flex
items. Only the children are affected, not the grandchildren. Delete the display: flex line and
the drinks go back to one per line. Each item is as wide as its text, and gap: 12px keeps them apart.
Main axis and cross axis
Every flex container has two directions. The main axis is the way the items flow: left to right in a row. The cross axis runs the other way: top to bottom in a row. Almost every flex property works on one of the two:
| Property | Works along | In a row, that means |
|---|---|---|
justify-content | The main axis | Left and right |
align-items | The cross axis | Up and down |
flex-grow, flex-shrink, flex-basis | The main axis | Width |
Remembering it this way saves a lot of guessing. When you switch to a column, the axes swap with it, so
justify-content starts working up and down.
flex-direction: row or column
flex-direction picks the main axis. The default is row. Here it’s column, so
the items stack, but they still get flexbox’s gap and alignment:
There’s also row-reverse and column-reverse, which flip the order on screen. Try
row in the example to put the steps side by side. A column also works well for a card where you want a
button pinned to the bottom, which you’ll see further down.
justify-content
justify-content decides what happens to the spare room along the main axis. Five values, five rows:
flex-startis the default. Items pack to the start and the spare room is left at the end.centerputs the items together in the middle.space-betweenpushes the first and last items to the edges and spreads the rest evenly between.-
space-evenlymakes every gap the same, edges included. There’s alsospace-around, where the edge gaps are half the size of the inner ones.
It only does something when there’s spare room. If the items already fill the row, all five look the same.
align-items
align-items lines the items up on the cross axis. In this 90px tall bar they’re centered vertically:
Change center to each of these and watch the yellow boxes:
-
stretchis the default: every item grows to the full height of the row. That’s why flex items so often come out as tall as their tallest neighbor. flex-startandflex-endpush them to the top or bottom.-
baselinelines up the first line of text in each box, so the big “Menu” and the small words sit on the same line. It looks right for text of mixed sizes.
gap
gap sets the space between items, and only between them: there’s none before the first or after the
last. Every example on this page uses it. gap: 12px sets both directions, which matters once items
wrap onto a second line; gap: 8px 16px sets the space between lines first, then between items in a line.
It’s simpler than giving items margins, because you never have to remove the margin from the last one.
flex-wrap
By default a flex row never wraps. When the items don’t fit, they shrink, and if they can’t shrink any further they spill out of the box. Both rows here hold the same three 90px tags in a 240px box:
The first row squeezes the tags, so “Gluten free” breaks onto two lines. The second has
flex-wrap: wrap, so the third tag moves down to a new line and keeps its width. Use
wrap for anything that should reflow on a phone: tags, buttons, a row of cards.
flex: 1, grow, shrink and basis
These decide how wide each item is. A search bar is the usual first need: the box should take all the space the button doesn’t.
flex: 1 on the input says “take the leftover room”. The button stays its normal size. The
min-width: 0 lets the box shrink on a narrow phone (more on that under gotchas).
flex is a shorthand for three properties:
flex-grow: how much of the spare room the item takes. 0 means none.-
flex-shrink: how much it gives up when the row is too full. 0 means it never gets smaller than its basis. -
flex-basis: the size it starts from before growing or shrinking.automeans “use my width, or my content’s size”.
| You write | Same as | What it does |
|---|---|---|
| (nothing) | 0 1 auto | Content size, can shrink, never grows |
flex: 1 | 1 1 0% | Starts from zero and shares the room, so items with flex: 1 come out equal |
flex: auto | 1 1 auto | Starts from its content size, then shares what’s left |
flex: none | 0 0 auto | Stays its own size no matter what |
flex: 0 0 200px | 0 0 200px | Exactly 200px wide, like a fixed sidebar |
flex: 2 | 2 1 0% | Twice the share of a flex: 1 neighbor |
The difference between flex: 1 and flex: auto is easiest to see:
With flex: 1 both boxes are usually the same width, however much text they hold, because they
both start from 0. (A long word or web address can stop one from shrinking, see gotchas.) With flex: auto the box with more text starts wider and stays wider. Shrinking works the other
way round: when the row is too full, items give up room in proportion to their flex-shrink times their
size, so bigger items lose more.
align-self and order
Two properties go on a single item instead of the container. align-self overrides
align-items for that item, and order moves it in the row:
The cookie comes last in the HTML, but order: -1 puts it first, and align-self:
flex-end drops it to the bottom while the others stay at the top. Every item starts at order:
0, and lower numbers come first.
order only changes what you see. The Tab key, screen readers and copy-paste still follow the HTML, so
a reordered row can be confusing to use with a keyboard. If the order matters, change it in the HTML.
Center anything
This is the most copied flexbox snippet. Two lines center the child both ways:
justify-content: center centers along the row, align-items: center across it. The container
needs some height for the vertical part to show, here min-height: 140px. For a full-screen welcome
panel you’d use min-height: 100vh. More ways to center are in
how to center text in HTML.
A nav bar
A row of links with the logo at the start, and one link pushed all the way to the right:
margin-left: auto on the Order link soaks up all the spare room on its left, so it moves to the far
end (anything after it in the HTML would go along too). Use it whenever one thing in a row should sit apart from the rest.
The navbar tutorial builds a full menu with hover styles, a sticky bar and a
phone menu.
A row of cards
Cards that sit side by side on a laptop, wrap on a phone, come out the same height, and keep their buttons lined up along the bottom:
-
flex: 1 1 160pxlets each card grow and shrink from 160px. When three don’t fit,flex-wrap: wrapmoves one down. - The cards are as tall as the tallest one, because of the default
align-items: stretch. -
Each card is a flex column too, and
margin-top: autoon the button pushes it to the bottom, the same trick as the nav bar, turned on its side.
One downside: a card that wraps alone onto the last line grows to the full width. If you want every card the same width in neat columns, that’s a grid. The CSS grid tutorial shows the same cards with grid, and when to pick which.
Gotchas
Flex items won’t shrink below the width of their content unless you let them. Both rows below should cut a long order off with “…”, but only the second does:
A flex item starts with min-width: auto, which means “never narrower than my content”. The first
.info refuses to shrink and gets cut off by the box’s overflow: hidden, so the dots never
appear. min-width: 0 on the item lets it shrink, and the ellipsis works. The same fix helps with long
links, wide tables and code blocks that push a flex layout off the side of a phone screen.
- Flex on the wrong element.
display: flexgoes on the parent, not on the items you want to line up. - Expecting grandchildren to line up. Only direct children are flex items. A wrapper div in between becomes the one item.
- Using
justify-contentto center vertically in a row. In a row, up and down isalign-items. In a column it’s the other way round. - Items squashed on a phone. The row doesn’t wrap by default. Add
flex-wrap: wrap, orflex: noneon things that must keep their size, like an icon.
To get a feel for all of this, click through the settings in the CSS Flexbox Generator and watch the boxes move.
Questions people ask
What is flexbox in CSS?
Flexbox is a layout mode for lining up items in one direction, a row or a column. You turn it on with display: flex on the parent, and its direct children become flex items that you can space out with justify-content, line up with align-items and separate with gap.
What is the difference between justify-content and align-items?
justify-content works along the main axis, the direction the items flow, so left to right in a row. align-items works across it, so up and down in a row. In a column (flex-direction: column) they swap: justify-content moves items up and down and align-items moves them left and right.
How do I center a div with flexbox?
Make its parent a flex container and center both ways: .parent { display: flex; justify-content: center; align-items: center; min-height: 300px; }. The parent needs some height for the vertical centering to be visible; min-height: 100vh centers in the whole screen.
What does flex: 1 mean?
flex: 1 is short for flex-grow: 1, flex-shrink: 1 and flex-basis: 0%. The item starts from zero width and takes an equal share of the free room, so several items with flex: 1 usually come out the same width whatever their content (a long unbreakable word or URL can stop one shrinking; min-width: 0 fixes that). flex: auto (1 1 auto) starts from the content size instead, so longer content stays wider.
Why are my flex items not wrapping?
Flex containers don't wrap by default (flex-wrap: nowrap). Items shrink to fit on one line and spill out when they can't shrink any more. Add flex-wrap: wrap to the container and items move to a new line when they run out of room.
Why does my flex item overflow its container?
Flex items have min-width: auto, so they won't get narrower than their content. A long word, a URL, a wide table or text with white-space: nowrap keeps the item wide and pushes it out of the container. Add min-width: 0 to the flex item; that also makes text-overflow: ellipsis work inside it.
How do I push one item to the right in a flex row?
Give it margin-left: auto. The auto margin takes all the spare room on its left, so the item and anything after it move to the far end. It is the usual way to put a Sign in or Order link on the right of a nav bar while the other links stay on the left.
Should I use flexbox or grid?
Use flexbox when things line up in one direction and their own size should decide the layout: a nav bar, a row of buttons, a search box next to its button. Use grid when you want rows and columns at the same time, like a card grid with equal columns or a whole page layout. Many pages use both.
Does the order property change the tab order?
No. order only changes where an item appears on screen. The Tab key, screen readers and text selection still follow the order in the HTML, so a heavily reordered row can be confusing for keyboard users. When the order matters, change the HTML.
Tools that help
- CSS Flexbox Generator Click through every flex setting on a live row and copy the CSS.
- CSS Grid Generator For layouts with rows and columns at the same time.
- HTML Editor Try a flex layout on a whole page with your own HTML.
- Responsive HTML Viewer See how your wrapping row looks on a phone, tablet and laptop at once.