Lesson 16 of 23

In progress

Course progress

HTML and CSS

Flexbox for rows and columns

Lay out navigation, cards and centred content with Flexbox, and understand the main axis instead of memorising properties.

Flexbox is for distributing items along one line: a row of navigation links, a card with an icon beside text, a footer with a logo on the left and links on the right. Once you know which axis you are on, most of it follows.

What you will learn

  • Turn a container into a flex row or column.
  • Align and distribute items on both axes.
  • Build common patterns without margins or floats.

One axis at a time

HTML
Editable. Runs in your browser.





Press Run to see the output.

display: flex makes the children lay out in a row. The main axis runs horizontally; the cross axis runs vertically. gap spaces the items without margins.

flex-direction: column swaps the axes: main becomes vertical and items stack. Everything else keeps working with the same property names, which is why the axis idea matters more than a list of values.

Aligning on the cross axis

align-items positions items across the cross axis: stretch (the default), flex-start, center, flex-end, baseline.

HTML
Editable. Runs in your browser.





Press Run to see the output.

flex: none on the avatar stops it shrinking when the text is long. Without it, a narrow screen squeezes the circle into an oval, which is a common and confusing bug.

Justifying along the main axis

justify-content distributes items along the main axis: flex-start, center, flex-end, space-between, space-around, space-evenly.

HTML
Editable. Runs in your browser.





Press Run to see the output.

The outer flex container pushes the brand and the navigation apart. The navigation is itself a flex container, which is normal: small components compose.

align-self overrides align-items for one item, which is how you make a single button sit at the top of an otherwise centred row. Use it sparingly; a layout where every item aligns differently is usually two containers pretending to be one.

The order property changes visual order without changing source order. Avoid it for anything focusable: keyboard order follows the DOM, so a reordered button receives focus in a place that does not match what the user sees. Fix the markup instead.

margin-left: auto on a flex item also pushes it to the end, and it is useful when only one item should move.

Growing, shrinking and wrapping

flex-grow lets an item take available space, flex-shrink lets it give space up, and flex-basis sets its starting size. The shorthand is flex: <grow> <shrink> <basis>.

HTML
Editable. Runs in your browser.





Press Run to see the output.

flex: 1 1 220px means each box starts at 220 pixels, can grow to fill the row, and can shrink. With flex-wrap: wrap, a narrow screen moves the third box to a new line instead of crushing all three.

A common shorthand is flex: 1, which means grow and shrink from a zero basis. It makes items equal width regardless of content, which is right for equal cards and wrong for a logo.

Centring, the short way

HTML
Editable. Runs in your browser.





Press Run to see the output.

Two declarations replace the old absolute-positioning tricks. Use min-height rather than height so the content can grow.

A flex item refuses to shrink below its content’s minimum width by default, so a long unbroken string or a wide table can overflow the container. Set min-width: 0 on the item, or overflow-wrap: anywhere on the text, instead of adding overflow: hidden and hiding the problem.

Build a card row where each card has a title, a paragraph and a footer link pinned to the bottom, with equal heights. Use Flexbox for both the row and the card’s internal column.

Show solution
HTML
Editable. Runs in your browser.





Press Run to see the output.

The row uses wrap for small screens. Each card is a column, and margin-top: auto absorbs the leftover space so the links line up at the bottom.

Recap

  • Flexbox lays out one axis; change flex-direction to swap the axes.
  • justify-content works on the main axis, align-items on the cross axis.
  • flex: 1 1 220px with flex-wrap gives responsive rows without media queries.

Next, you will choose units that behave on phones, in zoomed browsers and on large monitors.

When should I use Flexbox instead of Grid?
Use Flexbox when items flow along one line and their size depends on content. Use Grid when rows and columns both need to align. Many layouts use both, nested.
Why is my flex item not taking the full height?
The container’s height is determined by its content unless you set a height or min-height. `align-items: stretch` only stretches to the container’s height, not the viewport’s.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.