Skip to content

CSS Flexbox Tutorial: Flex Layout Explained with Examples

Flexbox lines things up in one direction, either a row or a column. Put display: flex on a parent and its children sit side by side. From there, justify-content spreads them along the row, align-items lines them up across it, and gap puts space between them. It’s what most sites use for nav bars, button rows, rows of cards and centering one thing inside a box.

Your first flex row

Three divs normally stack on top of each other. One line on their parent changes that:

display: flex on a menu

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:

PropertyWorks alongIn a row, that means
justify-contentThe main axisLeft and right
align-itemsThe cross axisUp and down
flex-grow, flex-shrink, flex-basisThe main axisWidth

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:

flex-direction: column

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:

Five justify-content values
  • flex-start is the default. Items pack to the start and the spare room is left at the end.
  • center puts the items together in the middle.
  • space-between pushes the first and last items to the edges and spreads the rest evenly between.
  • space-evenly makes every gap the same, edges included. There’s also space-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:

align-items: center

Change center to each of these and watch the yellow boxes:

  • stretch is 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-start and flex-end push them to the top or bottom.
  • baseline lines 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:

nowrap squeezes, wrap makes a new line

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 search box

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. auto means “use my width, or my content’s size”.
You writeSame asWhat it does
(nothing)0 1 autoContent size, can shrink, never grows
flex: 11 1 0%Starts from zero and shares the room, so items with flex: 1 come out equal
flex: auto1 1 autoStarts from its content size, then shares what’s left
flex: none0 0 autoStays its own size no matter what
flex: 0 0 200px0 0 200pxExactly 200px wide, like a fixed sidebar
flex: 22 1 0%Twice the share of a flex: 1 neighbor

The difference between flex: 1 and flex: auto is easiest to see:

flex: 1 next to flex: auto

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:

One item with align-self and order

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:

Centered 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 row of links with the logo at the start, and one link pushed all the way to the right:

A nav bar with one link pushed 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:

Cards with buttons along the bottom
  • flex: 1 1 160px lets each card grow and shrink from 160px. When three don’t fit, flex-wrap: wrap moves 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: auto on 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:

text-overflow needs min-width: 0

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: flex goes 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-content to center vertically in a row. In a row, up and down is align-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, or flex: none on 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