Emmanuel Akinfulubi

HTML and CSSPractical II

flex: 1 explained (grow, shrink, basis) in CSS

18 What flex: 1 actually means

hard

Use the three flex properties separately, then write out what the common shorthands expand to. The difference between a basis of 0 and auto is the whole exercise.

Stuck? Three hints, smallest first
  1. 1. flex is flex-grow flex-shrink flex-basis, and flex: 1 means 1 1 0%.
  2. 2. flex: auto is 1 1 auto: it grows, but starts from its content width, so items with more text end up wider.
  3. 3. flex: 1 starts every item from zero, which is why the columns come out equal.

index.html

<div class="bar">
  <div class="one">flex: 1</div>
  <div class="one">flex: 1, but much more text inside it</div>
</div>

<div class="bar">
  <div class="auto">flex: auto</div>
  <div class="auto">flex: auto, but much more text inside it</div>
</div>

<div class="bar">
  <div class="fixed">grow 0, basis 90px</div>
  <div class="grows">grow 2, basis 0</div>
</div>

style.css

.bar {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.bar > * {
  padding: 8px 10px;
  background: #eef1fd;
  border: 1px solid #3055d8;
  border-radius: 6px;
  font-size: 0.8rem;
}

/* Basis 0: content width is ignored, so both items come out equal. */
.one { flex: 1 1 0%; }

/* Basis auto: each starts from its content, then shares the surplus, so the
   wordier one stays wider. */
.auto { flex: 1 1 auto; }

.fixed { flex: 0 0 90px; }     /* never grows, never shrinks */
.grows { flex: 2 1 0%; }       /* takes twice the share of a grow: 1 sibling */
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 18

// Expand the shorthand the way the specification does.
const expand = (value) => {
  const v = value.trim();
  if (v === 'none') return ['0', '0', 'auto'];
  if (v === 'auto') return ['1', '1', 'auto'];
  if (v === 'initial') return ['0', '1', 'auto'];
  if (/^\d+$/.test(v)) return [v, '1', '0%'];
  return v.split(/\s+/);
};

test('flex: 1 expands to 1 1 0%', expand('1'), ['1', '1', '0%']);
test('flex: auto expands to 1 1 auto', expand('auto'), ['1', '1', 'auto']);
test('flex: none expands to 0 0 auto', expand('none'), ['0', '0', 'auto']);
test('the default, flex: initial, is 0 1 auto', expand('initial'), ['0', '1', 'auto']);
test('so 1 and auto differ only in the basis', expand('1')[2] === expand('auto')[2], false);
test('the stylesheet writes all three parts out in full', (css.match(/flex:\s*\d[^;]*;/g) || []).length, 4);
test('one item is fixed at 90px and cannot grow', /flex:\s*0 0 90px/.test(css), true);
test('another takes double the share', /flex:\s*2 1 0%/.test(css), true);

Output


      

flex: 1 and flex: auto look interchangeable and are not: both grow, both shrink, but the first starts every item from a basis of zero and the second starts from its content width. So flex: 1 gives you equal columns and flex: auto gives you columns proportional to how much text is in them. That single difference explains most “why is one column wider” reports. The other thing worth remembering is that flex-basis beats width when both are set, so a width on a flex item with a basis is simply ignored.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.