Flexbox Fundamentals

Beginner · 21 min

What display - flex actually changes, how the main axis and cross axis work, and why items behave the way they do before you set a single other property.

Before thisDisplay and Normal Flow

Coding2GO · 21 min

Flexbox Fundamentals

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Coding2GOReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `display: flex` on a parent turns its direct children into flex items laid out along one axis. Only direct children are affected, never grandchildren.
  • Every flex container has a main axis (the direction items flow, set by `flex-direction`) and a cross axis perpendicular to it. `justify-content` works on the main axis and `align-items` on the cross axis.
  • With no other properties, items sit in a row at their content width, start at the left, and stretch to the height of the tallest item. These are defaults, not magic: `flex-direction: row`, `justify-content: flex-start`, `align-items: stretch`.
  • Flex items are not laid out as block or inline boxes. Their margins don't collapse, `float` and `vertical-align` don't apply, and a child's `display` is blockified.
  • `flex-wrap: wrap` lets items drop to a new line when they run out of room. Without it, a flex container stays on one line and shrinks or overflows its items instead.

The problem flexbox solves

Block layout stacks things vertically and inline layout flows text. What was missing was “put these items in a row, line them up, and share out the leftover space”. For years that took floats, inline-block tricks with whitespace gaps, and table hacks. Vertical centering was a running joke.

Flexbox makes that one job simple. You declare a parent as a flex container and the browser takes over laying out its children along a line. Think of a shelf: the shelf is the container, the items on it are flex items, and you describe how the books are spaced, rather than computing each one’s position.

.toolbar { display: flex; }

That single line already does a lot. The children now sit side by side instead of stacking, each at the width of its content, and they all stretch to the same height.

Two axes

Everything in flexbox is described relative to an axis, not to left, right, top or bottom.

  • The main axis is the direction items flow. With the default flex-direction: row it runs left to right (in a left-to-right language). column makes it run top to bottom.
  • The cross axis is perpendicular to it.

Two properties then do the aligning:

  • justify-content positions items along the main axis.
  • align-items positions them along the cross axis.
.row    { display: flex; justify-content: space-between; align-items: center; }
.column { display: flex; flex-direction: column; justify-content: center; align-items: center; }

Change flex-direction and the roles of the two properties rotate with it. This is the most common source of confusion, and the fix is always the same: ask which way the main axis points now. row-reverse and column-reverse flip the start and end, which also flips what flex-start means.

What you get before setting anything

The defaults are worth memorising because every other property is a change from them:

.container {
  display: flex;
  flex-direction: row;       /* main axis left to right */
  flex-wrap: nowrap;         /* stay on one line */
  justify-content: flex-start;
  align-items: stretch;      /* fill the cross axis */
}

align-items: stretch is why equal-height columns are free. In a Chromium test, a flex row with one 100px-tall item and one item with no height set rendered the second item at 100px tall. Setting an explicit height on an item opts it out.

Flex items are a different kind of box

A child of a flex container is a flex item, and that changes its rules:

  • Its display is blockified, so a <span> child acts like a block.
  • Margins don’t collapse with neighbors. Two 20px margins between items give 40px, not 20px.
  • float, clear and vertical-align have no effect on it.
  • Every flex item is sized by flexbox’s own rules instead of the block-level “fill the width” rule. By default an item is as wide as its content needs to be.

Whether the container itself is block-level or inline-level depends on display: flex versus display: inline-flex. Use inline-flex when the container should sit within a line of text, like a button with an icon.

Wrapping

By default a flex container is single-line (flex-wrap: nowrap). When items don’t fit, they shrink, and when they can’t shrink further they overflow. Setting flex-wrap: wrap lets them flow onto a new line instead:

.tags { display: flex; flex-wrap: wrap; gap: 0.5rem; }

Each wrapped line is its own mini flex line and gets distributed separately. That is the reason the next lessons talk about “lines” and a property called align-content. The shorthand flex-flow: row wrap combines direction and wrap.

What the video covers

Coding2GO’s “Learn CSS Flexbox in 20 Minutes (Course)” was published in August 2024 and runs about twenty minutes. Its chapters are: What is Flexbox, How to Center a Div (two sections), Flex Direction, Other Flexbox Properties, FlexGrow, Flex Shrink, AlignSelf, and a Summary. Its description also mentions flex-wrap and the difference between align-items and align-content. I could not read a transcript, so I do not describe the demonstrations. The default-values breakdown and the flex-item box rules above are the prose’s own. The next lesson goes deeper on grow, shrink and basis than the video does.

Where this fits

A flex container changes how its children are laid out, which makes it a change to display and normal flow. The sizing rules start in the next lesson, flex-grow, flex-shrink and flex-basis. For a broader first look that also covers grid, see the frontend roadmap’s flexbox and grid layout lesson.

Check yourself

  1. 01You put `display: flex` on a `<ul>`. Which elements become flex items?

    Only the `<li>` elements, the direct children. Anything inside an `<li>` stays in normal flow unless that element is itself made a flex or grid container.

  2. 02In `flex-direction: column`, which property centers items horizontally?

    `align-items: center`. Setting `flex-direction: column` turns the main axis vertical and the cross axis horizontal, so `justify-content` now controls vertical placement and `align-items` controls horizontal.

  3. 03Three boxes of different heights sit in a flex row and they all appear the same height, even though you never set one. Why?

    The default `align-items` is `stretch`. Items without an explicit height are stretched on the cross axis to fill the container's line, which is the height of the tallest one.

  4. 04What happens to items in a 300px-wide `display: flex` container if there are five 100px-wide items and no `flex-wrap`?

    They stay on one line. Items can shrink below their set width because `flex-shrink` defaults to 1, so they compress to fit. They stop shrinking at their minimum content size, and if that is still too wide, they overflow the container.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark