DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • The Box Model
  • Specificity & The Cascade
  • CSS Units Deep Dive
  • Stacking Contexts & Z-Index
  • Flexbox Mastery
  • CSS Grid Mastery
  • Centering, Subgrid & Container Queries
  • CSS Custom Properties & Variables
  • Selectors: :has(), Pseudo-classes & Pseudo-elements
  • Responsive Functions: calc(), clamp(), min(), max()
  • Media Queries & Mobile-First Design
  • Transitions & Animations
  • Transforms, Filters & Clipping
  • CSS Methodologies: BEM, CSS-in-JS & Tailwind
  • Typography, Color Spaces & Dark Mode
  • CSS Performance & Optimization
  • CSS Interview Questions — Part 1
  • CSS Interview Questions — Part 2
← From Beginner to Expert in Fundamentals of Frontend

Flexbox Mastery

22 min read
Flexboxjustify-contentalign-itemsflex-growflex-shrinkflex-basisgapflex-wrap

One-Dimensional Layouts Made Easy

Flexbox is designed for one-dimensional layouts — either a row OR a column. It excels at distributing space and aligning items within a container.

The Two Axes

AxisDirection (row)Direction (column)Controlled By
Main AxisHorizontal →Vertical ↓justify-content
Cross AxisVertical ↓Horizontal →align-items

Container Properties

PropertyValuesEffect
flex-directionrow | column | row-reverse | column-reverseSets main axis direction
justify-contentflex-start | center | flex-end | space-between | space-around | space-evenlyDistributes items along main axis
align-itemsstretch | flex-start | center | flex-end | baselineAligns items on cross axis
flex-wrapnowrap | wrap | wrap-reverseControls overflow wrapping
gapAny lengthSpace between items (modern, replaces margins)

Item Properties — The flex Shorthand

The flex property is shorthand for three values:

flex: [grow] [shrink] [basis];
flex: 1 1 0%;   /* Grow, shrink, start from 0 */
flex: 0 0 auto; /* Don't grow, don't shrink, use content width */
PropertyDefaultWhat It Does
flex-grow0How much extra space this item should take (ratio)
flex-shrink1How much this item should shrink if space is tight
flex-basisautoThe starting size before grow/shrink kicks in

Common Patterns

Holy Grail Layout: Header, 3-column content, footer — all with Flexbox.

Sticky Footer: Footer always at the bottom, even with little content.

Equal Height Cards: All cards in a row match the tallest card's height (default align-items: stretch).

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.