Enabling Flexbox with display: flex
You'll learn how a single CSS property turns an ordinary element into a powerful Flexbox container.
The Magic Switch
Remember how we learned what Flexbox is in the previous lesson? Now it's time to actually turn it on. The moment you add display: flex to any element, that element becomes a flex container, and all of its direct children automatically become flex items. It's like flipping a switch that transforms how the browser arranges those children.
Think of it like organizing a shelf. Normally (in normal flow), items stack vertically like books in a pile. But when you apply display: flex, you're telling the browser: "Treat this shelf as a flexible organizer, and arrange the items on it intelligently."
How It Works
.container {
display: flex;
}
That's it! This single line does two things:
- Creates a flex container — the element with
display: flexbecomes the parent coordinator - Transforms direct children — only the immediate children become flex items (not grandchildren or deeper descendants)
<div class="container">
<div>Item 1</div> <!-- Flex item -->
<div>Item 2</div> <!-- Flex item -->
<div>Item 3</div> <!-- Flex item -->
</div>
Without display: flex, those divs would stack vertically (block-level behavior). With display: flex, they line up horizontally by default, ready to be controlled by Flexbox properties.
Important: Direct Children Only
Only the immediate children become flex items. If a flex item has children of its own, those grandchildren remain in normal flow unless you make their parent a flex container too.
Key Takeaway: display: flex is the single property that activates Flexbox, turning an element into a flex container and its direct children into flexible, controllable flex items.