What You'll Learn
You'll learn how to use flex-basis to define the starting size of flex items before flexbox begins distributing extra space or shrinking items.
What is flex-basis?
Think of flex-basis as the "ideal size" you want a flex item to be before flex-grow and flex-shrink do their work. It's like telling each item, "Start at this size, then we'll adjust if needed."
While you've already learned that flex-grow distributes extra space and flex-shrink handles overflow, flex-basis sets the baseline size that those calculations work from.
How It Works
When you set flex-basis on a flex item, you're defining its initial main size (width in a row layout, height in a column layout) before any growing or shrinking happens. This is different from setting width or height directly—flex-basis is specifically designed to work within the flexbox sizing algorithm.
Common values:
- Length values:
flex-basis: 200px;— starts at 200 pixels - Percentage:
flex-basis: 50%;— starts at 50% of container - auto (default): Uses the item's
width/heightor content size - content: Uses the natural content size (newer)
Why It Matters
The flex-basis property gives you precise control over the starting point of your flex item sizing. Combined with flex-grow and flex-shrink, you can create sophisticated layouts where items start at specific sizes, then respond to available space.
For example, a sidebar might have flex-basis: 250px, while the main content area has flex-basis: 0 with flex-grow: 1 to fill remaining space.
Key Takeaway: flex-basis sets the initial size of flex items before growth or shrinkage occurs, giving you control over the starting point of flexbox's sizing calculations.