Aral Code

Layout Lesson 19 of 33

CSS Flexbox — The Flex Container

Learn Flexbox, the easiest way to line things up in a row or column — display flex, flex-direction, justify-content, align-items, gap and flex-wrap.

Flexbox is a layout system for arranging items in a single row or column. It makes things that used to be frustrating — putting items side by side, spacing them evenly, centering something vertically — take just one or two lines of CSS. It's one of the most useful things you'll learn in this course.

The container and its items

Flexbox always involves two levels:

  • The flex container — the parent, which gets display: flex
  • The flex items — its direct children, which get arranged automatically
Example display: flex
<style>
  .row { display: flex; background: #f3f6f4; padding: 8px; border: 2px dashed #0f5c4d; }
  .item { background: #0f5c4d; color: white; padding: 14px 18px; margin: 4px; border-radius: 8px; font-family: Arial, sans-serif; }
</style>
<div class="row">
  <div class="item">Adobo</div>
  <div class="item">Sinigang</div>
  <div class="item">Kare-kare</div>
</div>

Just by adding display: flex to the parent, the three divs (normally stacked as blocks) line up side by side. Remove that line to see the difference.

flex-direction: row or column

flex-direction sets the main axis, the direction items are laid out in:

  • row — left to right (default)
  • column — top to bottom
  • row-reverse and column-reverse — the opposite order
Example Row vs. column
<style>
  .box { display: flex; gap: 6px; background: #f3f6f4; padding: 8px; margin-bottom: 10px; }
  .box div { background: #2f6fd6; color: white; padding: 10px 14px; border-radius: 6px; font-family: Arial, sans-serif; }
</style>
<div class="box" style="flex-direction: row;"><div>1</div><div>2</div><div>3</div></div>
<div class="box" style="flex-direction: column;"><div>1</div><div>2</div><div>3</div></div>

gap: space between items

gap adds space between items, but not on the outside edges. It's much easier than adding margins to every item:

.row { display: flex; gap: 16px; }

justify-content: spacing on the main axis

justify-content controls how items are spread along the main axis (horizontally, for a row):

Example justify-content values
<style>
  .box { display: flex; background: #f3f6f4; padding: 6px; margin-bottom: 8px; border-radius: 8px; }
  .box div { background: #0f5c4d; color: white; padding: 8px 14px; border-radius: 6px; font: 14px Arial, sans-serif; }
  .label { font: bold 13px monospace; margin: 6px 0 2px; }
</style>
<p class="label">flex-start (default)</p>
<div class="box" style="justify-content: flex-start;"><div>A</div><div>B</div><div>C</div></div>
<p class="label">center</p>
<div class="box" style="justify-content: center;"><div>A</div><div>B</div><div>C</div></div>
<p class="label">flex-end</p>
<div class="box" style="justify-content: flex-end;"><div>A</div><div>B</div><div>C</div></div>
<p class="label">space-between</p>
<div class="box" style="justify-content: space-between;"><div>A</div><div>B</div><div>C</div></div>
<p class="label">space-evenly</p>
<div class="box" style="justify-content: space-evenly;"><div>A</div><div>B</div><div>C</div></div>

space-between is the classic choice for a header: logo on the far left, menu on the far right.

align-items: alignment on the cross axis

align-items aligns items on the other axis (vertically, for a row). The container needs some height for you to see the difference:

Example align-items values
<style>
  .box { display: flex; gap: 6px; height: 90px; background: #f3f6f4; margin-bottom: 8px; border-radius: 8px; padding: 4px; }
  .box div { background: #b4372b; color: white; padding: 6px 12px; border-radius: 6px; font: 14px Arial, sans-serif; }
  .box div:nth-child(2) { font-size: 24px; }
  .label { font: bold 13px monospace; margin: 4px 0 2px; }
</style>
<p class="label">stretch (default)</p>
<div class="box" style="align-items: stretch;"><div>A</div><div>B</div><div>C</div></div>
<p class="label">center</p>
<div class="box" style="align-items: center;"><div>A</div><div>B</div><div>C</div></div>
<p class="label">flex-end</p>
<div class="box" style="align-items: flex-end;"><div>A</div><div>B</div><div>C</div></div>

Perfect centering

For years, centering something both horizontally and vertically was a famous CSS headache. With Flexbox, it's three lines:

Example Center anything
<style>
  .stage {
    display: flex;
    justify-content: center;   /* horizontal */
    align-items: center;       /* vertical */
    height: 200px;
    background: #0f5c4d;
  }
  .card { background: white; padding: 20px 28px; border-radius: 12px; font: bold 20px Arial, sans-serif; }
</style>
<div class="stage"><div class="card">Perfectly centered! 🎯</div></div>

flex-wrap

By default, flex items all squeeze onto one line, even if they get very narrow. flex-wrap: wrap lets them move to the next line when there isn't enough room, which is essential for phones.

Example Wrapping tags
<style>
  .tags { display: flex; flex-wrap: wrap; gap: 8px; max-width: 360px; font-family: Arial, sans-serif; }
  .tags span { background: #e3f1ec; color: #0f5c4d; padding: 6px 12px; border-radius: 999px; }
</style>
<div class="tags">
  <span>#HTML</span><span>#CSS</span><span>#JavaScript</span><span>#Python</span>
  <span>#SQL</span><span>#Java</span><span>#WebDesign</span><span>#ICT</span>
</div>

Practice: a website header

Example Logo left, menu right
<style>
  body { margin: 0; font-family: Arial, sans-serif; }
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 18px;
    background: #0f5c4d;
    color: white;
  }
  .logo { font-weight: bold; font-size: 20px; }
  .menu { display: flex; gap: 16px; list-style: none; margin: 0; padding: 0; }
  .menu a { color: white; text-decoration: none; }
</style>
<header class="header">
  <div class="logo">☕ Kapihan</div>
  <ul class="menu">
    <li><a href="#">Menu</a></li>
    <li><a href="#">Branches</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</header>

There are two flex containers here: the header (logo and menu) and the menu itself (the three links). Nesting flex containers like this is completely normal.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Where do you put display: flex?
2. Which property puts the logo on the far left and the menu on the far right?
3. Which two lines center an item both ways inside a flex container?
4. Items get squeezed too narrow on a phone. Which property lets them move to the next line?

By Sir Reyel · Last updated 2026-12-04