On this page
Flex Container and Flex Items
Flex Container and Flex Items Explained
Every flex layout has two roles: the flex container (the parent that has display: flex) and the flex items (its direct children). Knowing which property belongs to which role is the single biggest time saver when learning CSS Flexbox. Half of all Flexbox bugs come from putting a property on the wrong element.
What You Will Learn
- How to create a flex container with
display: flexanddisplay: inline-flex - Which elements become flex items (and which do not)
- The complete list of container properties and item properties
- What stops working on flex items (float, clear, vertical-align)
- How nested flex containers work
- The
min-width: autogotcha that causes overflow
Creating a Flex Container
.container {
display: flex; /* block-level flex container */
}
.inline-container {
display: inline-flex; /* inline-level flex container */
}Both values create a flex formatting context for the children. The difference is only how the container itself sits in its own parent:
display: flex: the container is block-level and takes the full available width.display: inline-flex: the container is inline-level and shrinks to fit its content, like an inline-block. Useful for badges, pill buttons and icon+text labels.
What Becomes a Flex Item?
Only the direct children of a flex container become flex items. Grandchildren are not affected and behave normally inside their parent item.
<div class="container"> <!-- flex container -->
<div class="item"> <!-- flex item -->
<p>Normal paragraph</p> <!-- NOT a flex item -->
</div>
<div class="item">Two</div> <!-- flex item -->
</div>Special Cases
- Text directly inside the container is wrapped into an anonymous flex item automatically.
- Whitespace-only text is ignored, so there are no inline-block style gaps.
- Absolutely positioned children do not participate in flex layout; they are taken out of flow.
- Children with
display: nonegenerate no flex item. - Margins do not collapse between flex items, unlike block margins.
Properties That Go on the Container
| Property | What it controls | Common values |
|---|---|---|
display | Turns on flex layout | flex, inline-flex |
flex-direction | Direction of the main axis | row, row-reverse, column, column-reverse |
flex-wrap | Whether items can wrap to new lines | nowrap, wrap, wrap-reverse |
flex-flow | Shorthand for direction + wrap | row wrap |
justify-content | Space distribution on the main axis | flex-start, center, space-between, space-around, space-evenly |
align-items | Alignment of items on the cross axis | stretch, flex-start, center, flex-end, baseline |
align-content | Spacing of wrapped lines on the cross axis | flex-start, center, space-between |
gap, row-gap, column-gap | Space between items | 16px, 1rem |
Properties That Go on the Items
| Property | What it controls | Default |
|---|---|---|
flex-grow | How much of the free space this item takes | 0 |
flex-shrink | How willing the item is to shrink when space is short | 1 |
flex-basis | Starting size before growing or shrinking | auto |
flex | Shorthand: grow shrink basis | 0 1 auto |
align-self | Overrides align-items for one item | auto |
order | Visual order of the item | 0 |
Memory trick: container properties describe the group (direction, wrap, distribution, alignment). Item properties describe one child (how much it grows, shrinks, where it starts, whether it aligns differently).
The flex Shorthand You Will Use Daily
| Declaration | Meaning | Typical use |
|---|---|---|
flex: 1 | grow 1, shrink 1, basis 0 | Equal-width columns, fill remaining space |
flex: auto | grow 1, shrink 1, basis auto | Grow from content size |
flex: none | grow 0, shrink 0, basis auto | Fixed-size item that never resizes |
flex: 0 0 250px | no grow, no shrink, 250px wide | Sidebar with a locked width |
What Stops Working on Flex Items
floatandclearhave no effect on flex items.vertical-alignhas no effect; usealign-itemsoralign-self.displayon an item is "blockified": an item set toinlinebehaves likeblock.::first-lineand::first-letterdo not apply to the container itself.
Nested Flex Containers
A flex item can itself be a flex container. This is extremely common: a navbar (flex row) contains a link group (flex row) and each card in a list (flex row) contains its own content (flex column). Each container manages only its own direct children, so you can combine rows and columns freely.
The min-width: auto Gotcha
Flex items have an automatic minimum size equal to their content. That means a long word, a wide image or a pre block can refuse to shrink and push the layout wider than its container. The fix is to allow the item to shrink:
.item {
min-width: 0; /* allow shrinking below content size */
overflow: hidden; /* or auto, to handle overflow */
}This tiny fix solves a large share of "why is my flex layout overflowing" questions, especially with text truncation (text-overflow: ellipsis) inside flex items.
Common Mistakes
- Applying
justify-contentto an item. It belongs on the container. - Applying
flex-growto the container. It belongs on the items. - Expecting nested elements to flex. Add
display: flexto that nested parent as well. - Using margin instead of gap.
gapis cleaner: no extra space after the last item. - Overusing
order. Screen readers and keyboard focus follow source order, so large visual reordering can hurt accessibility.
Key Takeaways
- The container has
display: flex; its direct children are the items. - Container properties:
flex-direction,flex-wrap,justify-content,align-items,align-content,gap. - Item properties:
flex-grow,flex-shrink,flex-basis,flex,align-self,order. - Use
min-width: 0to fix overflow caused by unshrinkable content.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Make .layout a flex container with a 16px gap. The .sidebar must be exactly 200px wide and never grow or shrink. The two .col elements must share the remaining space equally. Use the flex shorthand on every item.
Press Run to execute.
Show expected output
Sidebar is 200px wide, two blue columns are equal width and fill the rest, 16px gap between all three.This is a self-check — compare your result with the expected output above.
The long title pushes the layout wider than the 360px container. Without changing the HTML, fix the CSS so the title is truncated with an ellipsis and the button stays fully visible on the right.
Press Run to execute.
Show expected output
The title is cut with an ellipsis (...) inside the bar and the Open button stays visible inside the 360px container.This is a self-check — compare your result with the expected output above.
Was this page helpful?