Skip to main content
Syllabus
On this page

Flex Container and Flex Items

Badar KhalilUpdated October 1, 2026 6 min read

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: flex and display: 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: auto gotcha that causes overflow

Creating a Flex Container

CSS
.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.

HTML
<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: none generate no flex item.
  • Margins do not collapse between flex items, unlike block margins.

Properties That Go on the Container

PropertyWhat it controlsCommon values
displayTurns on flex layoutflex, inline-flex
flex-directionDirection of the main axisrow, row-reverse, column, column-reverse
flex-wrapWhether items can wrap to new linesnowrap, wrap, wrap-reverse
flex-flowShorthand for direction + wraprow wrap
justify-contentSpace distribution on the main axisflex-start, center, space-between, space-around, space-evenly
align-itemsAlignment of items on the cross axisstretch, flex-start, center, flex-end, baseline
align-contentSpacing of wrapped lines on the cross axisflex-start, center, space-between
gap, row-gap, column-gapSpace between items16px, 1rem

Properties That Go on the Items

PropertyWhat it controlsDefault
flex-growHow much of the free space this item takes0
flex-shrinkHow willing the item is to shrink when space is short1
flex-basisStarting size before growing or shrinkingauto
flexShorthand: grow shrink basis0 1 auto
align-selfOverrides align-items for one itemauto
orderVisual order of the item0

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

DeclarationMeaningTypical use
flex: 1grow 1, shrink 1, basis 0Equal-width columns, fill remaining space
flex: autogrow 1, shrink 1, basis autoGrow from content size
flex: nonegrow 0, shrink 0, basis autoFixed-size item that never resizes
flex: 0 0 250pxno grow, no shrink, 250px wideSidebar with a locked width

What Stops Working on Flex Items

  • float and clear have no effect on flex items.
  • vertical-align has no effect; use align-items or align-self.
  • display on an item is "blockified": an item set to inline behaves like block.
  • ::first-line and ::first-letter do 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:

CSS
.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-content to an item. It belongs on the container.
  • Applying flex-grow to the container. It belongs on the items.
  • Expecting nested elements to flex. Add display: flex to that nested parent as well.
  • Using margin instead of gap. gap is 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: 0 to fix overflow caused by unshrinkable content.
Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Exercise: Equal Columns and a Fixed SidebarHTML

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.

Try it Yourself HTML
Output

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.

Exercise: Fix the Overflowing Flex ItemHTML

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.

Try it Yourself HTML
Output

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?