← Back to HTML & CSS, page 1

html-css-0001 · Medium mid

Explain CSS Flexbox with practical examples.

Flexbox lays out the children of a container along one main axis, with a cross axis running across it. justify-content spreads items on the main axis, align-items aligns them on the cross axis, and the flex shorthand sets grow, shrink and basis for each item. It suits navigation bars, toolbars, card rows and sidebar layouts.

Full answer

Flexbox lays out the children of a container along one axis. The main axis follows flex-direction (a row by default) and the cross axis runs across it.

On the container:

  • display: flex turns it on.
  • flex-direction picks row or column.
  • justify-content spreads items along the main axis (flex-start, center, space-between, space-evenly).
  • align-items aligns them on the cross axis (stretch is the default).
  • flex-wrap lets items go to a new line.
  • gap sets space between items.

On an item, flex is a shorthand for flex-grow flex-shrink flex-basis. Basis is the starting size, grow says how free space is shared out, and shrink says how items give up space when there is not enough. flex: 1 means grow evenly from a basis of zero. flex: 0 0 250px means a fixed 250px.

At an 800px wide window the sidebar stays 250px and the main area fills the other 550px. Each card has a basis of 300px, so two fit per row (300 + 16 + 300 is less than 800, three would need 948px) and they grow to share the extra space. The fifth card sits alone on a third row and grows to fill it. The snippet starts with the border-box reset, because flex-basis: 250px would otherwise set only the content width and the padding would be added on top.

For centring one item both ways, the shortest working code is display: flex; justify-content: center; align-items: center; on the parent, with a height on the parent.

html
<style>
  *, *::before, *::after { box-sizing: border-box; }
  body { margin: 0; font-family: sans-serif; }
  .nav { display: flex; justify-content: space-between; align-items: center; padding: 1rem; background: #333; }
  .nav ul { display: flex; gap: 2rem; list-style: none; margin: 0; padding: 0; }
  .nav a { color: #fff; text-decoration: none; }
  .layout { display: flex; min-height: 200px; }
  .side { flex: 0 0 250px; background: #f0f0f0; padding: 1rem; }
  .main { flex: 1; padding: 1rem; }
  .cards { display: flex; flex-wrap: wrap; gap: 16px; padding: 16px; }
  .card { flex: 1 1 300px; background: #e8eefc; padding: 1rem; }
</style>
<nav class="nav" id="nav"><b>Logo</b><ul><li><a href="#">Home</a></li><li><a href="#">Fees</a></li></ul></nav>
<div class="layout"><aside class="side" id="side">Sidebar</aside><section class="main" id="main">Main</section></div>
<div class="cards" id="cards"><div class="card">1</div><div class="card">2</div><div class="card">3</div><div class="card">4</div><div class="card">5</div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Flexbox is one-dimensional: items sit on one line (or wrap into several lines).
  • justify-content works on the main axis and align-items on the cross axis; both swap when flex-direction is column.
  • flex is shorthand for flex-grow, flex-shrink and flex-basis; flex: 1 1 300px gives cards a base size that can grow and shrink.
  • gap sets space between items without margin hacks.
  • Add a border-box reset so flex-basis and padding behave the way you expect.

Common mistakes

  • using margin hacks to space items instead of gap.
  • a flex item whose long text refuses to shrink. Flex items have min-width: auto, so add min-width: 0 to allow the item to shrink and let overflow rules apply.

Follow-ups

  • what does flex: 1 expand to?
  • What is the difference between align-items and align-content?
  • How do flex-grow values of 1 and 2 divide leftover space between two items?

Tips

  • Remember the axes swap when flex-direction is column. Then justify-content works vertically and align-items works horizontally.
  • Describe each example in one sentence before showing the property list: a navbar, a card row, a sidebar. It sounds practical.
  • Be ready to say which values fix a flex row that overflows on small screens: flex-wrap, flex-basis and min-width: 0.