Mastering the Flex Container

Discover the fundamental CSS properties that give you full control over flexible layouts, alignment, and wrapping.

Lesson ProgressStep 1 of 10
1
2
3
0 EXP

Welcome! Let's learn CSS Flexbox. By default, these are block elements, stacked vertically.

The Flex Container: `display: flex`

To begin using Flexbox, you must first create a "flex container." You do this by setting the `display` property of an element to `flex`.

.container {
  display: flex;
}

As soon as you do this, the direct children of that element become "flex items." By default, they will all try to line up in a single horizontal row.

There is also `display: inline-flex`, which makes the container itself behave like an inline element (flowing with text) while still formatting its children as flex items.

System Check

When you apply `display: flex` to a container, what is the default `flex-direction`?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Flex Initiate

Use `display: flex` and `flex-direction` to control layout.

🏗️
Alignment Pro

Correctly align items with `justify-content` and `align-items`.

✍️
Responsive Wrapper

Use `flex-wrap` to create a responsive, multi-line layout.

Mission: The Holy Grail of Centering

Modify the `.container` rule to center its children both horizontally and vertically. Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Properties

Drag the CSS properties into the most logical order to create a responsive, centered container.

.container {

justify-content: center;
display: flex;
flex-wrap: wrap;

}

Challenge: Complete the Syntax

Fill in the missing parts of the properties to stack items and align them to the end.

.container {display: flex;
flex-: column;
-content: flex-end;
}

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Holy Grail Centering" project for feedback from other Net-Runners.

Beyond the Basics: Real-World Flexbox Layouts

When you first start with CSS, layout can feel like a battle. You use floats, `inline-block` hacks, and `position: absolute` tricks to wrangle elements into place. **Flexbox** (or the Flexible Box Module) was created to solve these problems. It provides a more efficient way to lay out, align, and distribute space among items in a container, even when their size is unknown or dynamic.

The "Holy Grail" of Centering

For decades, perfectly centering an element both horizontally and vertically was a common CSS challenge. With Flexbox, it's three lines of code. This is invaluable for modals, popups, and hero content.

✔️ Good Practice

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
}

Simple, predictable, and clean.

❌ Old "Hack"

.parent {
  position: relative;
  height: 300px;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Complex, relies on `position`, and can cause layout issues.

Main Axis vs. Cross Axis: A Mental Model

The most confusing part of Flexbox is the concept of "axes." The key is that **`flex-direction` determines the axes.**

  • `flex-direction: row` (default):
    • The **Main Axis** is horizontal (left-to-right).
    • `justify-content` controls horizontal alignment.
    • The **Cross Axis** is vertical (top-to-bottom).
    • `align-items` controls vertical alignment.
  • `flex-direction: column`:
    • The **Main Axis** becomes vertical (top-to-bottom).
    • `justify-content` now controls vertical alignment.
    • The **Cross Axis** becomes horizontal (left-to-right).
    • `align-items` now controls horizontal alignment.

Remembering this "axis swap" is the key to mastering `justify-content` and `align-items`.

Building Responsive Grids with `flex-wrap`

Flexbox can create responsive grids without any media queries. By setting `flex-wrap: wrap` on the container, you allow items to move to the next line when they run out of space.

If you then give the *items* a `flex-grow: 1` and a `flex-basis` (or `min-width`), they will automatically stretch to fill the available space and then "wrap" to the next line when the container gets too narrow. This creates a powerful, fluid grid system.

.container {
  display: flex;
  flex-wrap: wrap;
}
.item {
  flex-grow: 1;
  flex-basis: 250px; /* Each item wants to be 250px */
  /* They will grow to fill space, but wrap if < 250px */
}
Key Takeaway: Always ask: "Which way is my main axis?" `flex-direction` sets this. `justify-content` controls spacing *along* that axis. `align-items` controls alignment *across* that axis.

CSS Flexbox Glossary

Flex Container
The parent element on which `display: flex` or `display: inline-flex` is applied. It creates the flexbox layout context for its direct children.
Flex Item
A direct child of a flex container. These are the elements that get laid out using the flexbox rules.
Main Axis
The primary axis along which flex items are laid out. It is defined by the `flex-direction` property. If `flex-direction` is `row`, the main axis is horizontal. If it's `column`, the main axis is vertical.
Cross Axis
The axis perpendicular to the main axis. If the main axis is horizontal, the cross axis is vertical, and vice versa.
`flex-direction`
A container property that establishes the main axis. Values include `row` (default), `row-reverse`, `column`, and `column-reverse`.
`justify-content`
A container property that aligns flex items along the **main axis**. It controls the distribution of space. Common values are `flex-start`, `flex-end`, `center`, `space-between`, `space-around`, and `space-evenly`.
`align-items`
A container property that aligns flex items along the **cross axis**. Common values are `stretch`, `flex-start`, `flex-end`, `center`, and `baseline`.
`flex-wrap`
A container property that allows flex items to wrap onto multiple lines if they run out of space on one line. Values are `nowrap` (default), `wrap`, and `wrap-reverse`.
`align-content`
A container property that aligns the *lines* of flex items within the container when there is extra space on the cross axis. It **only works if `flex-wrap` is set and there are multiple lines**.
`flex-grow`
An item property that defines the ability for a flex item to grow if necessary. It accepts a unitless value (e.g., `1`) that serves as a proportion.
`flex-shrink`
An item property that defines the ability for a flex item to shrink if necessary. The default value is `1`.
`flex-basis`
An item property that defines the default size of an item before the remaining space is distributed. It's similar to `width` (in a `row` direction) but more flexible.
`flex`
A shorthand item property for `flex-grow`, `flex-shrink`, and `flex-basis`. The default is `0 1 auto`. A common value is `flex: 1` (short for `1 1 0%`) to make items grow and shrink equally from a zero basis.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS Flexible Box Layout Module Level 1 specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!