Course contentsShow
← Web Development
Lesson 255 of 2,9825. FlexboxFree lesson

The Flex Container and Flex Items

Understanding the parent-child relationship and which properties apply to containers vs items.

The Flex Container and Flex Items

You'll learn: How flexbox creates a parent-child relationship and which CSS properties belong to each role.

Understanding the Two-Part System

When you set display: flex on an element, you're establishing a two-tier system. The element with display: flex becomes the flex container (the parent), and its direct children automatically become flex items (the children).

Think of it like a shipping warehouse and boxes: the warehouse (container) controls the overall layout strategy, while each individual box (item) can have its own specific instructions about how it should behave within that warehouse.

Properties for the Container

The flex container controls the overall layout direction and spacing. Properties you'll apply to the container include things like:

  • How items should align as a group
  • Whether items should wrap to new lines
  • The direction items flow (row or column)

These go on the parent element that has display: flex.

Properties for the Items

Flex items control their individual behavior within the container. Each child can specify things like:

  • How much it should grow or shrink
  • Its own alignment override
  • Its display order

These properties go on the direct children of the flex container.

The Critical Rule

Only direct children become flex items. If you have nested elements, only the immediate children of the flex container participate in the flex layout. Their children follow normal flow unless they also become flex containers.

Key Takeaway: Flexbox splits responsibilities—the container (parent with display: flex) manages overall layout strategy, while flex items (direct children) control their individual behavior. Always apply flex properties to the correct level.