Flexbox is the part of Cascading Style Sheets (CSS) built for lining things up: menu links in a row, buttons in a toolbar, cards that share the width of a page. By the end of this guide you will be able to turn any element into a flex container, control the direction and wrapping of its items, share out spare space, and center content both ways.
What flexbox is for
MDN Web Docs’ Basic concepts of flexbox describes flexbox as a one-dimensional layout model for distributing space between items, with many alignment features. One-dimensional means it handles one row or one column at a time. CSS Grid, by contrast, controls rows and columns together.
MDN’s flexbox learning module lists jobs that flexbox makes easy: vertically centering a block inside its parent, giving all children of a container an equal share of the space, and making columns the same height even when their content differs.
Step 1: Create a flex container
Set display: flex on the parent element. Its direct children become flex items.
<nav class="menu">
<a href="/">Home</a>
<a href="/courses">Courses</a>
<a href="/contact">Contact</a>
</nav>
.menu {
display: flex;
}
With only that one line, the starting values take over. The links sit in a row and start from the start edge. They do not grow to fill extra space, but they can shrink. They stretch to the full height of the container, and they stay on a single line, overflowing if they are too wide to fit.
Step 2: Understand the two axes
Everything in flexbox refers to two axes. The main axis is set by flex-direction, which takes four values: row (the default), row-reverse, column and column-reverse. The cross axis runs at a right angle to it. With row, the main axis runs along the line of text and the cross axis runs down the page. With column, they swap.
Flexbox talks about start and end rather than left and right, because it follows the writing mode. In English a row starts on the left; in Arabic it starts on the right.
Step 3: Let items wrap and share space
By default flex-wrap is nowrap. Set it to wrap and items that do not fit move onto a new line. MDN advises treating each line as a separate flex container: space is shared within a line, not across lines. The flex-flow shorthand sets both properties at once, for example flex-flow: row wrap.
The flex property on each item controls how leftover space is shared. It combines three values in this order: flex-grow, flex-shrink and flex-basis. Here is a row of cards:
.cards {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.card {
flex: 1 1 200px;
}
Each card starts from a base width of 200 pixels, can grow to share spare space, and can shrink. Because wrapping is on, cards move to the next line when there is no room. The gap property, explained on MDN’s gap reference, adds space between flex items and between lines; one value sets both.
The grow numbers are proportions. MDN’s example: three 100-pixel items in a 500-pixel container leave 200 pixels of available space. Give the first item a grow value of 2 and the others 1, and the space is split into four parts: 100 pixels to the first item and 50 to each of the others. You will often see flex: 1, which MDN notes is the same as flex: 1 1 0, so items start from zero and share all the space.
Step 4: Align and center items
Two properties on the container handle alignment. justify-content positions items along the main axis, with values such as flex-start, center, space-between and space-evenly. align-items positions them on the cross axis; its initial value, stretch, is why items fill the container’s height. To center something both ways:
.hero {
display: flex;
justify-content: center;
align-items: center;
min-height: 300px;
}
The container needs a height, here a minimum height, for vertical centering to show. To align a single item differently from the rest, set align-self on that item.
Common mistakes
- Setting display: flex on the wrong element. Only direct children become flex items. A flex item can itself be a flex container for its own children.
- Assuming justify-content is always horizontal. With
flex-direction: column, the main axis runs top to bottom, sojustify-contentworks vertically. - Reaching for justify-items. It is ignored in flexbox layouts.
- Forgetting to wrap. With the default
nowrap, items shrink and can overflow the container. - Reordering with the order property. It changes the visual order only. The Tab key still follows the source order, which can confuse keyboard users.
Flexbox comes down to a few ideas: a container and its direct children, a main axis and a cross axis, and properties that share space and align items along them. Start with display: flex, add gap and flex-wrap, then reach for justify-content and align-items. When your layout needs rows and columns lined up together, that is the point to switch to CSS Grid.


