</>HTML & CSS
IntermediateLesson 9 of 15

Flexbox – One-Dimensional Layouts – HTML & CSS

Rows and columns with flexible items, alignment and spacing.

With display: flex an element becomes a flex container and its children flex items. The main axis is set by flex-direction (row is the default, column). Along it, justify-content distributes the space; on the cross axis, align-items aligns. gap sets spacing between the items – without margin hacks.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.leiste { display: flex; gap: 10px; justify-content: space-between; align-items: center;
          background: #eef; padding: 10px; height: 80px; }
.leiste div { background: royalblue; color: #fff; padding: 10px 14px; border-radius: 6px; }
.leiste .gross { padding: 24px 14px; }
</style>
<div class="leiste">
  <div>One</div><div class="gross">Two (taller)</div><div>Three</div>
</div>

Size distribution is controlled by the shorthand flex: <grow> <shrink> <basis>. flex: 1 (= 1 1 0%) lets items grow evenly; flex: 0 0 200px enforces a fixed width. With flex-wrap: wrap, items break into new lines; margin-left: auto pushes an item to the end.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.nav { display: flex; align-items: center; gap: 14px; background: #222; color: #fff; padding: 10px 16px; }
.nav .logo { font-weight: 800; }
.nav .push { margin-left: auto; }          /* pushes everything that follows to the right */
.reihe { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.reihe span { flex: 1 1 120px; background: #cde; padding: 10px; text-align: center; }
</style>
<div class="nav"><span class="logo">LOGO</span><a href="#a" style="color:#fff">Home</a>
  <a href="#b" style="color:#fff">Pricing</a><button class="push">Login</button></div>
<div class="reihe"><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span></div>

Perfect centering in two lines: display: flex; justify-content: center; align-items: center; – horizontally and vertically. For column layouts (e.g. pushing the footer to the bottom) you use flex-direction: column and let the main area grow with flex: 1.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.seite { display: flex; flex-direction: column; min-height: 220px; border: 2px solid #999; }
.seite header, .seite footer { background: #333; color: #fff; padding: 8px; }
.seite main { flex: 1; display: flex; justify-content: center; align-items: center; background: #f5f5f5; }
</style>
<div class="seite">
  <header>Header</header>
  <main>Centered in the middle</main>
  <footer>Footer stays at the bottom (sticky footer)</footer>
</div>
WarningFlex items shrink by default (flex-shrink: 1), but not below their minimum content width: long words or images then break the layout. Remedy: min-width: 0 on the item and overflow-wrap: anywhere. Also: align-items acts on the cross axis, justify-content on the main axis – with flex-direction: column the roles are swapped.
TipFlexbox thinks in one dimension (row or column). As soon as you want to control rows and columns at the same time, Grid is the better tool.