FLEXBOX / LESSON 01

Keep nested axes and reading order straight

Reason about independent flex containers and visual order without repeating the basic row-and-column setup.

Content updated

By the end, you can…

  • Each flex container owns its axes. Changing the outer direction does not change a nested container's direction.
  • Build a card whose action buttons stay in a row at the bottom, then align those buttons to the end without reversing their order.
Before you begin

Build on the CSS introduction

Start from the CSS Flexbox introduction, where you learned to create a row or column. Now inspect a card with two levels: an outer column holding a summary and an actions group, and an inner row holding two buttons. The actions group is one item in the outer layout, while its buttons are items in a separate inner layout. Trace each relationship before choosing an alignment rule.

The outer container's main axis is vertical, so its justify-content distributes spare height between the summary and actions. The inner row's main axis is horizontal, so its own justify-content moves the buttons toward the end of their row. These declarations can have different visible directions at the same time. A nested container does not inherit flex-direction from its parent.

Visual order is another independent concern. Reversing the actions row moves the buttons but does not rewrite their DOM order. Try Tab inside the reversed preview: Save still comes before Share even if it appears on the right. Use logical source order for a real workflow. The comparison is a demonstration of the mismatch, not a recommendation to reverse controls.

What to remember first

Remember first

Each flex container owns its axes. Changing the outer direction does not change a nested container's direction.

Tools for this experiment

.actionsA container and an item
It is an item of the outer column and a flex container for the buttons inside it.
justify-contentRead it in the current container
The outer declaration uses spare height; the inner declaration uses spare width in this example.
row-reverseChange visual direction only
Tab through the buttons to compare keyboard order with visual position. Their click actions are deliberately not implemented.

Walk through an example

Use this HTML inside the body of a document and place the CSS in your linked stylesheet. The preview adds a dashed outline and card colors to make boundaries visible; those decorations do not control the layout. Its body has 16px padding on each side. Fixed preview widths are intentional: on a small screen, scroll inside the preview to inspect the whole experiment.

html
<div class="layout">
  <div class="item">Article summary</div>
  <div class="actions">
    <button type="button">Save</button>
    <button type="button">Share</button>
  </div>
</div>
css
.layout {
  display: flex;
  flex-direction: column;
  min-height: 220px;
  justify-content: space-between;
  gap: 16px;
}
.item {
  padding: 12px;
}
.actions {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  gap: 8px;
}

The outer column separates summary and actions vertically; the inner row aligns Save and Share horizontally.

See it happen: try a small change

Two containers, two independent main axes

Compare the outer spacing, then Tab through the reversed buttons. The buttons demonstrate focus order; they do not save or share anything.

Code to notice

.layout {
  display: flex;
  flex-direction: column;
  min-height: 220px;
  justify-content: space-between;
  gap: 16px;
}
.item {
  padding: 12px;
}
.actions {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  gap: 8px;
}

Live result

Lesson example
index.htmlPreview

What changed: The outer column separates summary and actions vertically; the inner row aligns Save and Share horizontally.

A common wrong turn

Applying the outer column's alignment rule to each button mixes two different containers. When debugging, inspect the button's immediate parent first. A layout that looks correct with a mouse can still be confusing if reversed visual order disagrees with keyboard order.

Try it in your own project

Copy the example into your local HTML and CSS files. Predict one change before saving and refreshing the browser. Use the interactive versions above to check your prediction, then explain which container, item, or track changed. The related playground offers extra practice with its supported controls; use your local files for properties it does not expose.

Your turn: keep nested axes and reading order straight

Build a card whose action buttons stay in a row at the bottom, then align those buttons to the end without reversing their order.

Optional exploration

Use the playground to explore the controls and generated code. It is an open experiment, so it does not check your answer.

Open Flexbox playground →

  1. Use the displayed HTML and CSS in your own files. Identify the two direct children of the outer container.
  2. Change only the outer justify-content to flex-start. Restore space-between and explain why the inner buttons remain in a row.
  3. Compare the reversed demo, then keep row in your own version. Tab from Save to Share and confirm the visual and keyboard order agree.

Go to the source

Examples are authored for Tuklas and checked with code and reference documentation. This is not a claim of independent human review. Suggest a correction.