DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. CSS Flexbox: The Complete Visual Guide
XLinkedInReddit
MediumFrontend Engineering

CSS Flexbox: The Complete Visual Guide

D
DevPrep Team
February 10, 2026·2 min read·0
Table of Contents
  • Container Properties
  • display: flex
  • flex-direction
  • justify-content (Main Axis)
  • align-items (Cross Axis)
  • flex-wrap
  • Item Properties
  • flex shorthand
  • align-self
  • order
  • Common Patterns
  • Gotchas

Flexbox is the foundation of modern CSS layouts. Master these properties and you can build any 1D layout.

Container Properties

display: flex

Creates a flex container. Direct children become flex items.

flex-direction

.container {
  flex-direction: row;            /* → default, left to right */
  flex-direction: row-reverse;    /* ← right to left */
  flex-direction: column;         /* ↓ top to bottom */
  flex-direction: column-reverse; /* ↑ bottom to top */
}

justify-content (Main Axis)

.container {
  justify-content: flex-start;    /* |■ ■ ■      | */
  justify-content: center;        /* |   ■ ■ ■   | */
  justify-content: flex-end;      /* |      ■ ■ ■| */
  justify-content: space-between; /* |■    ■    ■| */
  justify-content: space-around;  /* | ■   ■   ■ | */
  justify-content: space-evenly;  /* |  ■  ■  ■  | */
}

align-items (Cross Axis)

.container {
  align-items: stretch;    /* Items fill container height (default) */
  align-items: flex-start; /* Items at top */
  align-items: center;     /* Items centered vertically */
  align-items: flex-end;   /* Items at bottom */
  align-items: baseline;   /* Items aligned by text baseline */
}

flex-wrap

.container {
  flex-wrap: nowrap; /* All items on one line (default) */
  flex-wrap: wrap;   /* Items wrap to next line */
}

Item Properties

flex shorthand

.item {
  flex: 1;       /* flex-grow: 1, flex-shrink: 1, flex-basis: 0 */
  flex: 0 0 200px; /* Don't grow, don't shrink, 200px wide */
  flex: 2 1 auto;  /* Grow 2x, can shrink, natural width */
}

align-self

.item {
  align-self: center; /* Override container's align-items for this item */
}

order

.item { order: -1; } /* Move before other items (default is 0) */

Common Patterns

/* Centering */
.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Sticky footer */
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
main { flex: 1; }

/* Holy grail layout */
.layout {
  display: flex;
}
.sidebar { flex: 0 0 250px; }
.content { flex: 1; }
.aside { flex: 0 0 200px; }

Gotchas

  • flex-basis vs width: flex-basis wins in a flex container
  • min-width: auto is the default for flex items — content can overflow. Use min-width: 0 to allow shrinking
  • Margin auto absorbs extra space: margin-left: auto pushes items to the right

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Container Properties
  • display: flex
  • flex-direction
  • justify-content (Main Axis)
  • align-items (Cross Axis)
  • flex-wrap
  • Item Properties
  • flex shorthand
  • align-self
  • order
  • Common Patterns
  • Gotchas

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.