Flexbox & GridExtra· 35 min read

Flex Items & Responsive Flex

Control how individual items grow, shrink and reorder — and make flex layouts responsive.

What you will learn

  • Use flex-grow, flex-shrink and flex-basis
  • Reorder items with order
  • Make a flex layout wrap responsively

The flex shorthand

On each item, flex sets how it grows and shrinks. flex: 1 means “share the space equally”. Different numbers give different proportions.

flex ratios share space proportionally
<style>
  .bar { display:flex; gap:8px; }
  .bar > div { background:#4338ca; color:#fff; padding:14px; border-radius:8px; text-align:center; }
  .one { flex: 1; } .two { flex: 2; }
</style>
<div class="bar">
  <div class="one">flex: 1</div>
  <div class="two">flex: 2 (twice as wide)</div>
  <div class="one">flex: 1</div>
</div>
Live preview

The flex number on each item is a share, not a fixed width. The middle item has flex: 2 and the outer two have flex: 1, so the row is split into 1 + 2 + 1 = four parts: the middle takes two of them, the others one each. That is why the middle box is exactly twice as wide. Change flex: 2 to flex: 3 and the middle would grow even bigger relative to its neighbours.

Reorder and align one item

order changes an item’s position without touching the HTML; align-self aligns a single item differently from the rest.

order moves an item; align-self shifts just one
<style>
  .row2 { display:flex; gap:8px; align-items:flex-start; height:90px; }
  .row2 > div { background:#4338ca; color:#fff; padding:10px; border-radius:8px; }
  .first { order: -1; }            /* jump to the front */
  .down  { align-self: flex-end; } /* drop to the bottom */
</style>
<div class="row2">
  <div>A</div>
  <div class="down">B (pushed down)</div>
  <div class="first">C (moved first)</div>
</div>
Live preview

In the HTML the order is A, B, C — but on screen C comes first because order: -1 pulls it ahead (lower numbers come earlier; the default is 0). Item B drops to the bottom because align-self: flex-end overrides the container’s alignment for that one item only, while A and C stay at the top. So order rearranges left-to-right and align-self adjusts a single item up or down.

Tip: For responsive layouts, combine flex-wrap: wrap with flex: 1 and a min-width on items. Cards then sit in a row on desktop and stack on mobile — automatically.

Q. An item with flex: 2 next to two items with flex: 1 will be:

Answer: flex values are ratios. flex: 2 takes twice the share of free space compared to flex: 1.

✍️ Practice

  1. Make a three-item row where the middle item is twice as wide using flex.
  2. Use order to move the last item to the front without changing the HTML.

🏠 Homework

  1. Make your card row responsive: side by side on desktop, stacked on mobile, using wrap + min-width.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →