Skip to the page

Layout systems · core

Flexbox, including centering

Flexbox lays items out in one direction: justify-content works on the main axis, align-items on the cross axis, and flex: grow shrink basis decides how items share space.

The notes

What you must know

  • Center anything: display: flex; justify-content: center; align-items: center (or place-items: center on grid).
  • flex: 1 means flex: 1 1 0% — grow, shrink, start from zero; flex: auto starts from the content size.
  • min-width: auto on flex items stops them shrinking below their content; set min-width: 0 to allow text truncation.
  • gap works in flexbox; margin-left: auto pushes an item to the end.
  • flex-wrap: wrap + flex-basis builds simple responsive rows.

The red pen

Common traps: mistake → fix

  • Mistake: "justify-content centers vertically"

    Fix: it works on the main axis; with flex-direction: column that is vertical.

  • Mistake: "text-overflow: ellipsis works in a flex item"

    Fix: only after min-width: 0.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you center a div? Give three ways.
  2. What does flex: 1 mean exactly?
  3. Why doesn't my flex item shrink?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview