CSS Flexbox Tutorial for Beginners (with Pictures)
Flexbox is the tool you will use almost every day. It puts boxes side by side, spaces them nicely, and centers things with two lines of code. We will go slowly, one picture at a time.
The problem flexbox solves
In the box model chapter you saw that block boxes like <div> always stack on top of each other. That is fine for text. But real websites need things side by side: a logo and a menu, three product cards, a photo next to some text.
Years ago, developers used tricks to do this, and the tricks broke easily. Flexbox was made to solve this problem properly. Today every modern browser supports it, so you can use it without fear.
<div class="row">
<div>1</div>
<div>2</div>
<div>3</div>
</div>Without flexbox: three boxes, one under the other. Let us fix that.
Two parts: the container and the items
Flexbox always has two parts. You need to know these two words:
- Flex container: the parent element. You give it
display: flex. - Flex items: the direct children of that parent. They follow the container's rules.
display: flex. Its direct children become flex items..row {
display: flex;
}One line of CSS, and the boxes are in a row.
display: flex too.Two directions: main axis and cross axis
Flexbox does not think "left/right" and "up/down". It thinks in two axes (an axis is just a line with a direction):
- The main axis is the direction the items line up. For a row, it goes left to right.
- The cross axis goes across it. For a row, it goes top to bottom.
There is a different property for each axis:
| Property | Works on | In a normal row |
|---|---|---|
justify-content | Main axis | Left ↔ right |
align-items | Cross axis | Up ↕ down |
justify-content: spread items along the row
justify-content decides what to do with the empty space on the main axis. Here are all the values in one picture:
justify-content moves items along the main axis..row {
display: flex;
justify-content: space-between;
}You will use space-between (logo left, menu right) and center the most.
space-between to center, then flex-end. Save and reload after each change. align-items: line up items up and down
align-items does the same job on the cross axis. You only see a difference when the container is taller than the items, or when the items have different heights.
align-items moves items along the cross axis (up and down in a row). stretch is the default..row {
display: flex;
align-items: center;
height: 140px;
}is
tall
Notice the default is stretch. That is why, in a flex row, all boxes become the same height. Many beginners get confused by this, so keep it in mind.
The famous perfect center
Centering a box used to be hard in CSS. With flexbox it takes three lines:
.hero {
display: flex;
justify-content: center; /* center on main axis */
align-items: center; /* center on cross axis */
height: 200px;
}flex-direction: rows or columns
By default items go in a row. Change flex-direction to column and they go top to bottom. Look at the arrow in the picture. The main axis turns:
column the main axis turns down. Now justify-content works up and down, and align-items works left and right..stack {
display: flex;
flex-direction: column;
gap: 8px;
}justify-content moves things up and down, and align-items moves them left and right. They swap because the axes turned. If something is not moving the way you expect, check the direction first.gap: easy space between items
Want space between the items but not on the outside edges? Use gap. It is much easier than adding margins to every item.
.row {
display: flex;
gap: 20px;
}flex-wrap: when items don't fit
By default flexbox keeps every item on one line, even if it has to squeeze them very thin. On a phone, this looks bad. flex-wrap: wrap lets items move to the next line:
.gallery {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.gallery > div {
width: 150px;
}Make your browser window narrow and wide. The items move to new lines by themselves.
flex: let items grow
Sometimes you want one item to fill all the empty space, like a search box in a toolbar. The flex property does this:
flex: 1 means "take a share of the free space". flex: 2 takes two shares..search { flex: 1; } /* takes all free space */
.main { flex: 2; } /* takes 2 shares */
.side { flex: 1; } /* takes 1 share */The free space is shared out. An item with flex: 2 gets twice as much extra space as an item with flex: 1. Items without flex get no extra space.
flex: 1 1 200px means: grow, shrink if needed, and start at 200px wide. Use it with flex-wrap: wrap to make a card row that shows 3 cards on a laptop and 1 on a phone.A small trick: margin auto
In a flex row, margin-left: auto on one item pushes it, and everything after it, to the far right.
auto margin pushes items away. Very handy for menus..about {
margin-left: auto;
}align-self: one item breaks the rule
align-items works on all items. If you want just one item to be different, give that item align-self:
.row { display: flex; align-items: flex-start; height: 120px; }
.last { align-self: flex-end; }Common mistakes (and quick fixes)
| Problem | Fix |
|---|---|
| Nothing happens | Did you put display: flex on the parent, not on the items? |
align-items: center does nothing | The container has no extra height. Give it a height or min-height. |
| Items move the wrong way | Check flex-direction. In a column the axes swap. |
| Items are squeezed on phones | Add flex-wrap: wrap. |
| Images stretch strangely | Add align-items: flex-start or center to stop the default stretch. |
Cheat sheet
| Put on the container | What it does |
|---|---|
display: flex | Turn on flexbox |
flex-direction | row or column |
justify-content | Space on the main axis |
align-items | Line up on the cross axis |
gap | Space between items |
flex-wrap | Allow new lines |
| Put on an item | What it does |
|---|---|
flex: 1 | Grow to fill free space |
align-self | Different alignment for this one item |
margin-left: auto | Push this item (and the rest) to the right |
Practice time
- Build the navbar from this chapter, then add a fourth link.
- Make a full-screen box (
height: 100vh) and put a heading in the exact center. - Make a row of 6 cards with
flex-wrap. Check it on the phone view in your browser. - Make a toolbar: a logo, a search box with
flex: 1, and a button.
Quick summary
- Flexbox = a container (parent) and items (children).
justify-contentworks on the main axis,align-itemson the cross axis.flex-direction: columnturns the main axis, so the two properties swap.gapfor space,flex-wrapfor small screens,flex: 1to grow.- Real layouts are flex boxes inside flex boxes.