CSS FLEXBOX /// DISPLAY: FLEX /// JUSTIFY-CONTENT /// ALIGN-ITEMS /// CSS FLEXBOX /// DISPLAY: FLEX /// JUSTIFY-CONTENT /// ALIGN-ITEMS ///

CSS Flexbox Containers

Master layout in a single dimension. Align, distribute, and perfectly center UI elements with the ultimate modern CSS tool.

flexbox.css
1 / 15
12345
📦

Tutor:Before Flexbox, centering an element was a nightmare. Flexbox changed everything by giving us a powerful, 1-dimensional layout model.


Layout Matrix

UNLOCK NODES BY MASTERING ALIGNMENT.

Concept: Display Flex

The foundation of flexbox. Applying display: flex to a container instantly transforms its direct children into flex items, arranging them in a row by default.

System Check

What happens immediately to direct children when a container is set to `display: flex`?


Community Holo-Net

Showcase Your Layouts

ACTIVE

Built a complex responsive grid using just Flexbox? Share your codepens and get feedback!

CSS Flexbox: Mastering the Container

Frontend Instructor in Code syllabus

Pascual Vila

Frontend Instructor // Code Syllabus

"Flexbox 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 Core Concept: Two Axes

The most important concept to grasp about Flexbox is that it works on a two-axis system: the Main Axis and the Cross Axis.

When you set display: flex; on a container, the direct children become flex items. By default, the Main Axis flows horizontally (left-to-right), and the Cross Axis flows vertically (top-to-bottom).

Main Axis: Justify-Content

To align items along the Main Axis, we use justify-content. This property determines how the browser distributes extra space along the primary direction of the flex container.

  • center: Packs items around the center.
  • space-between: Distributes items evenly; the first item is flush with the start, the last is flush with the end.
  • space-around: Distributes items evenly with equal space around them.

Cross Axis: Align-Items

To align items perpendicular to the Main Axis, we use align-items. This is what allows for perfect vertical centering. Note that by default, align-items is set to stretch, which is why flex items automatically expand to fill the container's height!

Wrapping and Gap

Flexbox tries to force all items onto a single line. When items overflow, you can allow them to wrap onto multiple lines using flex-wrap: wrap;.

Finally, ditch the awkward margins. The modern way to add spacing between flex items is the gap property, which intelligently applies space strictly between items, never around the edges of the container.

Frequently Asked Questions

What is the difference between Flexbox and CSS Grid?

Flexbox is one-dimensional. It is designed to lay out elements in a single row (main axis) or a single column. It is ideal for aligning UI components.

CSS Grid is two-dimensional. It handles rows and columns simultaneously, making it better for full-page layouts and complex structures.

How do I perfectly center a div using Flexbox?

This is the most famous use case. Apply these three lines to the parent container (and ensure the container has height):

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* Example */
}
Why use `gap` instead of `margin`?

margin often adds unwanted extra space at the end or beginning of a row, requiring pseudo-selectors like :last-child { margin-right: 0 }. Using gap eliminates this problem by inserting space strictly between elements.

Flex Container Glossary

display: flex
Defines a flex container; enables a flex context for all its direct children.
snippet.css
flex-direction
Establishes the main-axis, defining the direction flex items are placed in the flex container.
snippet.css
justify-content
Defines the alignment along the main axis. It helps distribute extra free space leftover.
snippet.css
align-items
Defines the default behavior for how flex items are laid out along the cross axis.
snippet.css
flex-wrap
By default, flex items try to fit onto one line. This changes that and allows items to wrap as needed.
snippet.css
gap
Controls the space between flex items explicitly, acting as a gutter.
snippet.css