The flex
property is a shorthand for flex-grow
, flex-shrink
, and flex-basis
. It allows you to set all three properties at once, providing a more concise way to write your CSS.
Example 1: flex: 1
.flex-container {
display: flex;
width: 600px;
border: 1px solid #000;
}
.flex-item {
height: 100px;
background-color: tomato;
border: 1px solid #000;
flex: 1; /* equivalent to flex: 1 1 0% */
}
Explanation
flex: 1
is a common shorthand that translates to flex-grow: 1
, flex-shrink: 1
, and flex-basis: 0%
. This makes the flex items flexible and share the available space equally.
Example 2: flex: 0 0 200px
.flex-container {
display: flex;
width: 700px;
border: 1px solid #000;
}
.flex-item {
height: 100px;
background-color: turquoise;
border: 1px solid #000;
flex: 0 0 200px; /* flex-grow: 0, flex-shrink: 0, flex-basis: 200px */
}
Explanation
This shorthand creates an inflexible item. flex-grow: 0
prevents it from growing, flex-shrink: 0
prevents it from shrinking, and flex-basis: 200px
gives it a fixed size of 200px.
Example 3: flex: auto
.flex-container {
display: flex;
width: 800px;
border: 1px solid #000;
}
.flex-item {
width: 150px;
height: 100px;
background-color: violet;
border: 1px solid #000;
flex: auto; /* equivalent to flex: 1 1 auto */
}
Explanation
flex: auto
is equivalent to flex: 1 1 auto
. The items can grow and shrink, and their initial size is determined by their width
or height
property.
Example 4: flex: 2
.flex-container {
display: flex;
width: 100%;
border: 1px solid #000;
}
.flex-item {
height: 100px;
background-color: wheat;
border: 1px solid #000;
}
.item-1 {
flex: 1; /* flex: 1 1 0% */
}
.item-2 {
flex: 2; /* flex: 2 1 0% */
}
Explanation
When a single number is used for flex
, it represents the flex-grow
value. Here, .item-2
will be twice as wide as .item-1
because its flex-grow
is 2.
Example 5: flex: none
.flex-container {
display: flex;
width: 500px;
border: 1px solid #000;
}
.flex-item {
width: 200px;
height: 100px;
background-color: yellowgreen;
border: 1px solid #000;
flex: none; /* equivalent to flex: 0 0 auto */
}
Explanation
The none
keyword for the flex
property is equivalent to flex: 0 0 auto
. This creates an inflexible item that will not grow or shrink, with its size determined by its width or height.
Example 6: Two Value Syntax
.flex-container {
display: flex;
width: 600px;
border: 1px solid #000;
}
.flex-item {
height: 100px;
background-color: teal;
border: 1px solid #000;
flex: 1 200px; /* flex-grow: 1, flex-basis: 200px, flex-shrink defaults to 1 */
}
Explanation
When two values are provided, the first is flex-grow
and the second is flex-basis
. flex-shrink
defaults to 1. The items will start at 200px and grow to fill the container.
Example 7: Responsive Flex Shorthand
.flex-container {
display: flex;
width: 100%;
border: 1px solid #000;
}
.flex-item {
height: 100px;
background-color: slateblue;
border: 1px solid #000;
flex: 1 1 300px; /* Items will try to be 300px, but can grow and shrink */
}
@media (max-width: 768px) {
.flex-item {
flex: 1 1 100%; /* On smaller screens, items take up the full width */
}
}
Explanation
This example demonstrates how the flex
shorthand can be used in media queries for responsive design. On larger screens, items have a basis of 300px, but on smaller screens, they take up the full width of the container.