Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Set the initial length of a flex item with CSS
The CSS flex-basis property sets the initial main size of a flex item before free space is distributed. It defines the default size of an element before the remaining space is distributed according to the flex factors.
Syntax
selector {
flex-basis: value;
}
Possible Values
| Value | Description |
|---|---|
auto |
Default value. The length is equal to the length of the flexible item |
length |
A length unit (px, em, rem, etc.) specifying the initial length |
% |
A percentage of the parent container's main size |
content |
Size based on the item's content |
Example
The following example demonstrates how flex-basis sets the initial width of flex items. Notice how the fourth item (Q4) has a larger initial width −
<!DOCTYPE html>
<html>
<head>
<style>
.mycontainer {
display: flex;
background-color: orange;
padding: 10px;
gap: 5px;
}
.mycontainer > div {
background-color: white;
text-align: center;
line-height: 40px;
font-size: 25px;
flex-basis: 100px;
border-radius: 4px;
}
.special-item {
flex-basis: 250px;
background-color: #e3f2fd !important;
}
</style>
</head>
<body>
<h3>Flex Items with Different flex-basis Values</h3>
<div class="mycontainer">
<div>Q1</div>
<div>Q2</div>
<div>Q3</div>
<div class="special-item">Q4</div>
<div>Q5</div>
<div>Q6</div>
</div>
</body>
</html>
A horizontal flex container with orange background containing six items. Items Q1, Q2, Q3, Q5, and Q6 appear as white boxes with 100px initial width, while Q4 appears as a light blue box with 250px initial width, demonstrating the flex-basis property effect.
Conclusion
The flex-basis property is essential for controlling the initial size of flex items. It works alongside flex-grow and flex-shrink to create flexible layouts that respond to available space.
Advertisements
