Flexbox in practice: wrap, grow, shrink and common patterns
Control how flex items share space with flex-wrap, flex-grow, flex-shrink and flex-basis, then build real patterns.
In this lesson you will
- Wrap items with flex-wrap
- Use the flex shorthand
- Build card rows and search bars
- Create a sticky footer
About 16 minutes
When things get tight
Your row of cards looks great on a laptop. On a phone, five cards in one row squeeze to the width of postage stamps. Or you want one search box to take all the leftover space beside a button. Flexbox handles both with a handful of properties about what items do when there is too much or too little room: wrap, grow, shrink and basis.
flex-wrap: let items drop to the next line
.cards {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
By default a flex row never wraps. It squeezes items until they cannot shrink further, then overflows. Add flex-wrap: wrap and items move to a new line when they run out of room. It is the simplest way to build a responsive row of cards without a single media query.
flex-grow: share the leftover space
.search { display: flex; gap: 8px; }
.search input { flex-grow: 1; } /* take all the spare space */
.search button { flex-grow: 0; } /* stay as small as the content */
flex-grow is a number. It says how the free space is shared. If one item has flex-grow: 1 and the others have 0, that one gets all the free space. If two items have 1 and one has 2, the free space is divided 1:1:2.
Make the input stretch to fill the row while the button keeps its natural size. Use flex-grow: 1 on the input.
<style>
.search { display: flex; gap: 8px; font-family: Arial; }
.search input { padding: 10px; border: 2px solid #5A28C8; border-radius: 10px; /* grow */ }
.search button { padding: 10px 18px; background: #5A28C8; color: #fff; border: 0; border-radius: 10px; }
</style>
<div class="search"><input type="text" placeholder="Search skills"><button>Search</button></div>
Add flex-grow: 1 to .search input.
<style>
.search { display: flex; gap: 8px; font-family: Arial; }
.search input { flex-grow: 1; padding: 10px; border: 2px solid #5A28C8; border-radius: 10px; }
.search button { padding: 10px 18px; background: #5A28C8; color: #fff; border: 0; border-radius: 10px; }
</style>
<div class="search"><input type="text" placeholder="Search skills"><button>Search</button></div>
flex-shrink: who gives way first?
When there is not enough room, items shrink. flex-shrink: 1 is the default, meaning every item may shrink equally. Set it to 0 on something that must keep its size, such as a logo or an icon.
.logo { flex-shrink: 0; width: 120px; }
flex-basis: the starting size
flex-basis is the size an item wants before growing or shrinking. It is like width, but along the main axis whichever way that points.
.card { flex-basis: 260px; }
The flex shorthand
These three properties are almost always written together using flex.
.item { flex: 1; } /* grow 1, shrink 1, basis 0 : equal shares */
.item { flex: 0 0 200px; } /* do not grow or shrink: exactly 200px */
.item { flex: 1 1 260px; } /* start at 260px, then grow and shrink as needed */
.item { flex: none; } /* leave it alone */
flex: 1 on every child gives you equal-width columns. The recipe flex: 1 1 260px with flex-wrap: wrap is a favourite for responsive cards: each card wants 260 pixels, wraps when there is no room, and stretches to share leftover space on each line.
Build three equal columns by giving every .col the shorthand flex: 1. Then make the middle one twice as wide with flex: 2.
<style>
.row { display: flex; gap: 10px; font-family: Arial; }
.col { background: #EFEAFD; padding: 18px; border-radius: 10px; text-align: center; }
</style>
<div class="row"><div class="col">A</div><div class="col">B</div><div class="col">C</div></div>
.col { flex: 1; } and then give the second column flex: 2.
<style>
.row { display: flex; gap: 10px; font-family: Arial; }
.col { flex: 1; background: #EFEAFD; padding: 18px; border-radius: 10px; text-align: center; }
.col:nth-child(2) { flex: 2; }
</style>
<div class="row"><div class="col">A</div><div class="col">B</div><div class="col">C</div></div>
Alignment of a single item
.card { align-self: flex-end; }
.last-link { margin-left: auto; }
align-self overrides align-items for one item. And here is a beloved trick: an auto margin inside a flex container absorbs all free space on that side. margin-left: auto on the last link pushes it, and everything after it, to the far right.
Changing the order
The order property changes the visual order of items without touching the HTML. Items default to 0 and are sorted by number. Use it sparingly, because keyboard and screen reader users follow the HTML order, which then no longer matches what they see.
A card with a footer pinned to the bottom
A common problem: three cards in a row have different amounts of text, so their buttons end up at different heights. Flexbox fixes it. Make each card a column flex container, and push the button to the bottom with an auto margin.
.card { display: flex; flex-direction: column; }
.card .btn { margin-top: auto; }
Because cards in the same flex row stretch to equal height by default (align-items: stretch), and each card pushes its button down with margin-top: auto, all the buttons line up neatly.
A sticky footer
body { min-height: 100vh; display: flex; flex-direction: column; }
main { flex: 1; }
On a page with little content, the footer floats up the middle of the screen. Make the body a column flex container with a minimum height of the screen, and let the main area grow to fill the space. The footer is pushed to the bottom, and on long pages nothing changes.
Setting widths on flex items and wondering why they do not obey. Flex items shrink and grow, so a width is only a starting suggestion. Use flex: 0 0 200px when a size must be exact.
Fix the layout: make the cards wrap and each want 140px, using flex-wrap on the parent and flex: 1 1 140px on the cards.
<style>
.cards { display: flex; gap: 10px; font-family: Arial; }
.cards div { background: #EFEAFD; padding: 20px; border-radius: 10px; }
</style>
<div class="cards"><div>One</div><div>Two</div><div>Three</div><div>Four</div></div>
.cards { flex-wrap: wrap; } .cards div { flex: 1 1 140px; }
<style>
.cards { display: flex; flex-wrap: wrap; gap: 10px; font-family: Arial; }
.cards div { flex: 1 1 140px; background: #EFEAFD; padding: 20px; border-radius: 10px; }
</style>
<div class="cards"><div>One</div><div>Two</div><div>Three</div><div>Four</div></div>
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which property lets flex items move to a new line?
flex-wrap: wrapflex-flow: nonewrap-itemsgap
wrap allows additional lines.
What does flex: 1 on all children produce?
- Equal-width items
- Hidden items
- A grid
- Reversed order
Each child gets an equal share of the space.
What does flex-shrink: 0 do?
- Prevents the item from shrinking
- Hides the item
- Grows the item
- Wraps the item
The item keeps its size when space is tight.
How do you push the last item to the far right of a flex row?
margin-left: autofloat: rightdisplay: righttext-align: end
An auto margin absorbs the free space.
Why use order with care?
- It is not supported
- Keyboard and screen reader order follows the HTML, not the visual order
- It deletes items
- It slows pages
A mismatch confuses assistive technology users.
Summary
- flex-wrap lets items drop to new lines.
- flex: 1 1 260px with wrap makes responsive cards.
- Auto margins push items apart.
- Column flex with margin-top: auto pins buttons to the bottom.
Questions
What is the difference between width and flex-basis?
Width is the size in the inline direction. flex-basis is the starting size along the flex main axis, and takes priority in a flex item.
Why do my items not wrap?
flex-wrap defaults to nowrap. Add flex-wrap: wrap to the container.
Can flex items be different heights?
Yes. Use align-items: flex-start to stop them stretching to equal height.
