π Introduction
Flexbox Item Properties control how individual flex items (children inside a flex container) behave, grow, shrink, and align. These properties are essential for building responsive layouts, controlling item spacing, distributing leftover space, and designing modern UI components. Letβs explore all Flex Item properties in a clean and visual way! π
π§© What Are Flex Items?
Flex items are the direct children of a flex container (i.e., an element withdisplay: flex or display: inline-flex).
Note
π Flexbox Item Properties (List)
- order β controls the visual order
- flex-grow β controls how items expand
- flex-shrink β controls how items shrink
- flex-basis β defines the initial size
- flex β shorthand (grow, shrink, basis)
- align-self β overrides cross-axis alignment
π₯ 1. order
Controls the visual placement of an item regardless of DOM order.
order Example
.item {
order: 3; /* Default is 0 */
}Note
π₯ 2. flex-grow
Defines how much a flex item should grow relative to the remaining space. A value of 1 lets the item grow to fill extra space.
flex-grow Example
.item {
flex-grow: 1; /* Will grow and fill space */
}π₯ 3. flex-shrink
Defines how much a flex item should shrink when space is limited.
flex-shrink Example
.item {
flex-shrink: 0; /* Prevent shrinking */
}π₯ 4. flex-basis
Specifies the initial size of a flex item before flex-grow or flex-shrink calculations.
flex-basis Example
.item {
flex-basis: 200px;
}Note
π₯ 5. flex (Shorthand)
Combines flex-grow, flex-shrink, andflex-basis into one powerful shorthand.
flex Shorthand
/* flex: grow shrink basis */
.item {
flex: 1 1 200px;
}Common flex shorthand presets:
- flex: 1 β flex: 1 1 0 (items grow evenly)
- flex: none β flex: 0 0 auto
- flex: auto β flex: 1 1 auto
π₯ 6. align-self
Overrides the containerβs align-items property for a single flex item. Useful when one item needs unique alignment.
align-self Example
.item {
align-self: center;
/* Options: auto, flex-start, flex-end, center, baseline, stretch */
}π Summary Table
| Property | Purpose | Example |
|---|---|---|
| order | Controls the visual ordering | order: 3; |
| flex-grow | Expands items to fill leftover space | flex-grow: 1; |
| flex-shrink | Allows shrinking when space is small | flex-shrink: 0; |
| flex-basis | Sets initial space an item occupies | flex-basis: 150px; |
| flex | Shorthand for grow, shrink, basis | flex: 1 1 auto; |
| align-self | Overrides cross-axis alignment | align-self: center; |
π₯ Real-World Example
Real Flex Item Example
.container {
display: flex;
gap: 10px;
}
.item1 {
flex: 2; /* grows twice as much */
}
.item2 {
flex: 1; /* standard grow */
}
.item3 {
flex-shrink: 0; /* never shrink */
align-self: flex-end;
}π§ Tips & Best Practices
- Use flex shorthand for clean code
- Use flex-grow for fluid layouts (e.g., navigation bars)
- Use align-self for one-off alignment overrides
- Avoid using too many custom order values β it harms accessibility
π Conclusion
Flexbox Item Properties give you fine-grained control over how individual elements behave inside a flex container. Mastering grow, shrink, basis, order, and alignment lets you build beautiful, responsive UI layouts that adapt effortlessly across screen sizes. Combine them with Flex Container properties for complete layout mastery! π