Reading history

CSS Flexbox Tutorial: justify-content, align-items, flex

4 min read 0 views

Learn CSS Flexbox: flex containers, main and cross axes, justify-content, align-items, gap, flex: 1, wrapping and column layouts.

Flexbox is the CSS layout system for arranging items in a single row or column. It makes jobs that used to take hacks simple: putting a logo on the left and a menu on the right, centring something vertically, or making cards share the available space and wrap on small screens. You switch it on with one line, display: flex, on the parent element.

In this lesson you will learn

  • Flex containers and flex items
  • The main axis and cross axis
  • Spacing with justify-content, align-items and gap
  • How flex: 1 shares space
  • Wrapping and column layouts

Containers, Alignment and Centring

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Flexbox</title>
  <style>
    .row { display: flex; gap: 8px; background: #eceff1; padding: 8px; margin-bottom: 8px; }
    .row div { background: #00695c; color: white; padding: 8px 12px; border-radius: 6px; }
    .between { justify-content: space-between; }
    .center  { justify-content: center; align-items: center; height: 70px; }
  </style>
</head>
<body>
  <div class="row"><div>1</div><div>2</div><div>3</div></div>
  <div class="row between"><div>Logo</div><div>Menu</div></div>
  <div class="row center"><div>Centred both ways</div></div>
</body>
</html>

Result

  • display: flex makes an element a flex container; its direct children become flex items and line up in a row.
  • The main axis runs along the row. justify-content spaces items along it: flex-start, center, space-between, space-around, space-evenly.
  • The cross axis runs across it. align-items positions items on it: stretch (the default), center, flex-start, flex-end.
  • justify-content: center plus align-items: center centres content both ways.
  • gap adds space between items without margins.

Growing, Wrapping and Columns

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Flex grow and wrap</title>
  <style>
    .row { display: flex; gap: 8px; background: #eceff1; padding: 8px; margin-bottom: 8px; }
    .row div { background: #6a1b9a; color: white; padding: 8px; border-radius: 6px; }
    .grow { flex: 1; }                       /* takes the spare space */
    .wrap { flex-wrap: wrap; }
    .wrap div { flex: 1 1 120px; }           /* at least 120px, then wrap */
    .col  { flex-direction: column; }
  </style>
</head>
<body>
  <div class="row"><div>Sidebar</div><div class="grow">flex: 1 fills the rest</div></div>
  <div class="row wrap"><div>Card A</div><div>Card B</div><div>Card C</div><div>Card D</div><div>Card E</div></div>
  <div class="row col"><div>Column 1</div><div>Column 2</div></div>
</body>
</html>

Result

  • flex: 1 tells an item to take all the spare space; the sidebar keeps its natural width.
  • flex: 1 1 120px means grow, shrink, and start from a base width of 120px.
  • flex-wrap: wrap moves items to a new line when they no longer fit, which gives a responsive card layout without media queries. Narrow your window to see the cards wrap.
  • flex-direction: column turns the main axis vertical, stacking items.

Flexbox Cheat Sheet

On the containerOn the items
display: flexflex: grow shrink basis
flex-direction: row | columnalign-self (override align-items for one item)
justify-contentorder (change visual order)
align-itemsmargin-left: auto (push an item to the end)
flex-wrap, gap

Common Mistakes

  1. Putting display: flex on the items instead of their parent.
  2. Mixing up the axes: after flex-direction: column, justify-content works vertically.
  3. Using order to rearrange content, which confuses keyboard and screen-reader users because they follow the HTML order.
  4. Expecting two-dimensional control. For rows and columns at once, use CSS Grid.

Practice Exercises

Exercise 1. Build a navigation bar with the logo on the left and links on the right, vertically centred.

Show answer
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Exercise 2. Make three equal-width columns that share a row.

Show answer
.row { display: flex; gap: 16px; }
.row > div { flex: 1; }

Frequently Asked Questions

When should I use Flexbox instead of Grid?

Use Flexbox for one-dimensional layouts such as toolbars, menus and rows of buttons. Use Grid when you need rows and columns together, such as a whole page layout or a photo gallery.

How do I centre a div in CSS?

Give its parent display: flex; justify-content: center; align-items: center; and a height.

Summary

  • display: flex on the parent lines up its children.
  • justify-content works along the main axis, align-items across it.
  • flex: 1 shares space; flex-wrap makes rows responsive.
  • Flexbox handles one direction; Grid handles two.

Next: CSS Grid.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *