Flexbox

You want three buttons sitting in a row, evenly spaced, all the same height, and vertically centred no matter how much text each one holds. In the old days this meant floats, clearfixes, and a lot of guessing. Flexbox is the tool built for exactly this: you tell one container to lay its children out in a line, and then you nudge them along two axes until they sit where you want. Most of the everyday layout you see on the web, nav bars, toolbars, rows of cards, is a flex container underneath.
The flex container
Flexbox starts with one line on a parent element. Set display: flex on a container and its direct children line up in a row, side by side. That container is now a flex container, and each child inside it becomes a flex item.
.nav {
display: flex; /* the children now sit in a row */
}The items lay out along two directions. The main axis runs left to right, the way the items line up. The cross axis runs top to bottom, across the row. You will steer items along both of these, so it helps to picture them now: the main axis is the line the items sit on, the cross axis is the direction across it.
.nav {
display: flex;
}A single declaration, and the children of any block stop stacking top to bottom and start sitting in a row. Everything else in this chapter is about steering those items once they are in the container.
display: flex on a parent and its direct children line up in a row. That parent is the flex container, the children are flex items. The items move along two directions: the main axis is the line they sit on, the cross axis runs across it, and you will push items along both. Aligning along the axes
Once your items are in a row, two properties move them around. justify-content spaces items along the main axis, the direction they sit in. align-items moves them along the cross axis, up and down within the row.
.nav {
display: flex;
justify-content: space-between; /* push items apart along the row */
align-items: center; /* line them up in the vertical middle */
}The common values for justify-content are flex-start (bunched at the left), center (bunched in the middle), flex-end (bunched at the right), space-between (gaps between items, none at the ends), and space-around (gaps on both sides of each item). For align-items, center is the one you reach for most.
To centre something both ways, the move everyone learns first: set both to center.
.hero {
display: flex;
justify-content: center; /* centred along the main axis */
align-items: center; /* centred along the cross axis */
}justify-content spaces items along the main axis, align-items moves them along the cross axis. The values to remember for the first are center, space-between, and flex-start. Set both to center and whatever is inside sits dead in the middle, which is the layout move you will use over and over. Direction and wrapping
By default a flex container lays items out in a row. Change that with flex-direction. Set it to column and the items stack top to bottom instead of sitting side by side.
.sidebar {
display: flex;
flex-direction: column; /* items stack vertically now */
}When items in a row run out of room, they get squashed thinner and thinner. To let them drop onto a new line instead, add flex-wrap: wrap.
.card-row {
display: flex;
flex-wrap: wrap; /* items that do not fit move to the next line */
}For the space between items, use gap. It adds a consistent gap between every item without adding space at the outer edges.
.card-row {
display: flex;
gap: 16px; /* 16px between each item */
}flex-direction: column stacks items vertically instead of in a row. flex-wrap: wrap lets items drop to a new line when they run out of room, instead of getting squashed. And gap puts even space between items without adding any at the edges, so reach for it before margins. Flexible item sizing
The point of flexbox is that items can flex, growing or shrinking to share the space. The shorthand you will use most is flex: 1 on an item, which tells it to grow and take an equal share of whatever room is available.
.card {
flex: 1; /* every card grows to fill an equal share of the row */
}Put flex: 1 on three cards in a row and they split the container into three equal widths. Give one card flex: 2 and it takes twice as much space as the others. That single number is how items divide up the leftover room between them.
Behind flex: 1 are three separate controls: how much an item grows, how much it shrinks, and its starting size. You rarely set them one by one at first, the shorthand covers the common cases, but it helps to know they are there.
.layout {
display: flex;
gap: 24px;
}
.sidebar {
flex: 0 0 240px; /* fixed 240px, never grows or shrinks */
}
.main {
flex: 1; /* takes all the remaining space */
}A fixed sidebar beside a flexible main column, three lines of intent. This one pattern, plus the aligned nav bar from earlier, covers a large slice of the layouts you will build.
flex: 1 on an item tells it to grow and take an equal share of the row, so three cards with flex: 1 come out the same width. Bump one to flex: 2 and it takes twice the space. That one number is how flex items split up the room between them. 
