TExcelTAD
Home › CSS › CSS Flex Items

CSS Flex Items

The container decides the overall flow; the items decide how big they are and how they react when there's too much or too little space. These properties go on the children of a flex container.

Item properties at a glance

Property Default Purpose
flex-grow 0 How much of the leftover space the item takes
flex-shrink 1 How much the item gives up when space runs out
flex-basis auto Starting size before growing/shrinking
flex 0 1 auto Shorthand for grow, shrink, basis
order 0 Visual order (lower first)
align-self auto Override the container's align-items for one item

flex-grow

After the items are laid out at their base size, any free space is shared out in proportion to flex-grow. An item with 2 gets twice as much extra space as an item with 1.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .c { display: flex; gap: 6px; margin-bottom: 10px; background: #eceff1; padding: 6px; }
  .c div { background: #3949ab; color: white; padding: 12px 6px; border-radius: 4px; text-align: center; }
  .g1 { flex-grow: 1; }
  .g2 { flex-grow: 2; background: #e53935 !important; }
</style>
</head>
<body>
  <div class="c"><div>0</div><div>0</div><div>0</div></div>
  <div class="c"><div class="g1">grow 1</div><div class="g1">grow 1</div><div>0</div></div>
  <div class="c"><div class="g1">grow 1</div><div class="g2">grow 2</div><div class="g1">grow 1</div></div>
</body>
</html>

flex-shrink and flex-basis

flex-basis is the size an item wants along the main axis (like width in a row). When the items' bases add up to more than the container, they shrink according to flex-shrink. Setting flex-shrink: 0 protects an item from shrinking.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .c { display: flex; width: 320px; border: 3px solid #00897b; padding: 4px; gap: 4px; }
  .c div { flex-basis: 150px; background: #4db6ac; padding: 10px 4px; text-align: center; color: white; border-radius: 4px; }
  .fixed { flex-shrink: 0; background: #ff7043 !important; }
</style>
</head>
<body>
  <p>Three items of basis 150px in a 320px box:</p>
  <div class="c"><div>shrinks</div><div class="fixed">shrink: 0</div><div>shrinks</div></div>
</body>
</html>

The orange item keeps its full 150px; the other two get squeezed.

The flex shorthand

Always prefer the flex shorthand. Its common forms:

Shorthand Means Behaviour
flex: 1 1 1 0% Equal share of all space, ignoring content size
flex: auto 1 1 auto Grow/shrink, starting from content size
flex: none 0 0 auto Rigid — never grows or shrinks
flex: 0 0 200px fixed 200px Exactly 200px
flex: 2 2 1 0% Twice the share of a flex: 1 sibling
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  .app { display: flex; height: 100vh; }
  .side { flex: 0 0 110px; background: #37474f; color: #eceff1; padding: 12px; }
  .main { flex: 1; background: #fafafa; padding: 12px; }
  .panel { flex: 0 0 80px; background: #ffe082; padding: 12px; }
</style>
</head>
<body>
  <div class="app">
    <aside class="side">Sidebar<br>flex: 0 0 110px</aside>
    <main class="main">Main — flex: 1<br>Resize the frame: only I change width.</main>
    <aside class="panel">80px</aside>
  </div>
</body>
</html>

order and align-self

order changes the visual position without touching the HTML (lower values come first; the default is 0). align-self lets one item break out of the container's cross-axis alignment.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .c { display: flex; align-items: flex-start; height: 160px; gap: 8px; background: #f3e5f5; padding: 8px; }
  .c div { width: 70px; padding: 10px 0; text-align: center; color: white; background: #8e24aa; border-radius: 6px; }
  .first { order: -1; background: #43a047 !important; }
  .bottom { align-self: flex-end; background: #fb8c00 !important; }
  .stretch { align-self: stretch; background: #1e88e5 !important; }
</style>
</head>
<body>
  <div class="c">
    <div>A</div>
    <div class="bottom">B end</div>
    <div class="stretch">C stretch</div>
    <div class="first">D order -1</div>
  </div>
</body>
</html>

Tip: margin-left: auto on a flex item pushes it (and everything after it) to the far end — the classic way to put a "Log in" button at the right of a nav bar.

Test yourself

What does flex: 1 expand to?

  1. 1 0 auto
  2. 1 1 0%
  3. 0 1 auto
  4. 1 1 auto

How do you stop a flex item from getting narrower than its basis?

  1. flex-grow: 0
  2. flex-shrink: 0
  3. order: 0
  4. align-self: none

Which property moves one item to the front visually without changing HTML?

  1. z-index
  2. order
  3. flex-basis
  4. align-self