FLEXBOX LAYOUT /// JUSTIFY CONTENT /// ALIGN ITEMS /// DISPLAY FLEX /// FLEXBOX LAYOUT /// JUSTIFY CONTENT /// ALIGN ITEMS ///

CSS Flexbox

Tame complex layouts with ease. Master alignments, distribution, and responsive fluid components using the modern Flexbox module.

flexbox.css
1 / 14
12345
📐

Tutor:Layout on the web used to be a nightmare of floats and clearfixes. Flexbox changed everything by introducing a 1-dimensional layout model.


Skill Matrix

UNLOCK NODES BY MASTERING LAYOUTS.

Concept: Flex Container

Everything begins with display: flex on the parent element, establishing a new formatting context.

System Check

What is the default layout axis when you apply `display: flex`?


Community Holo-Net

Showcase Your Layouts

ACTIVE

Built a crazy responsive dashboard using just Flexbox? Share your codepens and get feedback!

CSS Flexbox: Taming the 1D Layout

Frontend Instructor in Code syllabus

Pascual Vila

Frontend Instructor // Code Syllabus

Before Flexbox, aligning a single div vertically was an interview question designed to weed out the weak. Now, with a few keystrokes, we command the browser to bend layouts to our absolute will.

The Two Axes Paradigm

Flexbox operates on a completely different coordinate system than the standard block model. Instead of top-to-bottom or left-to-right, Flexbox uses a Main Axis and a Cross Axis.

By default, the Main Axis runs horizontally (row), and the Cross Axis runs vertically. If you change flex-direction: column, these axes swap. Understanding this is the crucial first step to mastering Flexbox.

Container Properties vs Item Properties

Flexbox rules are strictly divided. Some properties belong on the Container (the parent), while others go on the Items (the children).

  • Container Properties: display: flex, flex-direction, justify-content, align-items, flex-wrap, gap. These control the macro-layout.
  • Item Properties: flex-grow, flex-shrink, flex-basis, align-self, order. These allow individual children to override the container's rules or behave fluidly.

The Magic of "Justify" and "Align"

justify-content distributes space along the Main Axis. Whether you want items bunched at the start, centered, or evenly spaced out with space-between, this property handles it.

align-items aligns items along the Cross Axis. It's the go-to property for vertical centering in a row layout. If you need a single item to break rank and align differently, you apply align-self directly to that item.

View The Shorthand Tip+

Master the flex shorthand. Instead of writing flex-grow: 1; flex-shrink: 1; flex-basis: 0%;, just write flex: 1; on the item. It's cleaner, easier to read, and prevents default sizing bugs.

Frequently Asked Questions

What is the difference between justify-content and align-items?

justify-content manages alignment along the Main Axis (horizontal by default). It controls how excess space is distributed between items.

align-items manages alignment along the Cross Axis (vertical by default). It defines how items are positioned from top to bottom within their respective flex line.

What does "flex-wrap: wrap" do?

By default, Flexbox will attempt to fit all your items into a single line, even if it means shrinking them beyond their desired size. By usingflex-wrap: wrap, you tell the container: "If you run out of room, push the next item to a new line below." It is essential for responsive design.

Flexbox vs Grid: When to use which?

The general rule: Flexbox is 1-dimensional (useful for rows or columns, aligning content inside components like navbars or buttons). CSS Grid is 2-dimensional (useful for overall page layouts with simultaneous rows and columns). They are often used together: Grid for the main structure and Flexbox within individual cards.

Flexbox Glossary

display: flex
Establishes a flex formatting context for its children, turning them into flex items.
snippet.css
justify-content
Aligns flex items along the main axis of the current line of the flex container.
snippet.css
align-items
Aligns flex items along the cross axis (vertical, if flex-direction is row).
snippet.css
flex-wrap
Specifies whether flex items are forced onto one line or can wrap onto multiple lines.
snippet.css
gap
Controls the space between flex items, acting exactly like margin without targeting first/last children.
snippet.css
flex-grow
Applied to an ITEM. Specifies how much of the remaining space in the container should be assigned to the item.
snippet.css