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

display: flex and Creating a Flex Container

Learn how applying display: flex transforms an element into a flex container and its direct children into flex items.

display: flex and Creating a Flex Container

You'll learn how a single CSS declaration transforms an ordinary element into a flexible layout powerhouse.

The Core Concept

When you apply display: flex to an element, you're performing a kind of layout magic: that element becomes a flex container, and all of its direct children automatically become flex items. Think of it like declaring, "This box is now a flexible organizer, and everything directly inside it becomes an organized item."

Before Flexbox, elements followed normal flow rules—block elements stacked vertically, inline elements flowed horizontally. The moment you add display: flex, those normal flow rules no longer apply to the container's children. Instead, they follow Flexbox's powerful alignment and distribution system.

The Syntax

.container {
  display: flex;
}

That's it! This one line activates the entire Flexbox layout system for that container.

What Actually Happens

The flex container itself still behaves like a block-level element in the document flow (taking up the full width of its parent). However, its direct children now:

  • Line up along the main axis (horizontal by default)
  • Can be aligned, distributed, and sized using Flexbox properties
  • Gain access to powerful layout capabilities

Important: Direct Children Only

Only the immediate children become flex items. Grandchildren and deeper descendants remain unaffected unless their parent also becomes a flex container.

.parent {
  display: flex; /* This is a flex container */
}

/* .child elements are flex items */
/* .grandchild elements follow normal flow unless .child also uses display: flex */

Key Takeaway: display: flex transforms an element into a flex container, giving you control over how its direct children are laid out, aligned, and distributed—it's the essential first step to using Flexbox.