Flex Basis and Flex Shorthand – Understanding Item Sizing
Quick Answer: flex-basis sets the starting size of a flex item along the main axis, before free space is shared or removed. The flex shorthand sets flex-grow, flex-shrink and flex-basis in one line, for example flex: 1 1 220px. A single number such as flex: 1 means grow 1, shrink 1 and a basis of 0%.
S1. Why Do We Need Flex Basis and the Flex Shorthand?
Imagine a page with three equal columns. Another page has a fixed 250px sidebar and a content area that fills the rest. A third page has cards that should be about 220px wide but can grow and shrink.
Would you control each case with a mix of width, flex-grow and flex-shrink written on separate lines? That works, but it is easy to get a result you did not expect.
Flexbox gives us one starting size and one shorthand to describe item sizing. Let's first see what happens when we rely on grow alone.
S2. The Problem: Growing Items from a Content-Based Start
Flex base size = the starting size of a flex item before free space is shared or removed.
Imagine three columns in a 600px container. The developer wants equal columns, so every column gets flex-grow: 1. The columns hold different amounts of text, and their content is about 100px, 300px and 100px wide.
Example 1: Equal columns using only flex-grow
This example renders, but the columns are not equal.
<!-- HTML -->
<div class="columns">
<div class="col">Short text</div>
<div class="col">A much longer piece of text in this column</div>
<div class="col">Short text</div>
</div>
/* CSS */
.columns { display: flex; width: 600px; }
.col { flex-grow: 1; }
Expected output
Starting sizes (flex-basis: auto, so the content decides): column 1: 100px column 2: 300px column 3: 100px = 500px Free space: 600px - 500px = 100px, shared equally (about 33px each) Result column 1: about 133px column 2: about 333px column 3: about 133px The columns are not equal.
The code runs, but flex-grow only shares the free space. Each column still starts from its own content size, so the long column stays much wider.
What goes wrong?
- Unequal columns: The starting size comes from the content.
- Surprising results: Changing the text changes the column widths.
- Risky changes: Editing one column affects the whole row.
- Harder testing: Different content gives a different layout every time.
- Poor clarity: The CSS does not say what the starting size should be.
Now think about many rows of cards and columns across a whole website. Leaving the starting size to the content becomes hard to predict.
Remember: The problem is not flex-grow. The real problem is that we never told the browser what size the items should start from.
Animation 1: How a content-based start creates unequal items
Same grow value, different starting sizes
The red highlights show that the content decides where each column starts.
The animation highlights the shared grow value and the different starting sizes.
S3. The Solution: Set the Starting Size with Flex Basis
Flex basis = the property that sets the initial main size of a flex item.
Instead of letting the content decide, give every item the same starting size. The easiest way is the flex shorthand.
flex-basissets the starting size, such as220pxor0%.flex: 1means grow 1, shrink 1 and a basis of 0%.- With a basis of 0%, all the container space is free space, so equal grow values give equal columns.
Now the same three columns are equal at any content length.
Example 2: Equal columns using flex: 1
Assumption: This example uses standard CSS supported by all modern browsers.
.columns { display: flex; width: 600px; }
.col { flex: 1; } /* same as: flex: 1 1 0% */
Expected output
Starting size of every column: 0% Free space: the full 600px, shared equally Result column 1: 200px column 2: 200px column 3: 200px The columns are equal, whatever their content length is.
Notice the difference. The HTML is unchanged, and one shorthand gives every column the same start.
Animation 2: The starting size design
One shorthand, three sizing roles
Each layout part gets the sizing rule that fits it.
The animation highlights each layout part and the shorthand that fits it.
Compare both approaches
| Without an explicit basis | With flex-basis or flex |
|---|---|
| The content decides the starting size. | You decide the starting size. |
| Columns can end up unequal. | Columns can be made equal. |
| Changing text changes the layout. | The layout is more predictable. |
| Grow and shrink are written on separate lines. | One line describes the whole sizing rule. |
| Layout intent is hidden. | Layout intent is clear. |
The idea to remember
- flex-basis = the starting size of an item along the main axis.
- flex = a shorthand for
flex-grow,flex-shrinkandflex-basis, in that order. - flex: 1 =
1 1 0%, so items share the space equally. - flex: auto =
1 1 auto, so items start from their content size. - flex: none =
0 0 auto, so items are fully inflexible.
S4. Step-by-Step Code Breakdown
1. Understand the flex base size
Before the browser grows or shrinks an item, it needs a starting size. That is the flex base size. The default value of flex-basis is auto, which means "look at the item's width (or height in a column). If that is also auto, use the content size."
| Value | Behaviour |
|---|---|
auto |
Uses the item's width (or height). If that is auto, uses the content size. This is the default. |
220px |
Starts at a fixed length. |
30% |
Starts at a percentage of the container's main size. |
0 or 0% |
Starts at zero, so all the space is shared as free space. |
content |
Starts at the size of the item's content. |
2. Use flex-basis instead of width
When flex-basis is not auto, it takes priority over width for the starting size in a row. The item can still grow or shrink afterwards, and min-width and max-width still apply.
Example 3: flex-basis wins over width
.layout { display: flex; width: 900px; }
.sidebar {
width: 100px;
flex-basis: 250px; /* this is the starting size */
flex-grow: 0;
flex-shrink: 0;
}
.content { flex: 1; }
Expected output
sidebar: width 100px, flex-basis 250px -> 250px (basis wins) content: takes the remaining 650px [ sidebar 250px ][ content 650px .................... ]
3. Write the shorthand
The shorthand order is grow shrink basis. You can write it with one, two or three values, or with a keyword.
| Shorthand | Same as | Typical use |
|---|---|---|
flex: initial |
0 1 auto |
The default behaviour. Items can shrink but not grow. |
flex: auto |
1 1 auto |
Items grow and shrink from their content size. |
flex: none |
0 0 auto |
Items that must keep their size, such as icons. |
flex: 1 |
1 1 0% |
Equal columns, or one item filling the rest. |
flex: 2 1 |
2 1 0% |
An item that takes twice the share of free space. |
flex: 1 220px |
1 1 220px |
Cards that start at 220px and can grow. |
flex: 0 0 250px |
Written out in full | A fixed-width sidebar. |
Example 4: Common patterns
/* A fixed sidebar */
.sidebar { flex: 0 0 250px; }
/* A content area that fills the rest */
.content { flex: 1; }
/* Responsive cards with wrapping */
.cards { display: flex; flex-wrap: wrap; gap: 20px; }
.card { flex: 1 1 220px; }
/* A search row: input fills, button keeps its size */
.search input { flex: 1; }
.search button { flex: none; }
Expected output
Layout: [ sidebar 250px ][ content ........................ ] Cards: [ card ][ card ][ card ] (wrap when there is no room) Search: [ input ................................ ][ Search ] Each rule states the starting size and how the item may change.
4. Compare flex: 1 and flex: auto
Both let the items grow, but they start from different sizes. Use the content sizes from Example 1 (100px, 300px and 100px in a 600px container).
flex: 1 (1 1 0%) -> 200px 200px 200px equal, content ignored flex: auto (1 1 auto) -> 133px 333px 133px content sets the start flex: auto is what Example 1 behaved like. flex: 1 is what gave us equal columns in Example 2.
5. Remember that the shorthand resets omitted values
When you write flex: 1, the shrink and basis values are set as well. If you wrote flex-shrink: 0 earlier in the same rule, a later flex: 1 replaces it with a shrink of 1. Keep the sizing in a single flex declaration to avoid this.
Good to know: A flex item still will not shrink below its minimum content size unless you set min-width: 0. You saw this in the Flex Shrink article.
S5. Architecture and Real-World Trade-Offs
One sizing line per item keeps components predictable. It is useful in app layouts, card grids, search rows and form rows.
How the design fits a website
Imagine a website with an app layout, a product card list and a search form. Each area describes its sizing with one shorthand.
A focused sizing flow
Each website area uses the shorthand that fits its job.
The animation traces each website area to the sizing shorthand it uses.
Benefits for larger websites
- Maintainability: One line describes the whole sizing rule.
- Testing: Items behave consistently at different widths.
- Flexibility: The same shorthand works for rows and columns.
- Clear ownership: Each item states its starting size and how it may change.
- Fewer surprises: Equal columns no longer depend on the content.
Important trade-offs
flex: 1ignores the content size as a starting point, so very different content may look cramped or loose.- Percentage values refer to the container's main size and do not include the
gap, so three items of 33.33% plus gaps may need to shrink. - The shorthand sets three values at once, so it can reset a longhand written earlier in the same rule.
- An item still has a minimum size based on its content, so it may not reach the size the basis suggests.
Important: The basis is only the starting point. The final size depends on the free space, the grow and shrink values, and the minimum and maximum sizes.
S6. Common Mistakes and Best Practices
Mistake 1: Assuming width and flex-basis do the same thing
Wrong: Set both width: 100px and flex-basis: 250px and expect 100px.
Why it fails: A flex-basis that is not auto takes priority over width for the starting size in a row.
Better: Use one of them for the starting size, preferably flex-basis inside a flex container.
Rule: Do not set two competing starting sizes.
Mistake 2: Using flex-grow alone for equal columns
Wrong: Write flex-grow: 1 and expect equal widths.
Why it fails: The basis stays auto, so each item starts from its content size.
Better: Use flex: 1 so that every item starts from 0%.
Rule: Equal columns need an equal starting size.
Mistake 3: Expecting flex: 1 to respect the content size
Wrong: Use flex: 1 on items with very different content and expect widths based on that content.
Why it fails: The basis is 0%, so the content is ignored as a starting point.
Better: Use flex: auto when the content should influence the size.
Rule: Choose 1 for equal sizes and auto for content-based sizes.
Mistake 4: Forgetting that the shorthand resets values
Wrong: Write flex-shrink: 0; and then flex: 1; in the same rule.
Why it fails: The later shorthand sets shrink back to 1.
Better: Put the full sizing in one declaration, such as flex: 1 0 200px.
Rule: Keep the sizing in one flex declaration.
Mistake 5: Using percentage bases without allowing for the gap
Wrong: Use flex: 0 0 33.33% for three items together with a gap.
Why it fails: The three items already fill the line, so the gaps push the total over 100% and the row overflows.
Better: Use flex: 1 so that the items share the space after the gaps, or allow shrinking.
Rule: Remember that gaps take space too.
S7. How This Comes Up in Interviews
Question 1: What is flex-basis?
What the interviewer wants: They want to know whether you understand how flex items are sized.
Strong answer:
"flex-basis sets the initial main size of a flex item before free space is shared or removed. The default is auto, which uses the item's width or height, or its content size if that is auto."
Question 2: What is the difference between flex-basis and width?
Strong answer:
"In a row, a flex-basis that is not auto takes priority over width for the starting size. The width is used only when the basis is auto. Either way, the item can still grow or shrink afterwards."
Question 3: What does flex: 1 mean?
Strong answer:
"It is the same as flex: 1 1 0%. The item can grow and shrink, and its starting size is zero, so all the container space is shared according to the grow values. Equal values give equal sizes."
Question 4: What is the difference between flex: 1, flex: auto and flex: none?
Strong answer:
"flex: 1 is 1 1 0%, which gives equal sizes. flex: auto is 1 1 auto, which grows and shrinks from the content size. flex: none is 0 0 auto, which means the item is fully inflexible."
What should different experience levels explain?
- Junior developer: Explain
flex: 1and the idea of a starting size. - Senior developer: Explain base size, the shorthand forms, and how basis interacts with width and gap.
- Technical architect: Explain which shorthand patterns belong in a design system.
S8. Hands-On Practice
Beginner: Make equal columns
Create a container with three columns that hold different amounts of text. Try flex-grow: 1 first, then flex: 1.
Expected result: With flex-grow: 1 the columns are unequal. With flex: 1 they are equal.
Intermediate: Build a sidebar layout
Create a layout with a fixed 250px sidebar and a content area that fills the rest. Use flex: 0 0 250px and flex: 1.
Hint: Resize the browser and check that the sidebar does not change.
Advanced: Compare 1, auto and none
Create three rows with the same items. Use flex: 1 on the first, flex: auto on the second and flex: none on the third. Add different text lengths to the items and compare the widths.
Expected result: The first row has equal items. The second row has items sized from their content. In the third row, the items keep their content size and do not grow.
S9. Next Steps and Community
Previous article: Flex Shrink – Controlling How Items Compress.
Next article: Gap, Auto Margins and Spacing in CSS Flexbox.
Continue the series by learning how gap and auto margins create spacing and push items apart inside a flex container.
Try the examples in your own HTML and CSS project. Change the basis values, compare flex: 1 with flex: auto, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.
Choose the starting size first. Then let the items grow and shrink from there.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.