Lesson 5 of 7 · 5 min
Flexbox
Lay out items in a row or a column, align them on both axes, share space between them, and build the navigation bars and card rows you see everywhere.
Flexbox lays out items along one line, either a row or a column. It is the right tool for navigation bars, toolbars, button groups, and any row of things that need to line up.
Turn it on
.nav {
display: flex;
}
The .nav element is now a flex container, and its direct children are flex items. By default they sit in a row, as wide as their content.
Main axis and cross axis
flex-direction sets the main axis:
flex-direction: row; /* left to right (default) */
flex-direction: column; /* top to bottom */
The cross axis runs at right angles to it. Two properties align items, one per axis:
.nav {
display: flex;
justify-content: space-between; /* main axis */
align-items: center; /* cross axis */
}
justify-content |
Effect |
|---|---|
flex-start |
Pack at the start |
center |
Pack in the middle |
space-between |
First and last at the edges, equal space between |
space-around / space-evenly |
Space around every item |
align-items takes stretch (default), flex-start, center, flex-end or baseline.
gap
Space between items, without margins on the first or last:
.actions {
display: flex;
gap: 0.75rem;
}
Wrapping
By default items stay on one line and shrink. Let them wrap onto new lines instead:
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
Growing and shrinking
Each item has three flex properties, usually written together with the flex shorthand:
.item { flex: 1; } /* grow to share leftover space equally */
.sidebar { flex: 0 0 16rem; } /* don't grow, don't shrink, start at 16rem */
.search { flex: 1 1 18rem; } /* start at 18rem, grow or shrink as needed */
The three values are flex-grow, flex-shrink and flex-basis.
Common patterns
A site header: logo on the left, links on the right.
.header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
}
Push one item to the end: an auto margin takes all the free space.
.nav .login { margin-left: auto; }
Perfect centering:
.hero {
display: flex;
align-items: center;
justify-content: center;
min-height: 60vh;
}
A media object: an image next to text that fills the rest.
.media { display: flex; gap: 1rem; align-items: flex-start; }
.media img { flex: none; width: 4rem; }
.media .body { flex: 1; min-width: 0; }
min-width: 0 lets a flex item shrink below the width of its content, which stops long words or code from overflowing.
Try it
Build a header with a logo and three links. Make the links sit on the right with gap: 1rem. Then add flex-wrap: wrap and narrow the window until the links drop to a second line.
More from CSS Crash Course
- Lesson 7 · 5 minResponsive design and motionMobile-first CSS, media and container queries, fluid widths, responsive images, and transitions and animations that respect people who prefer less motion.
- Lesson 1 · 5 minHow CSS worksRules, selectors and declarations, three ways to add CSS to a page, and how the cascade and inheritance decide what you see.
- Lesson 2 · 5 minSelectors and specificityTarget exactly the elements you mean with type, class, attribute and pseudo-class selectors, and learn how the browser scores them.