Understanding the flex: 1
Property in CSS
What is the flex: 1
Property?
The flex: 1
property is a shorthand property that sets the flex-grow, flex-shrink, and flex-basis properties to 1. It is used to make a flex item take up equal space in a flex container.
Flex Shrink
The flex-shrink property in CSS determines: how much a flex item will shrink relative to the other flex items within the same flex container when there isn’t enough space available.
flex-shrink: 0:
Item will not shrink, even if it overflows the containerflex-shrink: 1:
Default; item shrinks at the same rate as othersflex-shrink: 2:
Item shrinks twice as much as items with 1
Flex Grow
The flex-grow property in CSS defines how much a flex item will grow relative to the other items inside a flex container when there is extra space available along the main axis (either horizontal or vertical, depending on flex-direction) of the container
flex-grow: 0:
Item will not growflex-grow: 1:
Default; item grows at the same rate as othersflex-grow: 2:
Item grows twice as much as items with 1
Flex Basis
The flex-basis property in CSS defines the initial size of a flex item before any available space is distributed. It can be a length (e.g., px, em, rem) or a percentage.
flex-basis: auto;
Default; item’s initial size is based on its contentflex-basis: 0;
Item’s initial size is 0flex-basis: 100px;
Item’s initial size is 100 pixels
When to Use the flex: 1
Property?
- Equal space: If you want to make a flex item take up equal space in a flex container, you can use the
flex: 1
property.
Example
- HTML
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
- CSS
.container {
display: flex;
}
.item {
flex: 1;
}