JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicscssFlexbox Layout
PrevNext
css
beginner
10 min read

Flexbox Layout

alignment
centering
flex-grow
flexbox
fundamentals
gap
layout
responsive

Flexbox is a one-dimensional layout model for distributing space and aligning items along a main axis and cross axis, with container properties controlling flow direction and item properties controlling grow, shrink, and basis behavior.

Key Points

1Main and Cross Axes

Flexbox lays out items along a main axis (set by flex-direction) and aligns them along the perpendicular cross axis. Changing direction swaps which axis justify-content and align-items control.

2Flex Grow, Shrink, and Basis

flex-grow controls how items absorb extra space, flex-shrink controls how they give up space, and flex-basis sets the starting size before distribution. The flex shorthand combines all three.

3Alignment and Distribution

justify-content distributes items along the main axis (space-between, center, etc.), align-items aligns on the cross axis, and gap adds spacing without margin hacks.

4One-Dimensional Layout

Flexbox handles layout in one direction at a time. For two-dimensional layouts controlling both rows and columns simultaneously, use CSS Grid instead.

What You'll Learn

  • Set up a flex container and control item flow with direction, wrap, and gap
  • Use flex-grow, flex-shrink, and flex-basis to control how items share available space
  • Center elements both horizontally and vertically using Flexbox
  • Decide when to use Flexbox versus CSS Grid for a given layout

Deep Dive

The CSS Flexible Box Layout (Flexbox) is the most widely used CSS layout mechanism for component-level arrangements. It provides powerful alignment and distribution capabilities along a single axis.

Core Concepts

Flexbox operates on two axes:

  • Main axis — Defined by flex-direction (default: row, meaning left to right)
  • Cross axis — Perpendicular to the main axis

A flex container is created with display: flex (block-level) or display: inline-flex (inline-level). All direct children become flex items.

Container Properties

  • flex-direction — Sets the main axis: row (default), row-reverse, column, column-reverse. When direction is column, the main axis is vertical and justify-content controls vertical distribution.
  • flex-wrap — nowrap (default) keeps all items on one line, wrap allows items to flow to the next line. The shorthand flex-flow combines direction and wrap.
  • justify-content — Distributes items along the main axis: flex-start, flex-end, center, space-between (equal gaps, no outer space), space-around (equal space around each item), space-evenly (equal space everywhere).
  • align-items — Aligns items along the cross axis: stretch (default), flex-start, flex-end, center, baseline.
  • align-content — Controls spacing between wrapped lines (only applies when items wrap to multiple lines).
  • gap — Sets spacing between flex items without margin hacks. Supports row-gap and column-gap independently.

Item Properties

  • flex-grow — Proportion of remaining space the item should absorb. Default 0 (don't grow). flex-grow: 1 on all items distributes space equally.
  • flex-shrink — Proportion by which the item shrinks when space is insufficient. Default 1 (all items shrink equally). Set to 0 to prevent shrinking.
  • flex-basis — The initial size of the item before growing or shrinking. Default auto (uses the item's width/height). Set to 0 to ignore content size and distribute purely by flex-grow ratio.
  • flex shorthand — flex: grow shrink basis. Common values: flex: 1 (same as 1 1 0), flex: auto (same as 1 1 auto), flex: none (same as 0 0 auto).
  • align-self — Overrides align-items for a single item.
  • order — Changes visual order without changing DOM order (use sparingly for accessibility).

Common Patterns

Centering (the classic interview question):

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

Navigation bar: display: flex; justify-content: space-between; for logo on the left and links on the right.

Sticky footer: flex-direction: column; min-height: 100vh; on the body, with flex: 1 on the main content area.

Equal-height cards: Flex items in a row stretch to the height of the tallest item by default (align-items: stretch).

Flexbox vs Grid

Flexbox is one-dimensional — it handles layout in a single direction (row or column) at a time. CSS Grid is two-dimensional — it handles rows and columns simultaneously. Use Flexbox for component internals (navigation bars, card contents, form layouts) and Grid for page-level structure. They combine well: a Grid page layout with Flexbox components inside each grid area.

Gotchas

  • flex-basis takes priority over width (in row direction) or height (in column direction) on flex items
  • The minimum size of a flex item defaults to min-content, which can prevent shrinking below content width — use min-width: 0 to allow it
  • margin: auto on flex items absorbs remaining space, which can be used for alignment (e.g., pushing an item to the far right)

Fun Fact

Before Flexbox, centering a div both horizontally and vertically in CSS required hacks like negative margins, table-cell display, or absolute positioning with transforms. Flexbox's two-line centering solution became so celebrated that 'how to center a div' is now a running joke in the front-end community.

Learn These First

The CSS Box Model

beginner

Continue Learning

CSS Grid Layout

intermediate

Responsive Design Fundamentals

beginner

CSS Positioning & Stacking Context

beginner

Practice What You Learned

How does CSS Flexbox work and when should you use it?
junior
flexbox
Flexbox is a one-dimensional layout system for arranging items in a row or column. Set display: flex on a container, then control direction with flex-direction, alignment with justify-content (main axis) and align-items (cross axis), and item sizing with flex-grow, flex-shrink, and flex-basis.
Explain the CSS Flexbox axis model and how to solve common layout challenges with it.
mid
layout
Flexbox uses a main axis (direction items are laid out) and a cross axis (perpendicular). justify-content aligns items along the main axis, align-items along the cross axis. The flex shorthand controls how items grow, shrink, and their base size.
Previous
CSS Custom Properties (Variables)
Next
CSS Grid Layout
PrevNext