Basis, grow and shrink
Definition and purpose
Flex sizing starts from each item’s flex basis and resolves free space within its line. Grow factors distribute positive space; shrink factors, scaled by base sizes, distribute a deficit, subject to minimum and maximum constraints.
Grow from equal bases
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #p { width: 300px; } #p > div { background: lightblue; outline: 1px solid navy; } #p { display: flex; } #a { flex: 1 1 50px; } #b { flex: 2 1 50px; } #c { flex: 0 1 50px; } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> <div id="c">C</div> </div> </body></html>The final widths are 100, 150 and 50px.
Syntax and mental model
flex: grow shrink basis makes the three components explicit. For a simple 300px line with three 50px bases and grow factors 1, 2, 0, the extra 150px is shared as 50px, 100px, 0px. Minimum/maximum clamping can change this simple calculation.
Scaled shrink
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #p { width: 300px; } #p > div { background: lightblue; outline: 1px solid navy; } #p { display: flex; } #a { flex: 0 1 200px; } #b { flex: 0 1 100px; } #c { flex: 0 0 60px; } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> <div id="c">C</div> </div> </body></html>The 60px deficit is split 40:20 between A and B; C does not shrink.
Common mistakes
Grow ratios apply to extra space, not necessarily final widths. Shrink uses the base size as well as the factor; unequal bases can lose unequal amounts with equal shrink factors.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Alignment and justification. Next lesson: Automatic minimum sizes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Basis, grow and shrink — knowledge check
Five questions on basis, grow and shrink.
What is the third value in flex: 2 1 80px?
Which factor distributes positive free space?
Which two inputs scale ordinary shrink distribution?
In a 300px row, bases 100px and 100px grow 1 and 1. Final widths?
Can min/max constraints alter the simple ratio calculation?