Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

flex — growth, shrinking, and basis

The flex shorthand sets a flex item's flex-grow, flex-shrink, and flex-basis. It controls sizing along the main axis rather than creating a flex container.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ];

flex sets flex-grow, flex-shrink, and flex-basis together. It applies to flex items; it does not replace display: flex on the parent. It is not inherited. Its initial value is 0 1 auto.

flex-grow controls the share of extra space, flex-shrink controls shrinking, and flex-basis supplies the base size before space distribution. Factors are nonnegative; shrinking also accounts for the base size. Growth ratios do not always produce matching final width ratios.

Common forms: initial means 0 1 auto, auto means 1 1 auto, and none means 0 0 auto. The shorthand resets omitted components. Three explicit values, such as 1 1 0px, remove ambiguity about an omitted basis.

A content-based minimum size can constrain shrinking; min-width: 0 often helps in a row layout. In the example, zero bases and factors of 1 and 2 divide 300px into 100px and 200px.

Examples

<!doctype html>
<meta charset="utf-8">
<style>
  .row { display: flex; width: 300px; }
  .row > div { min-width: 0; flex: 1 1 0px; }
  .row > .wide { flex: 2 1 0px; }
</style>
<div class="row"><div>A</div><div class="wide">B</div></div>
<pre id="result"></pre>
<script>
  const widths = [...document.querySelector('.row').children]
    .map(item => item.getBoundingClientRect().width);
  document.getElementById('result').textContent = widths.join(', ');
</script>

Result

100, 200

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources