02. Flex Item Sizing (grow, shrink, basis, align-self, order)
🟡 Intermediate
1. Definition
Flex Item properties control child element growth and alignment: flex-grow (expands into free space), flex-shrink (shrinks on overflow), flex-basis (initial main size), align-self (overrides container alignment), and order (reorders elements visually).
2. Why It Is Used
Flex item properties enable fluid layouts where main content areas expand to consume remaining window width while sidebars remain fixed.
3. How It Works
The flex: grow shrink basis shorthand sets all three sizing parameters. flex: 1 1 auto is a common flexible preset.
4. Syntax
.main-content {
flex: 1 1 0%; /* flex-grow: 1, flex-shrink: 1, flex-basis: 0% */
}
.special-item {
align-self: flex-end;
}
5. Flex Item Properties Reference Table
| Property | Default Value | Purpose & Function |
|---|---|---|
flex-grow | 0 | Proportional growth factor into remaining container space |
flex-shrink | 1 | Proportional shrinkage factor when container overflows |
flex-basis | auto | Initial main size before growing or shrinking |
align-self | auto | Overrides container align-items for single item |
order | 0 | Visually reorders items without changing HTML source |
6. Beginner Example
.fill-remaining {
flex-grow: 1;
}
7. Intermediate Example
.sidebar {
flex: 0 0 280px; /* Fixed width 280px, does not grow or shrink */
}
.content-area {
flex: 1 1 auto; /* Takes remaining screen space */
}
8. Advanced Visual Reordering Example
/* Moves sidebar above main content on mobile screens */
@media (max-width: 768px) {
.sidebar {
order: -1;
}
}
9. Line-by-Line Code Explanation
flex: 0 0 280px;: Setsflex-grow: 0,flex-shrink: 0, andflex-basis: 280px, pinning the sidebar to exactly 280px.order: -1;: Promotes the sidebar to the front of the visual order without modifying HTML structure.
10. Expected Terminal / Browser Output
A responsive layout with a fixed sidebar and expanding content area that automatically reflows on mobile devices.
11. Common Mistakes & Pitfalls
- Using order excessively for accessibility: Changing visual
orderwithout updating HTML breaks keyboard tab navigation focus for screen readers.
12. Important Rules & Limitations
- Always set
flex-basis: 0%when using equal-width column layouts withflex-grow: 1.
13. Multilingual Explanation: English
Flex item properties control individual child sizing. Use flex: 1 to make items fill remaining space, and align-self to override item alignment.
14. Multilingual Explanation: Hindi (Roman Script)
Flex items flex-grow se bachi hui jagah fill karte hain aur align-self se individual item ki cross-axis alignment change hoti hai.
15. Multilingual Explanation: Marathi (Roman Script)
Flex item size flex-grow mule wadhva. Single item align karnyasathi align-self vapara.
16. Practice Questions
- What is the difference between
flex: 1andflex: 0 0 300px?
17. Practical Coding Challenge
Write CSS rules for a .flexible-input that expands to fill form space (flex: 1) alongside a fixed button (flex: 0 0 auto).