On this page12 sections
Lecture 050: Flex Items
| Category | 8: Flexbox & Grid |
| Lecture | 050 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Lecture 049 styled the flex container. Each item inside it has its own properties: how much it grows, how much it shrinks, its starting size, its order, and its own alignment. They're what make flexible layouts actually flex.
Learning Objectives
- Grow, shrink and size items with
flex-grow,flex-shrinkandflex-basis - Use the
flexshorthand:flex: 1,flex: auto,flex: none - Reorder items with
order - Align one item with
align-self - Build responsive layouts with
flex-wrapandflex-basis
1. flex-grow: Sharing the Extra Space
Example
<style>
.row { display: flex; gap: 6px; margin-bottom: 8px; }
.row div { padding: 10px; background: #4c6ef5; color: white; font: 13px monospace; }
</style>
<div class="row"><div>0</div><div>0</div><div>0</div></div>
<div class="row"><div style="flex-grow: 1;">1</div><div>0</div><div>0</div></div>
<div class="row"><div style="flex-grow: 1;">1</div><div style="flex-grow: 2;">2</div><div style="flex-grow: 1;">1</div></div>The numbers are shares of the leftover space: in the last row the middle item gets twice as much extra as each of the others.
2. flex-shrink: Giving Space Back
When the items are too wide, they shrink. flex-shrink: 0 says "never shrink me".
Example
<style>
.row { display: flex; width: 320px; gap: 6px; border: 1px dashed #adb5bd; }
.row div { flex-basis: 150px; padding: 10px; background: #12b886; color: white; font: 13px monospace; }
</style>
<div class="row">
<div>shrinks</div>
<div style="flex-shrink: 0;">flex-shrink: 0</div>
<div>shrinks</div>
</div>3. flex-basis: The Starting Size
flex-basis is the item's size before growing or shrinking: a length, a percentage, or auto (use its width or content).
Example
<style>
.row { display: flex; gap: 6px; }
.row div { padding: 10px; background: #f76707; color: white; font: 13px monospace; }
</style>
<div class="row">
<div style="flex-basis: 100px;">100px</div>
<div style="flex-basis: 50%;">50%</div>
<div style="flex-basis: auto;">auto</div>
</div>4. The flex Shorthand
| You write | Means | Use it for |
|---|---|---|
flex: 1 | grow 1, shrink 1, basis 0 | Equal columns, whatever their content |
flex: auto | grow 1, shrink 1, basis auto | Grow, starting from the content size |
flex: none | grow 0, shrink 0, basis auto | A fixed item that never changes size |
flex: 2 1 200px | grow 2, shrink 1, basis 200px | Full control |
Example: a sidebar that stays fixed and content that fills the rest:
<style>
.app { display: flex; gap: 10px; height: 120px; }
.side { flex: none; width: 150px; background: #fff3bf; padding: 10px; }
.main { flex: 1; background: #d0ebff; padding: 10px; }
</style>
<div class="app"><aside class="side">Fixed 150px</aside><main class="main">flex: 1 fills the rest</main></div>5. order
Changes the visual order without touching the HTML. The default is 0; lower numbers come first.
Example
<style>
.row { display: flex; gap: 6px; }
.row div { padding: 10px 16px; background: #7048e8; color: white; }
</style>
<div class="row">
<div style="order: 3;">First in HTML</div>
<div style="order: 1;">Second in HTML</div>
<div style="order: 2;">Third in HTML</div>
</div>Screen readers and the Tab key still follow the HTML order, so use order for small visual changes only.
6. align-self
Overrides the container's align-items for one item.
Example
<style>
.row { display: flex; gap: 6px; height: 120px; align-items: flex-start; background: #f1f3f5; }
.row div { padding: 10px; background: #e64980; color: white; font: 13px monospace; }
</style>
<div class="row">
<div>start</div>
<div style="align-self: center;">center</div>
<div style="align-self: flex-end;">end</div>
<div style="align-self: stretch;">stretch</div>
</div>7. Responsive Flex: Cards That Wrap
Example: drag the preview corner:
<style>
.cards { display: flex; flex-wrap: wrap; gap: 12px; }
.card { flex: 1 1 180px; padding: 14px; background: white; border: 1px solid #dee2e6; border-radius: 10px; }
</style>
<div class="cards">
<div class="card"><strong>HTML</strong><p>Structure</p></div>
<div class="card"><strong>CSS</strong><p>Style</p></div>
<div class="card"><strong>JavaScript</strong><p>Behaviour</p></div>
</div>flex: 1 1 180px: start at 180px, grow to fill the row, wrap onto a new line when there isn't room.
Common Beginner Mistakes
1. Setting item properties on the container. flex-grow and order go on the items.
2. flex: 1 columns with content of different lengths. flex: 1 makes them equal; flex: auto lets longer content take more room.
3. Images squashing in a flex row. Give them flex-shrink: 0.
4. Using order to fix the HTML. Keyboard and screen-reader order doesn't change.
Practice Questions
- Make one item take twice as much extra space as the others.
- Stop an item from ever shrinking.
- What does
flex: 1expand to? - Make the third item appear first visually.
- Centre just one item vertically in a row.
- Make cards at least 200px wide that wrap onto new rows.
Practice Answers
- Give it
flex-grow: 2(the othersflex-grow: 1). flex-shrink: 0;flex-grow: 1; flex-shrink: 1; flex-basis: 0%;- Give it
order: -1(or give the others higher numbers). align-self: center;on that item.- Container
display: flex; flex-wrap: wrap;and cardsflex: 1 1 200px;
Key Takeaways
flex-growshares extra space;flex-shrinkgives space back;flex-basisis the start size.flex: 1= equal shares;flex: none= fixed size.orderchanges visual order;align-selfaligns one item.flex-wrap+flex-basisgive responsive rows without media queries.