![]() |
VOOZH | about |
The CSS flex property is used for controlling the size and behavior of items within a flexible container. It combines three key properties—flex-grow, flex-shrink, and flex-basis—which determine how items grow, shrink, and set their initial size relative to the available space.
This property is essential for creating responsive layouts that adapt to different screen sizes, making content more dynamic and easier to manage.
/* flex: flex-grow flex-shrink flex-basis */
flex: 1 0 auto;| Property | Description |
|---|---|
flex-grow | Specifies how much an item will grow relative to other items in the container. |
flex-shrink | Determines how much an item will shrink relative to the rest of the flexible items when space is limited. |
flex-basis | Sets the initial size of a flex item. It can be auto, inherit, or a length value like 50%. |
flex-wrap | Controls whether flex items stay on a single line or can wrap onto multiple lines within the container. |
flex-grow specifies how much a flex item will grow relative to other items in the container when extra space is available. The value can be any positive number, with higher values allowing items to grow more.
Example:
.item1 {
flex-grow: 2;
}
.item2 {
flex-grow: 1;
}flex-shrink controls how much an item will shrink relative to other items when there is not enough space in the container. The value can be any positive number, with higher values indicating more shrinkage.
Example:
.item1 {
flex-shrink: 3;
}
.item2 {
flex-shrink: 1;
}
In this example, .item1 will shrink more than .item2 when space is limited.
flex-basis sets the initial size of a flex item before any space distribution occurs. It can be a length value (e.g., 100px), percentage (e.g., 50%), or keyword (auto or inherit).
Example:
.item1 {
flex-basis: 200px;
}
.item2 {
flex-basis: 30%;
}
Here are a few examples of the flex property that will help you better understand how it works.
In this example the CSS flex property creates a flexible container with equal-width items, adjusting their sizes dynamically based on available space within a defined container.
Output:
👁 CSS Flex property example output
This example describes the flex property with the help of the 3 values that represents the flex-grow, flex-shrink & flex-basis properties.
Output:
The flex property is widely supported across all modern browsers. Below are the details: