FLEX ITEM PROPERTIES
flex-grow
What it does
Controls how much an item grows compared to others when there is extra space.
Key Idea
- Default =
0โ item does NOT grow - Bigger number = takes more space
Example 1 (Equal growth)
.item {
flex-grow: 1;
}
๐ All items grow equally
Example:
- 3 items โ each gets equal width
Example 2 (Different growth)
.item:nth-child(1) {
flex-grow: 2;
}
๐ First item gets double space
Example:
- Item 1 โ 2 parts
- Others โ 1 part each
flex-shrink
What it does
Controls how items shrink when there is not enough space
Key Idea
- Default =
1โ items can shrink 0โ item will NOT shrink
Example
.item {
flex-shrink: 0;
}
๐ Items keep their size โ may overflow
๐ฅ Real Use Case
- Prevent logo or button from shrinking
- Keep important UI fixed size
flex-basis
๐ What it does
Sets the initial size of an item before growing/shrinking
๐ง Key Idea
- Works like
width(in row) - Works like
height(in column)
๐งช Example
.item {
flex-basis: 200px;
}
๐ Each item starts at 200px
โ ๏ธ Important
flex-basis overrides width
.item {
width: 100px;
flex-basis: 200px;
}
๐ Final size = 200px
flex (MOST IMPORTANT)
๐ What it does
Shortcut for:
flex: grow shrink basis;
๐งช Example
.item {
flex: 1 1 200px;
}
๐ Means:
- grow = 1 โ can expand
- shrink = 1 โ can shrink
- basis = 200px โ starting size
Responsive cards (BEST USE)
.item {
flex: 1 1 250px;
}
๐ Cards:
- Try to be 250px
- Grow if space
- Shrink if needed
align-self
๐ What it does
Overrides align-items for one specific item
๐ง Key Idea
- Works on cross-axis
- Individual control
๐งช Example
.item:nth-child(2) {
align-self: center;
}
๐ Only second item is centered
๐ฅ Values
flex-startflex-endcenterstretch
order
๐ Controls visual order of items (without changing HTML)
.item:nth-child(1) {
order: 2;
}
.item:nth-child(2) {
order: 1;
}
๐ Output:
- Item 2 appears first
- Item 1 appears later
๐ฅ Use case:
- Reordering layout in responsive design
Default Values
Most people forget this ๐
.container {
display: flex;
}
๐ Default behavior:
flex-direction: row; flex-wrap: nowrap; justify-content: flex-start; align-items: stretch; align-content: stretch;
๐ Important for debugging layouts