What are flex items?
In flexbox, items are the direct children of a flex container. While the container controls the overall layout (direction, wrapping, alignment), flex items control their own sizing and growth.
So far you've learned to control the container with properties like flex-direction, justify-content, and align-items. Now it's time to focus on the items themselves.
There are 6 main flex item properties you need to know:
flex-grow— How much the item grows when there's extra spaceflex-shrink— How much the item shrinks when space is tightflex-basis— The starting size before growing or shrinkingflex— Shorthand for grow, shrink, and basisalign-self— Override the container's alignment for this itemorder— Change the visual order without touching HTML
flex-grow: sharing available space
flex-grow controls how much an item expands when there's extra space in the container. The default is 0 (don't grow).
The math is simple: available space is divided among all items by their flex-grow ratio. If one item has flex-grow: 2 and another has flex-grow: 1, the first takes twice as much extra space.
Practical use case: In a navigation bar, you want the logo fixed-width, search bar to grow and fill space, and user menu fixed-width. Use flex-grow: 0 on logo and menu, flex-grow: 1 on search.
flex-basis and flex-shrink
flex-basis is the starting size of an item before any growing or shrinking happens. It's like width, but smarter — it's the "opinion" of the item before the container makes decisions.
flex-shrink (default: 1) controls how much an item shrinks when the container is too small. Set it to 0 to prevent shrinking.
flex-basis: 0 with flex-grow: 1 to make items grow equally from zero, ignoring their content size. This is the secret to even-width columns.
The flex shorthand
Instead of writing flex-grow, flex-shrink, and flex-basis separately, use the flex shorthand:
Most common patterns:
flex: 1— flexible item that grows and shrinks equallyflex: 0 0 300px— fixed-width item (doesn't grow or shrink)flex: auto— item that grows but maintains minimum content size
align-self and order
align-self lets a single item override the container's alignment (the align-items property). Useful when you want one item to behave differently.
order changes the visual order of items without touching the HTML. This is powerful for responsive design — you can change layout order on mobile without duplicating content.
Task: Create a header layout with three sections using flex properties:
- Logo (fixed 200px width, left side)
- Search bar (grows to fill available space)
- User menu (fixed 150px width, right side)
Requirements:
- All items vertically centered in the header
- Logo:
flex: 0 0 200px - Search:
flex: 1 - Menu:
flex: 0 0 150px - Use a border or background to make each section visible
Hint: Use display: flex on the header. Set gap for spacing. Use align-items: center for vertical alignment. Apply the flex values to each child element.
Key concepts to remember
- flex-grow controls how items expand. Items share available space proportionally based on their flex-grow values.
- flex-basis is the starting size before any growing/shrinking.
flex-basis: 0makes items grow from zero. - flex-shrink controls shrinking. Set to
0to prevent an item from shrinking (useful for sidebars). - flex shorthand combines grow, shrink, and basis:
flex: grow shrink basis. - align-self overrides the container's alignment for a single item.
- order changes visual order without changing HTML — perfect for responsive design.
flex: 1 is your friend. It makes items flexible and responsive without overthinking. For fixed sizes, use flex: 0 0 SIZE.
Test your knowledge
You just mastered flexbox items.
Ready for the next challenge?
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →