Sub topic 8
Starting size with flex-basis
This chapter assumes you know how to create a flex container with display: flex, that you can tell the main axis from the cross axis, and that you know the CSS box model and px, rem and % units. You will see flex-grow and flex-shrink here only as far as they help explain flex-basis. Later chapters cover them in full.
What flex-basis does
flex-basis sets an item's starting size along the main axis. The browser first gives each item that starting size. Then it checks whether the items fill the container. If there is free space, flex-grow can enlarge the items. If the items overflow, flex-shrink can reduce them.
The main axis depends on flex-direction:
- In a row,
flex-basisacts likewidth. - In a column, it acts like
height.
The CSS specification calls this starting value the flex base size.
How the starting size is chosen
The default value is auto. With auto, the browser looks at the item's width (or height in a column). If that property is set, the browser uses it. If it is also auto, the browser uses the size of the item's content.
Any value other than auto takes precedence over width or height. The width is then ignored when computing the starting size.
Accepted values
| Value | Meaning |
|---|---|
| `auto` | Use `width` or `height` if set, otherwise the content size. This is the default. |
| `content` | Use the content size and ignore `width` or `height`. Older browsers may not support it. |
| `200px`, `12rem` | A fixed length. |
| `25%` | A percentage of the container's inner main size. |
| `0` | A starting size of zero. The item's final size comes only from growing. |
A percentage needs a container whose main size is known. A row container usually has one, because its width comes from the layout. A column container with height: auto has no known height, so a percentage basis behaves like content. Give the column container a definite height if you want percentages to work.
A worked example: auto versus 0
Two items sit in a 600px row. The first has width: 100px and the second has width: 300px. Both have flex-grow: 1. The only difference between the two containers is the basis.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>flex-basis: auto versus 0</title>
<style>
* { box-sizing: border-box; }
.row {
display: flex;
width: 600px;
margin-bottom: 1rem;
border: 1px solid #888;
}
.item { padding: 0.5rem; background: #cde; border: 1px solid #36a; }
.a { width: 100px; }
.b { width: 300px; }
.from-auto .item { flex: 1 1 auto; }
.from-zero .item { flex: 1 1 0; }
</style>
</head>
<body>
<div class="row from-auto">
<div class="item a">A</div>
<div class="item b">B</div>
</div>
<div class="row from-zero">
<div class="item a">A</div>
<div class="item b">B</div>
</div>
</body>
</html>In the first row the basis is auto, so the starting sizes are 100px and 300px. That leaves 200px of free space. Each item has the same flex-grow, so each gets 100px extra. The final widths are 200px and 400px.
In the second row both bases are 0, so the width values are ignored. All 600px is free space, and each item gets half. The final widths are 300px and 300px.
Use auto when items should keep their natural proportions and share only the leftover. Use 0 when items should be equal regardless of content.
Interactions to remember
Basis wins over width
If you write width: 100px; flex-basis: 300px; on a row item, the item starts at 300px. This is a common source of confusion when old width rules sit next to new flex rules. Keep one of them, usually flex-basis inside the flex shorthand.
Min and max sizes still apply
min-width and max-width clamp the result, and so does flex-basis. An item with flex-basis: 50px and min-width: 120px ends up at least 120px wide. The browser also applies an automatic minimum based on content. The chapter on min-width: 0 covers that.
Box sizing changes what the number means
flex-basis measures the box that box-sizing selects. With the default content-box, a basis of 200px plus 20px of padding on each side gives a 240px item. With border-box, the same item is 200px wide in total. Setting box-sizing: border-box on all elements makes basis values easier to predict.
Gap reduces free space
gap is subtracted from the container's space before growing or shrinking. Three items with flex-basis: 33.333% and gap: 1rem do not fit exactly, because the percentages already fill the row. Shrinking absorbs the difference, so the items end up slightly narrower than 33.333%. If you want exact thirds, use flex: 1 1 0 and let gap take its share.
Basis in the flex shorthand
The shorthand is flex: <grow> <shrink> <basis>. You will often see these forms:
flex: 1expands to1 1 0%. Items start at zero and share the space equally.flex: autoexpands to1 1 auto. Items start from their size and share the leftover.flex: noneexpands to0 0 auto. Items are rigid at their natural size.flex: 0 0 200pxmakes a fixed 200px item that neither grows nor shrinks.
Watch out for a unitless 0 in the shorthand. flex: 1 0 is read as grow 1 and shrink 0, not as a zero basis. When you write the basis, give it a unit, such as 0px or 0%, or write all three values.
Using basis for card rows
The card pattern from the spine uses basis as a preferred width:
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
}Each card prefers 200px. When the row can fit more, wrapping lines fill with as many cards as fit, and growth shares the leftover on each line. On a narrow screen, one card per line stretches to the full width. Here flex-basis acts as a breakpoint, because it decides how many cards fit before a new line starts. Wrapping uses the starting size (clamped by min and max) to decide where lines break, not the final size.
Common errors
- Setting
widthon flex items and expecting it to win after you addflex-basis. The basis wins. - Using a percentage basis in a column container with automatic height. The percentage has nothing to resolve against, so the item falls back to its content size.
- Writing
flex: 1 0and expecting a zero basis. The0becomesflex-shrink. - Expecting equal columns from
flex-grow: 1alone. With a basis ofauto, a column with more content stays larger. Useflex: 1 1 0for equal columns.
Exercise
A row container is 800px wide with no gap and contains two items. Item A has width: 200px and item B has width: 400px. Both items have flex-grow: 1 and flex-shrink: 1. What are the final widths if the basis is auto for both? What are they if the basis is 0 for both?
Show answer
With basis auto, the starting sizes are 200px and 400px, so 200px is free. Each item gets 100px extra, giving 300px and 500px. With basis 0, the widths are ignored and all 800px is free space. Each item gets half, so both are 400px.