Skip to content

Flexbox: Aligning Layouts the Right Way

Flex container vs items, main/cross axis, and alignment properties that interviewers ask about.

Rajesh DhimanFebruary 08, 20262 min read
On this page

30‑second interview answer

Flexbox is a one-dimensional layout system (row or column). You set display: flex on a container, then align items along the main axis and cross axis using properties like justify-content and align-items. Flexbox shines for toolbars, navs, centering, and distributing space between items.


Flex mental model

  • Flex container: the parent with display: flex
  • Flex items: the direct children
  • Main axis is controlled by flex-direction:
    • row → main axis is horizontal
    • column → main axis is vertical

The big 6 properties (memorize)

Container

  1. flex-direction: row / column
  2. justify-content: alignment along main axis
  3. align-items: alignment along cross axis
  4. gap: spacing between items
  5. flex-wrap: allow wrapping

Item

  1. flex: shorthand for grow/shrink/basis

Centering (classic interview)

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

flex: 1 explained

flex: 1 typically means:

  • grow to fill remaining space
  • shrink if needed
  • base size is 0

Good for layouts like:

  • sidebar + content
  • navbar items

align-content vs align-items (trap)

  • align-items: aligns items within a single line
  • align-content: aligns lines when there is wrapping (flex-wrap: wrap)

If there’s no wrapping, align-content does nothing.


Common mistakes

  1. Applying align-items to items (it belongs to container)
  2. Confusing main vs cross axis
  3. Forgetting that only direct children are flex items

Mini Q&A

Q1: Difference between justify-content and align-items?

  • main axis vs cross axis.

Q2: When does align-content matter?

  • when items wrap into multiple lines.

Q3: Flexbox vs Grid?

  • Flexbox is 1D; Grid is 2D.

Summary checklist

  • I can explain main/cross axis.
  • I know justify-content vs align-items.
  • I can center content quickly.
  • I understand flex: 1 and wrapping.
Companion Book

Looking for structured, offline interview prep?

Free online lessons • Portable Amazon Kindle / Paperback

Frontend Interview Guide (2025 Edition) cover
Recommended for React & Frontend Rounds

Frontend Interview Guide (2025 Edition)

By Rajesh Dhiman

Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.

View on Amazon ↗Kindle & Paperback
JavaScript Interview Guide cover
Recommended for Core JS Rounds

JavaScript Interview Guide

By Rajesh Dhiman

Deep dive into the Event Loop, microtask draining, closures, prototypal inheritance, and asynchronous execution patterns.

View on Amazon ↗Kindle & Paperback

Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate, we may earn from qualifying purchases.

RD

Written by Rajesh Dhiman

Authored by Rajesh Dhiman and engineered at Eunix Tech. Focused on deep-dive frontend engineering, JavaScript runtime internals, React architecture, and web performance.