Nearby lessons

2 of 15

📦 CSS Flex Container

📌 What is a Flex Container?

The flex container is the parent element where the Flexbox layout begins. 🚀 It controls how child items (called flex items) are positioned and spaced.

Using display: flex or display: inline-flex turns any HTML element into a flex container.

This section explains each flex container property with examples to help you master layout building with Flexbox. 🧠💪

📋 What Can Flex Container Do?

  • 📏 Arrange items in rows or columns
  • 🔄 Change item direction and wrapping
  • 📐 Align and justify content easily

1️⃣ display - The Foundation

The display: flex property creates a flex container. This enables flex layout for all direct children.

🔑 Key points:

  • Turns any element into a flex container
  • Affects only direct children (flex items)
  • Removes whitespace between items
AttributeValueDescription
displayflexBlock-level flex container
displayinline-flexInline-level flex container

🧱 display: flex

display: flex creates a block-level flex container (takes full width).

Code Example
PREVIEW READY
Loading Editor...
Live Preview

📏 display: inline-flex

display: inline-flex creates an inline-level flex container (takes only needed width).

Code Example
PREVIEW READY
Loading Editor...
Live Preview

2️⃣ flex-direction - Flow Control

🔄 The flex-direction property establishes the main axis and direction of flex items.

🎯 Visualization tip: Imagine items flowing along this direction!

AttributeValueDescription
flex-directionrow (default)Left to right in ltr; right to left in rtl
flex-directionrow-reverseRight to left in ltr; left to right in rtl
flex-directioncolumnSame as row but top to bottom
flex-directioncolumn-reverseSame as row-reverse but bottom to top

➡️ flex-direction: row

flex-direction: row (default) - Items flow left to right.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

⬅️ flex-direction: row-reverse

flex-direction: row-reverse - Items flow right to left.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

⬇️ flex-direction: column

flex-direction: column - Items flow top to bottom.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

⬆️ flex-direction: column-reverse

flex-direction: column-reverse - Items flow bottom to top.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

3️⃣ flex-wrap - Multi-line Magic

🔄 The flex-wrap property controls whether flex items are forced onto one line or can wrap.

📱 Responsive tip: Essential for creating flexible layouts on different screen sizes!

AttributeValueDescription
flex-wrapnowrap (default)All items on single line (may overflow)
flex-wrapwrapItems wrap to multiple lines top to bottom
flex-wrapwrap-reverseItems wrap to multiple lines bottom to top

🚫 flex-wrap: nowrap

flex-wrap: nowrap (default) - All items stay on one line (may overflow).

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🔀 flex-wrap: wrap

flex-wrap: wrap - Items wrap to multiple lines when needed.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🔄 flex-wrap: wrap-reverse

flex-wrap: wrap-reverse - Items wrap in reverse (bottom to top).

Code Example
PREVIEW READY
Loading Editor...
Live Preview

4️⃣ flex-flow - Direction + Wrap

🎯 The flex-flow shorthand combines flex-direction and flex-wrap.

💡 Syntax: flex-flow: <flex-direction> <flex-wrap>

AttributeValueDescription
flex-flowrow nowrap (default)Same as flex-direction: row + flex-wrap: nowrap
flex-flowcolumn wrapColumn direction with wrapping
flex-flowrow-reverse wrap-reverseReversed row with reverse wrapping

🔀 flex-flow: row wrap

flex-flow: row wrap - Row direction with wrapping.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

📦 flex-flow: column wrap

flex-flow: column wrap - Column direction with wrapping.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

5️⃣ justify-content - Main Axis Alignment

↔️ The justify-content property aligns items along the main axis.

🌈 Visualization: Imagine pushing items along the primary direction!

AttributeValueDescription
justify-contentflex-start (default)Items packed toward start of line
justify-contentflex-endItems packed toward end of line
justify-contentcenterItems centered along the line
justify-contentspace-betweenEqual space between items
justify-contentspace-aroundEqual space around items
justify-contentspace-evenlyEqual space around and between items

👈 justify-content: flex-start

justify-content: flex-start (default) - Items align to start.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

👉 justify-content: flex-end

justify-content: flex-end - Items align to end.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🎯 justify-content: center

justify-content: center - Items center along main axis.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

↔️ justify-content: space-between

justify-content: space-between - Equal space between items.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

⏹️ justify-content: space-around

justify-content: space-around - Equal space around items.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

⚖️ justify-content: space-evenly

justify-content: space-evenly - Equal space around and between items.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

6️⃣ align-items - Cross Axis Alignment

↕️ The align-items property defines how items are laid out along the cross axis.

🔄 Remember: Cross axis is perpendicular to main axis!

AttributeValueDescription
align-itemsstretch (default)Items stretch to fill container
align-itemsflex-startItems align to cross start
align-itemsflex-endItems align to cross end
align-itemscenterItems center along cross axis
align-itemsbaselineItems align by their baselines

📏 align-items: stretch

align-items: stretch (default) - Items stretch to fill container.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

👆 align-items: flex-start

align-items: flex-start - Items align to cross start.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

👇 align-items: flex-end

align-items: flex-end - Items align to cross end.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🎯 align-items: center

align-items: center - Items center along cross axis.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

📐 align-items: baseline

align-items: baseline - Items align by text baselines.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

7️⃣ align-content - Multi-line Alignment

📦 The align-content property aligns flex container's lines when there is extra space.

⚠️ Note: Only works with flex-wrap: wrap and multiple lines!

AttributeValueDescription
align-contentstretch (default)Lines stretch to take remaining space
align-contentflex-startLines packed to container start
align-contentflex-endLines packed to container end
align-contentcenterLines packed to container center
align-contentspace-betweenEqual space between lines
align-contentspace-aroundEqual space around lines

📏 align-content: stretch (default)

align-content: stretch stretches lines of items to fill the container's cross-axis space when there are multiple lines.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

👆 align-content: flex-start

align-content: flex-start packs lines to the start of the cross-axis.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

👇 align-content: flex-end

align-content: flex-end packs lines to the end of the cross-axis.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🎯 align-content: center

align-content: center centers the lines within the container's cross-axis space.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

↔️ align-content: space-between

align-content: space-between places equal space between lines, with no space at the edges.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

⏹️ align-content: space-around

align-content: space-around distributes lines with equal space around them, leaving half-space at the edges.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!CSS - Flexbox Item