CSS flex-basis Property
The flex-basis property defines the initial main size of a flex item before the flex layout algorithm distributes available space.
It works with flex-grow and flex-shrink to determine the final size of an item. Depending on the flex direction, the main size corresponds to either its width or height.
flex-basis Syntax
The flex-basis property accepts automatic sizing, content-based sizing, lengths, percentages, and other supported sizing keywords.
Basic Syntax
selector {
flex-basis: 200px;
}
Keyword Values
.item {
flex-basis: auto;
}
.item {
flex-basis: content;
}
.item {
flex-basis: min-content;
}
.item {
flex-basis: max-content;
}
.item {
flex-basis: fit-content;
}
Length Values
.item {
flex-basis: 150px;
}
.item {
flex-basis: 12rem;
}
.item {
flex-basis: 0;
}
Percentage Values
.item {
flex-basis: 25%;
}
.item {
flex-basis: 50%;
}
CSS-Wide Keywords
.item {
flex-basis: inherit;
}
.item {
flex-basis: initial;
}
.item {
flex-basis: unset;
}
.item {
flex-basis: revert;
}
.item {
flex-basis: revert-layer;
}
The initial value is auto. The property has an effect on flex items, which are direct children of a flex container.
Property Values
The value determines how the flex item's initial main size is calculated.
| Value | Description |
|---|---|
auto |
Uses the item's main-size property, or content-based sizing when that size is automatic. |
content |
Determines the flex basis from the item's content rather than its specified main size. |
<length> |
Sets a nonnegative length such as 200px or 15rem. |
<percentage> |
Sets a percentage relative to the flex container's inner main size when that size is definite. |
min-content |
Uses the minimum intrinsic size of the content. |
max-content |
Uses the maximum intrinsic size of the content. |
fit-content |
Uses content-based sizing constrained by the available space. |
fit-content() |
Uses a supplied sizing limit. Support and behavior should be checked for the intended browser environment. |
calc() |
Calculates a basis using supported length and percentage expressions. |
CSS-Wide Values
| Value | Description |
|---|---|
inherit |
Uses the computed value of the parent element. |
initial |
Restores the initial value, auto. |
unset |
Restores auto because the property is not inherited. |
revert |
Reverts the property according to the CSS cascade origins. |
revert-layer |
Reverts the property according to the CSS cascade layers. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | No |
| Applies to | Flex items |
| Computed value | As specified, with relative lengths converted to absolute lengths |
| Animation type | By computed value type |
| Percentages | Relative to the flex container's inner main size |
Some intrinsic and newer sizing values have more limited browser support than the basic auto, length, and percentage values.
How It Works
Flexbox uses flex-basis to establish a starting main size before distributing positive or negative free space.
Step 1: Create a Flex Container
.container {
display: flex;
gap: 10px;
}
Step 2: Set the Initial Basis
.item {
flex-basis: 200px;
}
Each flex item starts with a flex basis of 200px.
Step 3: Allow Flexible Growth
.item {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 200px;
}
Items can now grow when extra space is available or shrink when their combined sizes exceed the available space.
Step 4: Use the Shorthand
.item {
flex: 1 1 200px;
}
This declaration produces the same three flex component values as the previous example.
Important Distinction
The flex basis is not necessarily the final width or height. The flex algorithm can adjust the item's target main size according to growth, shrinking, and sizing constraints.
auto Value
The auto value uses the item's main-size property as its starting point. If that property is also auto, content-based sizing is used.
Syntax
.item {
flex-basis: auto;
}
Example with Width
.container {
display: flex;
}
.item {
width: 180px;
flex-basis: auto;
}
In a row flex container, the item's specified width provides the flex basis.
Example with Automatic Width
.item {
width: auto;
flex-basis: auto;
}
The item's content contributes to determining its flex basis.
Important Consideration
auto does not always mean that an item uses only its content size. A definite width or height can determine its basis, depending on the main axis.
content Value
The content value calculates the flex basis from the item's content, rather than using a specified main-size property.
Syntax
.item {
flex-basis: content;
}
Example
.container {
display: flex;
gap: 10px;
}
.item {
width: 200px;
flex: 0 1 content;
}
The item's content determines the flex basis instead of the specified 200px width. The final size may still be affected by shrinking and minimum-size constraints.
auto vs. content
| Value | Starting Size |
|---|---|
auto |
Uses the main-size property when specified, otherwise content-based sizing. |
content |
Uses content-based sizing without taking the specified main-size property as its basis. |
Important Consideration
The content value is useful when the intrinsic content size should determine the starting point, even if a width or height is declared.
Length Values
A length establishes a specific flex basis using units such as pixels, rems, or ems.
Pixel Basis
.item {
flex-basis: 200px;
}
Relative Length Basis
.item {
flex-basis: 15rem;
}
Calculated Basis
.item {
flex-basis: calc(50% - 10px);
}
Example with Different Bases
.sidebar {
flex: 0 1 180px;
}
.main-content {
flex: 1 1 400px;
}
The sidebar begins with a basis of 180px, while the main content begins with 400px. Both may shrink, and the main content can grow to use positive free space.
Important Consideration
Negative lengths are invalid for flex-basis. A specified length does not automatically prevent shrinking or growing.
Percentage Values
Percentage values calculate the flex basis relative to the inner main size of the flex container.
Syntax
.item {
flex-basis: 50%;
}
Example
.container {
display: flex;
width: 800px;
}
.item {
flex: 0 1 25%;
}
Each item begins with a basis of 200px, before the flex algorithm applies any necessary adjustments.
Percentage Basis with Gaps
.container {
display: flex;
gap: 20px;
}
.item {
flex: 0 0 50%;
}
Two items with a 50% basis plus a gap can exceed the available width because the gap is additional space.
Adjusted Percentage Basis
.item {
flex: 0 0 calc(50% - 10px);
}
For a two-column layout with one 20px gap, this calculation reserves half of the gap from each item's basis.
Important Consideration
When the flex container's main size is indefinite, percentage flex-basis values resolve to content-based sizing rather than a predictable percentage length.
Zero Basis
A zero flex basis starts the flex sizing calculation without an initial main-size allocation from flex-basis.
Syntax
.item {
flex-basis: 0;
}
Equal Growth
.container {
display: flex;
}
.item {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
min-width: 0;
}
With equal growth factors, the items can receive equal shares of available space, assuming no other constraints interfere.
Different Growth Factors
.first {
flex: 1 1 0;
}
.second {
flex: 2 1 0;
}
.third {
flex: 1 1 0;
}
When sufficient positive space exists, the second item receives twice the growth allocation of the first or third item.
Important Consideration
In the flex shorthand, browsers commonly expand flex: 1 to 1 1 0%. An explicit flex-basis: 0 uses a zero length, which can behave differently from 0% in containers with indefinite main sizes.
Flex Direction
The flex-direction property determines which dimension is the main axis and therefore which dimension flex-basis controls.
Row Direction
.container {
display: flex;
flex-direction: row;
}
.item {
flex-basis: 200px;
}
In a row layout, the flex basis determines the initial horizontal main size.
Column Direction
.container {
display: flex;
flex-direction: column;
height: 500px;
}
.item {
flex-basis: 150px;
}
In a column layout, the flex basis determines the initial vertical main size.
Direction Comparison
| Flex Direction | Main Axis | Dimension Affected |
|---|---|---|
row |
Inline direction | Main-axis width in horizontal writing modes |
row-reverse |
Reverse inline direction | Main-axis width in horizontal writing modes |
column |
Block direction | Main-axis height in horizontal writing modes |
column-reverse |
Reverse block direction | Main-axis height in horizontal writing modes |
Important Consideration
The physical dimension depends on the writing mode. Flexbox uses logical main-axis sizing rather than always treating flex-basis as a width.
flex-basis vs. width
The flex-basis and width properties can both influence a flex item's horizontal size, but they are not interchangeable.
Using width
.item {
width: 250px;
}
Using flex-basis
.item {
flex-basis: 250px;
}
Using Both Properties
.item {
width: 300px;
flex-basis: 200px;
}
In a row flex container, the 200px flex basis generally determines the starting main size instead of the specified 300px width.
Using auto with width
.item {
width: 300px;
flex-basis: auto;
}
Here, the specified width provides the flex basis in a row layout.
Comparison
| Behavior | flex-basis | width |
|---|---|---|
| Purpose | Sets the initial flex main size | Sets a preferred horizontal size |
| Direction-aware | Yes | No |
| Flex algorithm | Directly participates as the starting basis | May provide the basis when flex-basis: auto |
| Outside Flexbox | Does not affect normal block sizing | Continues to control width |
Important Consideration
When designing flexible layouts, prefer flex-basis for the initial main-axis size and use width when a horizontal dimension is specifically required.
Flex Shorthand
The flex-basis property is one of the three components controlled by the flex shorthand.
Individual Properties
.item {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 200px;
}
Equivalent Shorthand
.item {
flex: 1 1 200px;
}
Common Shorthand Values
| Declaration | Basis | Behavior |
|---|---|---|
flex: initial |
auto |
Does not grow, but can shrink. |
flex: auto |
auto |
Can grow and shrink. |
flex: none |
auto |
Neither grows nor shrinks. |
flex: 1 |
Zero basis | Can grow and shrink from a zero starting basis. |
flex: 1 1 200px |
200px |
Can grow and shrink from a 200px basis. |
flex: 0 0 200px |
200px |
Uses a nonflexible 200px basis. |
Important Consideration
Setting the flex shorthand resets all three flex components. A later shorthand declaration can therefore replace an earlier flex-basis value.
Growing and Shrinking
The flex basis provides the starting point for distributing free space, while flex-grow and flex-shrink control adjustments.
Growing from a Basis
.item {
flex: 1 1 150px;
}
Items begin with a basis of 150px and can grow when the container has extra main-axis space.
Shrinking from a Basis
.item {
flex: 0 1 250px;
}
Items begin with a basis of 250px and may shrink when the combined sizes exceed the available space.
Preventing Flexible Changes
.item {
flex: 0 0 200px;
}
The item does not participate in flexible growth or shrinking, although other sizing constraints can still affect its rendered size.
Different Starting Bases
.first {
flex: 1 1 100px;
}
.second {
flex: 1 1 250px;
}
Both items have the same growth factor, but the second begins with a larger basis. With positive free space and no limiting constraints, each receives an equal growth allocation, so their final sizes generally remain different.
Important Consideration
Negative free space is distributed using scaled shrink factors that account for both the shrink factor and the flex base size. Items with different bases may therefore shrink by different amounts.
Responsive Layouts
The flex-basis property is useful for responsive card layouts, flexible columns, and sidebars.
Responsive Cards
.card-container {
display: flex;
flex-wrap: wrap;
gap: 15px;
}
.card {
flex: 1 1 220px;
min-width: 0;
}
Each card starts with a basis of 220px and can grow, shrink, or wrap depending on the available space.
Sidebar Layout
.layout {
display: flex;
gap: 20px;
}
.sidebar {
flex: 0 0 220px;
}
.main {
flex: 1 1 0;
min-width: 0;
}
The sidebar uses a nonflexible basis of 220px, while the main area occupies the remaining space.
Responsive Adjustment
@media (max-width: 700px) {
.layout {
flex-direction: column;
}
.sidebar {
flex: 0 1 auto;
}
.main {
flex: 1 1 auto;
}
}
On smaller screens, the layout changes to a vertical arrangement and uses automatic bases for both sections.
Important Consideration
flex-basis does not cause wrapping by itself. The flex container must use flex-wrap: wrap when items should move onto additional flex lines.
Minimum and Maximum Sizes
The final size of a flex item can be limited by minimum and maximum sizing properties, even when a different flex basis is specified.
Minimum Width
.item {
flex: 1 1 100px;
min-width: 150px;
}
The minimum width can prevent the item from becoming narrower than 150px.
Maximum Width
.item {
flex: 1 1 200px;
max-width: 350px;
}
The maximum width limits the item's growth to 350px in a horizontal flex layout.
Allowing Shrinking
.item {
flex: 1 1 0;
min-width: 0;
}
Setting min-width: 0 removes the automatic minimum-width restriction that can otherwise prevent a row flex item from shrinking below its content-based minimum.
Column Layout
.container {
display: flex;
flex-direction: column;
height: 400px;
}
.content {
flex: 1 1 0;
min-height: 0;
overflow: auto;
}
For a vertical flex layout, min-height: 0 can allow the content area to shrink and scroll inside a constrained container.
Important Consideration
The specified flex basis is not an absolute minimum or maximum. Use min-width, max-width, min-height, or max-height when those constraints are required.
flex-basis Example
This example compares equal flex bases, different flex bases, and a layout with a fixed sidebar and flexible main content.
HTML Example
<div class="basis-demo">
<h3>Equal Flex Bases</h3>
<div class="basis-row equal">
<div>100px</div>
<div>100px</div>
<div>100px</div>
</div>
<h3>Different Flex Bases</h3>
<div class="basis-row different">
<div>100px</div>
<div>200px</div>
<div>100px</div>
</div>
<h3>Fixed Sidebar and Flexible Content</h3>
<div class="basis-row mixed">
<div class="sidebar">Sidebar</div>
<div class="main">Main Content</div>
</div>
</div>
CSS Example
.basis-demo .basis-row {
display: flex;
gap: 8px;
margin-bottom: 20px;
}
.basis-demo .basis-row > div {
box-sizing: border-box;
min-width: 0;
padding: 15px 8px;
background-color: #dceaf7;
text-align: center;
}
.basis-demo .equal > div {
flex: 1 1 100px;
}
.basis-demo .different > div {
flex: 1 1 100px;
}
.basis-demo .different > div:nth-child(2) {
flex-basis: 200px;
}
.basis-demo .mixed .sidebar {
flex: 0 0 100px;
}
.basis-demo .mixed .main {
flex: 1 1 0;
}
Equal Flex Bases
Different Flex Bases
Fixed Sidebar and Flexible Content
The first row starts with equal bases and distributes positive free space equally. The second row gives the middle item a larger starting basis. The third row uses a fixed sidebar basis while the main content grows to fill the remaining space.
The final widths depend on the available container space, gaps, and any applicable sizing constraints.
Accessibility
The flex-basis property changes the visual size of flex items without changing their semantic meaning or document order.
Allow Content to Resize
Test layouts with enlarged text and different viewport widths. Fixed bases should not prevent content from remaining readable.
Avoid Horizontal Overflow
Use wrapping or responsive adjustments when fixed or percentage bases would cause items to exceed the available width.
Preserve Logical Source Order
Keep HTML elements in a meaningful reading sequence. Flex sizing should not be used to compensate for an incorrect document structure.
Maintain Usable Controls
Ensure that form fields, buttons, and navigation links have enough space for their labels and remain operable when flex items shrink.
Important Consideration
A responsive layout should accommodate text resizing, keyboard navigation, and narrow screens without clipping essential information.
Important Considerations
Keep the following points in mind when using flex-basis:
- Purpose: Establishes the initial main size of a flex item.
- Initial value:
auto. - Inheritance: The property is not inherited.
- Applies to: Flex items.
- Main axis: Determined by
flex-directionand the writing mode. - auto: Uses the main-size property or falls back to content-based sizing.
- content: Uses content-based sizing instead of a specified main-size property.
- Lengths: Accepts nonnegative values such as
200pxand15rem. - Percentages: Resolve against the container's inner main size when definite.
- Indefinite sizes: Percentage bases use content-based sizing when the container's main size is indefinite.
- Zero basis: Commonly used to distribute space according to growth factors.
- Intrinsic sizing: Supports content-based sizing keywords, with compatibility differences for newer forms.
- flex-grow: Controls allocation of positive free space.
- flex-shrink: Controls reduction when negative free space exists.
- Flex shorthand: Combines growth, shrinking, and basis values.
- Width and height: Can supply the basis when
flex-basis: autois used. - Minimum sizes: Can prevent an item from shrinking below a specified or automatic minimum.
- Maximum sizes: Can limit the final size after flexible growth.
- Wrapping: Controlled by
flex-wrap, notflex-basis. - Gaps: Occupy space in the container and affect the remaining space available to flex items.
- Responsive layouts: Can combine flexible bases with wrapping and media queries.
- Accessibility: Ensure that content remains readable when flex items resize.
Use flex-basis to define a starting size that can participate in flexible layout calculations. For most layouts, combine it with flex-grow and flex-shrink through the flex shorthand.
Browser Support
The CSS flex-basis property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.
Basic values such as auto, lengths, and percentages have broad support. Some intrinsic and newer sizing values may have additional compatibility limitations.
Supported Example
.container {
display: flex;
}
.item {
flex-basis: 200px;
}
Feature Detection
@supports (display: flex) and (flex-basis: 200px) {
.container {
display: flex;
}
.item {
flex-basis: 200px;
}
}
Fallback Example
.item {
width: 200px;
}
@supports (display: flex) and (flex-basis: 200px) {
.item {
flex: 1 1 200px;
}
}
The width provides a basic size when Flexbox is unavailable, while supported browsers can use flexible sizing.
Compatibility Considerations
Older browsers may require legacy Flexbox implementations. Test intrinsic sizing keywords and percentage bases in the browsers your site supports.
For current compatibility information, visit Can I Use? .
Specifications
The flex-basis property is defined in the CSS Flexible Box Layout Module Level 1. The specification describes how a flex item's initial main size is established and used during flexible layout calculations.
CSS Flexible Box Layout Module Level 1: flex-basis Property
The Flexbox layout algorithm defines how the flex base size interacts with growth, shrinking, and minimum or maximum size constraints.
CSS Flexible Box Layout Module Level 1: Flex Layout Algorithm
Additional sizing terminology and intrinsic sizing behavior are described in the CSS Box Sizing specifications.
