- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- Flex Item Sizing
CSS Flexbox
Flex Item Sizing
Control how individual flex items grow, shrink, reorder, align, and resolve their size against the available space. It explains flex-basis, positive and negative free space, growth, shrinkage, automatic minimum sizes, per-item alignment, and visual reordering.
In Flexbox, you do not set an item's width directly. Each item starts from a base size, then the container shares out any extra space, or takes some away if there is not enough. flex-grow, flex-shrink and flex-basis are the three dials.
Once you know what each dial does, Flexbox stops feeling random. The container side of Flexbox is in Flex Container, and lining items up across the row is in Align Items.
The flex shorthand
Example
css
.search-field { flex: 1 1 18rem; }
.search-button { flex: 0 0 auto; }
.truncate {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
}flex packs three values into one: flex: grow shrink basis.
- flex-basis is the starting size, before any sharing: here, 18rem for the search field.
- flex-grow is how much of the extra space the item takes. 0 means "none", 1 means "a share".
- flex-shrink is how willing the item is to get smaller when space runs out. 0 means "never".
So the search field starts at 18rem, grows to fill the row and can shrink, while the button stays exactly as big as its label.
A common surprise: flex: 1 is short for flex: 1 1 0%, with a basis of zero. That makes items equal width no matter what is inside them. flex: auto (1 1 auto) starts from each item's content size instead, so items with more content end up wider.
Why an item will not shrink
Flex items do not shrink below the width of their longest word or widest content. One long URL or file name can push a whole row wider than the screen. The fix is in the .truncate rule above: min-width: 0 allows the item to shrink, and then overflow and text-overflow decide what happens to the text.
Moving one item
Example
css
.card__action {
align-self: end;
margin-inline-start: auto;
}align-self changes the cross-axis alignment for one item only. margin-inline-start: auto eats up all the free space on its left, pushing the item to the far end of the row. Auto margins are the simplest way to push one item away from the others.
Visual order is not reading order
order and row-reverse change where items appear, but screen readers and the Tab key still follow the HTML order. If the order matters, change it in the HTML.
Flex Item Properties
| Property | Purpose | Example |
|---|---|---|
| flex-grow | Distributes positive free space. | flex-grow: 1 |
| flex-shrink | Controls shrinking when space is insufficient. | flex-shrink: 0 |
| flex-basis | Sets the initial main size. | flex-basis: 18rem |
| align-self | Overrides cross-axis alignment for one item. | align-self: start |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Flex Item Sizing example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
.search-field { flex: 1 1 18rem; }
.search-button { flex: 0 0 auto; }
.truncate {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
}
.card__action {
align-self: end;
margin-inline-start: auto;
}
</style>
</head>
<body>
<main class="toolbar"><input class="search-field" value="A flexible search field with a long value"><button class="search-button">Search</button><a class="card__action" href="#">Help</a></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| flex-grow | 29 | 12 | 20 | 9 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Quick fixes
- Space between items: use
gap, not margins on every item. - Text overflowing a flex item: add
min-width: 0to the item. - Controls getting squashed: let the row wrap with
flex-wrap: wraprather than shrinking buttons below a usable size.
Flex Item Sizing exercises
Grow
The row is a flex container. Let A take all the free space with flex-grow.
The HTML your code works on
<style>
.row { display: flex; height: 120px; border: 1px solid black; }
</style>
<div class="row">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
</div>Result
