JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionscss
PrevNext

Learn the concept

Flexbox Layout

css
mid
layout

Explain the CSS Flexbox axis model and how to solve common layout challenges with it.

flexbox
layout
alignment
responsive
css
Quick Answer

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.

Detailed Explanation

CSS Flexbox is a one-dimensional layout system for distributing space and aligning items within a container. Understanding the axis model is the key to mastering Flexbox — everything revolves around the main axis and cross axis.

The Two Axes

  • Main axis — the direction items are laid out, controlled by flex-direction:

    • row (default): left → right
    • row-reverse: right → left
    • column: top → bottom
    • column-reverse: bottom → top
  • Cross axis — perpendicular to the main axis. If main axis is horizontal (row), cross axis is vertical, and vice versa.

Alignment Properties

| Property | Axis | Applied to | Controls | |----------|------|-----------|----------| | justify-content | Main | Container | Distribution of items along main axis | | align-items | Cross | Container | Alignment of items along cross axis | | align-self | Cross | Individual item | Override align-items for one item | | align-content | Cross | Container | Distribution of wrapped lines (multi-line only) | | gap | Both | Container | Space between items |

justify-content values: flex-start, flex-end, center, space-between, space-around, space-evenly

align-items values: stretch (default), flex-start, flex-end, center, baseline

The flex Shorthand

flex is shorthand for flex-grow, flex-shrink, and flex-basis:

  • flex-grow — How much an item should grow relative to siblings when extra space is available (default: 0 = don't grow)
  • flex-shrink — How much an item should shrink when space is insufficient (default: 1 = shrink equally)
  • flex-basis — The initial size before growing/shrinking (default: auto = use content size or width/height)

Common patterns:

  • flex: 1 = flex: 1 1 0% — Item takes equal share of available space
  • flex: auto = flex: 1 1 auto — Item grows/shrinks from its content size
  • flex: none = flex: 0 0 auto — Item doesn't grow or shrink (fixed size)

flex-wrap

flex-wrap: wrap allows items to flow onto multiple lines when they don't fit on one line. Combined with flex-basis, this creates responsive grid-like layouts without media queries:

CSS
.container { display: flex; flex-wrap: wrap; gap: 1rem; }
.item { flex: 1 1 300px; } /* Each item is at least 300px, grows to fill */

Common Layout Patterns

  1. Perfect centering — display: flex; justify-content: center; align-items: center;
  2. Sticky footer — flex-direction: column; min-height: 100vh; with flex: 1 on main content
  3. Equal-height columns — align-items: stretch (the default)
  4. Navigation bar — Logo left, links right: justify-content: space-between
  5. Card grid — flex-wrap: wrap with flex-basis for minimum card width

Flexbox vs Grid

  • Flexbox — one-dimensional (row OR column). Best for: navigation bars, toolbars, card rows, centering, flexible inline layouts.
  • Grid — two-dimensional (rows AND columns). Best for: page layouts, data grids, complex dashboards, overlapping elements.

Use Flexbox for components and linear arrangements. Use Grid for page-level structure and 2D layouts.

Code Examples

Common Flexbox PatternsCSS
/* 1. Perfect centering */
.center-content {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

/* 2. Navigation bar — logo left, links right */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 1rem;
}

/* 3. Sticky footer */
.page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.page-content {
  flex: 1; /* Takes all available space, pushing footer down */
}

/* 4. Responsive card grid (no media queries needed) */
.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.card {
  flex: 1 1 300px; /* Min 300px, grows to fill row */
}

/* 5. Equal-width columns */
.columns {
  display: flex;
  gap: 1rem;
}
.col {
  flex: 1; /* Each column gets equal space */
}

Real-World Applications

Use Cases

Product Card Layouts

E-commerce sites use Flexbox with flex-wrap and flex-basis to create responsive product grids that adapt from 4 columns on desktop to 1 column on mobile without media queries.

Navigation Components

Navigation bars use justify-content: space-between for logo/links separation, and align-items: center for vertical alignment of varied-height elements.

Mini Projects

Responsive Dashboard Layout

intermediate

Build a dashboard with a sidebar, main content, and header using only Flexbox. The sidebar should collapse on mobile and the content cards should reflow responsively.

Industry Examples

Tailwind CSS

Tailwind's flex utilities (flex, flex-row, justify-center, items-center, gap-4) are the most-used layout classes, making Flexbox the primary layout tool in Tailwind projects.

Resources

CSS-Tricks - A Complete Guide to Flexbox

article

MDN - CSS Flexible Box Layout

docs

Flexbox Froggy — Interactive Game

course

Related Questions

How does CSS Grid work and how does it differ from Flexbox?

mid
grid
Previous
What are CSS custom properties (variables) and how do they differ from Sass variables?
Next
What are the advantages of utility-first CSS (Tailwind) over CSS-in-JS?
PrevNext