On this page12 sections
  1. Learning Objectives
  2. 1. flex-grow: Sharing the Extra Space
  3. 2. flex-shrink: Giving Space Back
  4. 3. flex-basis: The Starting Size
  5. 4. The flex Shorthand
  6. 5. order
  7. 6. align-self
  8. 7. Responsive Flex: Cards That Wrap
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 050: Flex Items

Category8: Flexbox & Grid
Lecture050 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelIntermediate

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-shrink and flex-basis
  • Use the flex shorthand: flex: 1, flex: auto, flex: none
  • Reorder items with order
  • Align one item with align-self
  • Build responsive layouts with flex-wrap and flex-basis

1. flex-grow: Sharing the Extra Space

Example

html
<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

html
<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

html
<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 writeMeansUse it for
flex: 1grow 1, shrink 1, basis 0Equal columns, whatever their content
flex: autogrow 1, shrink 1, basis autoGrow, starting from the content size
flex: nonegrow 0, shrink 0, basis autoA fixed item that never changes size
flex: 2 1 200pxgrow 2, shrink 1, basis 200pxFull control

Example: a sidebar that stays fixed and content that fills the rest:

html
<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

html
<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

html
<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:

html
<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

  1. Make one item take twice as much extra space as the others.
  2. Stop an item from ever shrinking.
  3. What does flex: 1 expand to?
  4. Make the third item appear first visually.
  5. Centre just one item vertically in a row.
  6. Make cards at least 200px wide that wrap onto new rows.

Practice Answers

  1. Give it flex-grow: 2 (the others flex-grow: 1).
  2. flex-shrink: 0;
  3. flex-grow: 1; flex-shrink: 1; flex-basis: 0%;
  4. Give it order: -1 (or give the others higher numbers).
  5. align-self: center; on that item.
  6. Container display: flex; flex-wrap: wrap; and cards flex: 1 1 200px;

Key Takeaways

  • flex-grow shares extra space; flex-shrink gives space back; flex-basis is the start size.
  • flex: 1 = equal shares; flex: none = fixed size.
  • order changes visual order; align-self aligns one item.
  • flex-wrap + flex-basis give responsive rows without media queries.