🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Flex-basis

AI & DATA SCIENCE // flex-basis

The flex-basis property sets a flex item's initial main-axis size, before any flex-grow or flex-shrink adjustments are applied to distribute leftover or overflow space.

Syntax

flex-basis: auto | length | percentage | content;

Deep Dive Course

auto, the default, uses the item's own explicit width, in a row container, or height, in a column container, if one is set, or falls back to the item's natural content-based size if not — a specific length or percentage value instead overrides both of those, establishing a fixed initial size that flex-grow and flex-shrink then further adjust based on the container's actual available space. flex-basis, flex-grow, and flex-shrink are commonly combined into the flex shorthand property, and flex: 1 specifically is a very common shorthand equivalent to flex-grow: 1, flex-shrink: 1, flex-basis: 0%, telling an item to grow and shrink freely starting from essentially no initial base size.

1Understanding Flex-basis

auto, the default, uses the item's own explicit width, in a row container, or height, in a column container, if one is set, or falls back to the item's natural content-based size if not — a specific length or percentage value instead overrides both of those, establishing a fixed initial size that flex-grow and flex-shrink then further adjust based on the container's actual available space. flex-basis, flex-grow, and flex-shrink are commonly combined into the flex shorthand property, and flex: 1 specifically is a very common shorthand equivalent to flex-grow: 1, flex-shrink: 1, flex-basis: 0%, telling an item to grow and shrink freely starting from essentially no initial base size.

💡

flex-basis, not width, is technically the property that establishes a flex item's initial main-axis size before flex-grow/flex-shrink adjustments — width still generally works as a fallback when flex-basis is left at its default auto value, but understanding this distinction matters once you're troubleshooting more complex flex sizing behavior.

editor.html
.item {
  flex: 1;
}

/* equivalent to: flex-grow: 1; flex-shrink: 1; flex-basis: 0%; */
localhost:3000

2Practical Example

Here is a real-world application of Flex-basis showing how it is used in production CSS code.

editor.html
.sidebar {
  flex: 0 0 250px;
}

/* flex-grow: 0; flex-shrink: 0; flex-basis: 250px; */
localhost:3000

3Best Practices

Follow these guidelines when working with Flex-basis:

1. Use flex-basis, often via the flex shorthand, to set a flex item's intended starting size, rather than relying solely on width/height, once you need precise control interacting with flex-grow/flex-shrink

2. Use the flex: 1 shorthand, equivalent to flex-grow: 1, flex-shrink: 1, flex-basis: 0%, for items that should grow and shrink freely from essentially no fixed starting size

3. Set an explicit flex-basis value rather than leaving it at auto whenever you need flex-grow/flex-shrink calculations to start from a specific, known size rather than each item's natural content size

⚠️

Tip: flex-basis, not width, is technically the property that establishes a flex item's initial main-axis size before flex-grow/flex-shrink adjustments — width still generally works as a fallback when flex-basis is left at its default auto value, but understanding this distinction matters once you're troubleshooting more complex flex sizing behavior.

editor.html
.item {
  flex: 1;
}

/* equivalent to: flex-grow: 1; flex-shrink: 1; flex-basis: 0%; */
localhost:3000

Examples

Example 01Basic Usage
.item {
  flex: 1;
}

/* equivalent to: flex-grow: 1; flex-shrink: 1; flex-basis: 0%; */
Example 02Advanced Example
.sidebar {
  flex: 0 0 250px;
}

/* flex-grow: 0; flex-shrink: 0; flex-basis: 250px; */

Best Practices

  • Use flex-basis, often via the flex shorthand, to set a flex item's intended starting size, rather than relying solely on width/height, once you need precise control interacting with flex-grow/flex-shrink
  • Use the flex: 1 shorthand, equivalent to flex-grow: 1, flex-shrink: 1, flex-basis: 0%, for items that should grow and shrink freely from essentially no fixed starting size
  • Set an explicit flex-basis value rather than leaving it at auto whenever you need flex-grow/flex-shrink calculations to start from a specific, known size rather than each item's natural content size

Interview Question

Why does flex: 1, shorthand for flex-basis: 0%, cause an item to essentially ignore its own content's natural size when determining how much space it ends up taking?

Hint: Think about what flex-basis: 0% specifically sets as the item's starting point, before flex-grow's proportional space distribution is even applied.

flex-basis: 0% explicitly sets the item's initial, pre-growth main-axis size to essentially zero, rather than to auto, which would otherwise fall back to the item's own natural, content-based size as that starting point — this means the flex-grow calculation that happens afterward isn't starting from, and therefore isn't influenced at all by, how much space the item's own actual content would naturally want to occupy. Because every sibling item using flex: 1 in this way is likewise starting from that same effectively-zero base, the final space each one ends up with is determined purely and entirely by the proportional flex-grow distribution among them, dividing up the container's available space strictly according to each item's flex-grow ratio, with each item's own individual content size playing no role whatsoever in that particular calculation. This is exactly why flex: 1 is such a common, reliable shorthand for creating perfectly equal-width flexible columns regardless of how much or how little actual content each one happens to contain, since the zero starting basis removes each item's own content size as a factor in the space-distribution calculation entirely.

Exercises

MediumPractice using Flex-basis in a real scenario.
View Solution
.item {
  flex: 1;
}

/* equivalent to: flex-grow: 1; flex-shrink: 1; flex-basis: 0%; */

Frequently Asked Questions

Why does flex: 1, shorthand for flex-basis: 0%, cause an item to essentially ignore its own content's natural size when determining how much space it ends up taking?

flex-basis: 0% explicitly sets the item's initial, pre-growth main-axis size to essentially zero, rather than to auto, which would otherwise fall back to the item's own natural, content-based size as that starting point — this means the flex-grow calculation that happens afterward isn't starting from, and therefore isn't influenced at all by, how much space the item's own actual content would naturally want to occupy. Because every sibling item using flex: 1 in this way is likewise starting from that same effectively-zero base, the final space each one ends up with is determined purely and entirely by the proportional flex-grow distribution among them, dividing up the container's available space strictly according to each item's flex-grow ratio, with each item's own individual content size playing no role whatsoever in that particular calculation. This is exactly why flex: 1 is such a common, reliable shorthand for creating perfectly equal-width flexible columns regardless of how much or how little actual content each one happens to contain, since the zero starting basis removes each item's own content size as a factor in the space-distribution calculation entirely.

Related Functions

Flex-growFlex-shrinkRefDimensions