CSS Flexbox — The Complete Guide

The ultimate one-dimensional layout solution. From property reference to real-world layout patterns, master every capability Flexbox has to offer.

Flexbox (Flexible Box Layout) is one of the most practical CSS layout modules, designed to solve element alignment, arrangement, and space distribution along a single axis. Whether you're a frontend beginner or a seasoned developer, Flexbox is the CSS feature you'll use most often in day-to-day work.

1. Container Properties Reference (Parent Element)

After setting display: flex on the parent, you can use these container properties:

PropertyDescriptionCommon Values
flex-directionMain axis directionrow (default), column, row-reverse, column-reverse
flex-wrapWrapping behaviornowrap (default), wrap, wrap-reverse
justify-contentMain axis alignmentflex-start, center, flex-end, space-between, space-around, space-evenly
align-itemsCross axis alignmentstretch (default), flex-start, center, flex-end, baseline
align-contentMulti-line alignmentflex-start, center, flex-end, space-between, space-around, stretch
gapItem spacing10px, 1rem (same for row & column) or 10px 20px (row column)

2. Item Properties Reference (Child Elements)

PropertyDescriptionCommon Values
orderDisplay orderInteger (default 0, lower = earlier)
flex-growGrow factor0 (default), 1 (equal share of remaining space)
flex-shrinkShrink factor1 (default), 0 (no shrinking)
flex-basisInitial sizeauto (default), 200px, 30%
flexShorthand1, auto, 0 0 auto
align-selfIndividual item alignmentauto, flex-start, center, flex-end, stretch

3. The flex Shorthand Explained

flex is shorthand for flex-grow, flex-shrink, and flex-basis. Remember these common combinations:

/* Most common: distribute remaining space equally */
flex: 1;  /* Equivalent to flex: 1 1 0% */

/* No grow, no shrink, keep original size */
flex: 0 0 auto;  /* or flex: none */

/* Proportional distribution */
flex: 2;  /* Takes 2 shares */
flex: 1;  /* Takes 1 share */

/* Fixed basis + flexible */
flex: 1 1 200px;  /* Starts at 200px, can grow and shrink */
flex: 0 1 300px;  /* Starts at 300px, no grow, can shrink */
💡 Recommendation: Prefer concise forms like flex: 1 or flex: 0 0 auto to avoid unexpected behavior from manually setting all three values.

4. Common Layout Patterns

1. Responsive Navigation Bar

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2rem;
  height: 64px;
  background: #2b6cb0;
  color: #fff;
}

.nav-links {
  display: flex;
  gap: 1.5rem;
  list-style: none;
}

2. Card Grid Layout

.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
}

.card {
  flex: 1 1 300px;  /* Minimum 300px, auto-fill */
  max-width: calc(33.333% - 1rem);
  background: #1a1d2e;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}

3. Vertical Centering (The Classic Use Case)

/* Method 1: flex container */
.center-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

/* Method 2: margin auto */
.center-wrapper {
  display: flex;
}
.center-child {
  margin: auto;
}

4. Holy Grail Layout (Header + 3 Columns + Footer)

.holy-grail {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.main-content {
  display: flex;
  flex: 1;
}

.sidebar-left {
  flex: 0 0 200px;
  order: -1;
}

.main-area {
  flex: 1;
}

.sidebar-right {
  flex: 0 0 200px;
}

/* Mobile responsive */
@media (max-width: 768px) {
  .main-content {
    flex-direction: column;
  }
  .sidebar-left, .sidebar-right {
    flex: 0 0 auto;
  }
}

5. Sticky Footer (Footer Always at Bottom)

.page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.page main {
  flex: 1;  /* Fills remaining space, pushing footer to bottom */
}

6. Equal-Height Columns

.columns {
  display: flex;
  gap: 1rem;
}

.column {
  flex: 1;  /* All columns automatically equal height */
}

5. Alignment Techniques

Six Ways to Center Content

/* 1. Most common flex approach */
display: flex;
justify-content: center;
align-items: center;

/* 2. Horizontal only */
justify-content: center;

/* 3. Vertical only */
align-items: center;

/* 4. Baseline alignment (text baseline) */
align-items: baseline;

/* 5. space-between (flush to edges) */
justify-content: space-between;

/* 6. space-evenly (fully uniform distribution) */
justify-content: space-evenly;

6. The gap Property

gap is the recommended spacing solution—cleaner than margins and doesn't create extra space at the start or end:

.flex-container {
  display: flex;
  gap: 16px;           /* Row gap = Column gap */
  gap: 16px 8px;       /* Row gap 16px, Column gap 8px */
  row-gap: 16px;       /* Row gap only */
  column-gap: 8px;     /* Column gap only */
}
💡 Browser support: Flexbox gap is well-supported in all modern browsers (Chrome 84+, Firefox 63+, Safari 14.1+) and safe to use.

7. Common Pitfalls & Solutions

8. Recommended Tools

FAQ

Should I use Flexbox or Grid?

Use Flexbox for one-dimensional layouts (a single row or column) and Grid for two-dimensional layouts (rows + columns). In practice, they complement each other: Grid handles page structure, Flexbox handles component-level alignment.

What does flex: 1 mean?

flex: 1 is shorthand for flex-grow: 1, flex-shrink: 1, flex-basis: 0%. The item can grow, can shrink, and starts at zero size—effectively distributing remaining space equally.