Lesson 20 of 29 · Layout with Flexbox
CSS flex-wrap and flex-grow: Rows That Wrap and Stretch
In the last lesson every flex item kept its natural size, and all items stayed in one line. Real layouts need more: a search field that fills the space next to its button, or a row of cards that wraps onto a new line when the screen gets narrow. That's what flex-wrap and the flex-grow, flex-shrink and flex-basis properties are for.
Wrapping onto new lines
By default a flex container squeezes all its items into a single line (flex-wrap: nowrap), even when they get uncomfortably narrow. With flex-wrap: wrap, items that don't fit move to the next line:
.tags {
display: flex;
flex-wrap: wrap;
gap: 8px; /* used between items and between lines */
}
How big is a flex item?
Three properties on the items control their size along the main axis:
flex-basisis the starting size, before any free space is shared out. In a row it works like a width:flex-basis: 200px. The default,auto, uses the item'swidthor its content.flex-growdecides who gets the leftover space. It is0by default, so items don't grow. An item withflex-grow: 1takes a share of the free space, and an item withflex-grow: 2takes twice as much as one with1.flex-shrinkdecides who gives up space when there isn't enough. It is1by default, so all items shrink evenly.flex-shrink: 0keeps an item at its basis, which is handy for icons and logos.
A search bar is the classic example of flex-grow. The button keeps its natural size, and the input takes everything else:
.search {
display: flex;
gap: 8px;
}
.search input {
flex-grow: 1;
}
The flex shorthand
You'll rarely write the three properties one by one. The flex shorthand sets them all at once, in the order grow, shrink, basis:
.card {
flex: 1 1 200px; /* grow: 1, shrink: 1, basis: 200px */
}
Two shortcuts are worth remembering: flex: 1 means 1 1 0%, so all items share the row equally whatever their content, and flex: none means 0 0 auto, so the item keeps its natural size.
Put flex-wrap: wrap on the container and flex: 1 1 200px on the items, and you get a responsive card row without a single media query. Every card starts at 200px, as many as fit share a row and grow to fill it, and the rest wrap onto the next line. On a phone, each card gets a row of its own.
Common mistakes
- Putting
flex-growon the container. Grow, shrink and basis belong on the items;flex-wrapbelongs on the container. - Forgetting
flex-wrap: wrap. Without it, the items shrink into one long, cramped line instead of moving to the next one. - Expecting
flex-growto make everything equal. Growing only shares out the free space, so an item with a bigger basis or more content stays bigger. For exactly equal items, useflex: 1.
Assignment
Finish the realm search page:
- Make the search input grow to fill all the free space in
.search(flex-grow: 1). The form is already a flex container. - Make
.cardsa flex container that wraps (flex-wrap: wrap), with a16pxgap. - Give every
.cardthe shorthandflex: 1 1 200px.
On the 1024px-wide test page, four cards fit in the first row and grow to fill it, and the fifth card wraps onto a second row of its own. On a 600px screen, the cards form rows of two.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .