🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

CSS Flexbox Complete Guide 2026: Layouts, Patterns and Real Examples

⏱️3 min read  ·  603 words

CSS Flexbox is still the most important layout tool for UI components in 2026. While CSS Grid handles two-dimensional layouts, Flexbox excels at one-dimensional alignment, responsive navigation bars, card layouts, and centering elements. This complete guide covers every property with visual examples.

Flex Container Properties

.container {
  display: flex;          /* or inline-flex */
  flex-direction: row;    /* row | row-reverse | column | column-reverse */
  flex-wrap: nowrap;      /* nowrap | wrap | wrap-reverse */
  flex-flow: row nowrap;  /* shorthand: flex-direction + flex-wrap */

  /* Main axis alignment (row = horizontal, column = vertical) */
  justify-content: flex-start;   /* flex-start | flex-end | center | space-between | space-around | space-evenly */

  /* Cross axis alignment */
  align-items: stretch;          /* stretch | flex-start | flex-end | center | baseline */

  /* Multi-line cross axis alignment (only when flex-wrap: wrap) */
  align-content: normal;         /* normal | flex-start | flex-end | center | space-between | space-around | space-evenly | stretch */

  /* Gap between items */
  gap: 16px;             /* gap: row-gap col-gap */
  row-gap: 16px;
  column-gap: 24px;
}

Flex Item Properties

.item {
  /* flex-grow: how much to grow (0 = don't grow) */
  flex-grow: 1;          /* 0 = don't grow, 1 = grow proportionally */

  /* flex-shrink: how much to shrink (1 = shrink proportionally) */
  flex-shrink: 0;        /* 0 = don't shrink */

  /* flex-basis: initial size before growing/shrinking */
  flex-basis: 200px;     /* auto | 0 | 200px | 25% */

  /* Shorthand: grow shrink basis */
  flex: 1 1 auto;        /* flex: 1 (common) | flex: 0 0 200px | flex: auto */

  /* Self alignment (overrides align-items for this item) */
  align-self: center;    /* auto | stretch | flex-start | flex-end | center | baseline */

  /* Order: visual order (default: 0) */
  order: -1;             /* appears before order:0 items */
}

Common Flexbox Patterns

Perfect Centering

/* Center anything horizontally and vertically */
.center-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

/* Or use margin: auto on the child (works when parent is flex) */
.centered-item {
  margin: auto;
}

Navigation Bar

<nav class="navbar">
  <a href="/" class="logo">TechPulse</a>
  <ul class="nav-links">
    <li><a href="/blog">Blog</a></li>
    <li><a href="/about">About</a></li>
  </ul>
  <a href="/contact" class="cta">Get Started</a>
</nav>

.navbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  height: 64px;
}

.logo { font-weight: bold; }

.nav-links {
  display: flex;
  gap: 24px;
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;              /* push CTA to right */
  justify-content: center;
}

.cta { margin-left: auto; }  /* or use flex: 1 on nav-links

Card Layout with Equal Heights

.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
}

.card {
  display: flex;
  flex-direction: column;
  flex: 1 1 300px;     /* grow, shrink, min-basis 300px */
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 24px;
}

.card-content {
  flex: 1;             /* grows to fill space, pushes button to bottom */
}

.card-button {
  margin-top: 16px;    /* always at bottom regardless of content height */
}

Sticky Footer

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

main {
  flex: 1;             /* grows to fill remaining space, pushing footer down */
}

footer {
  /* Always at bottom */
}

Holy Grail Layout

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

.layout-header,
.layout-footer { flex: none; }

.layout-body {
  display: flex;
  flex: 1;
}

.layout-sidebar { width: 250px; flex: none; }
.layout-main    { flex: 1; }        /* fills remaining space */
.layout-aside   { width: 200px; flex: none; }

Responsive Layout with flex-wrap

/* Items stack vertically on mobile, go horizontal on desktop */
.responsive-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.responsive-item {
  flex: 1 1 250px;     /* min-width 250px, grows to fill */
}

/* Force break: half-width items */
.half-width {
  flex: 1 1 calc(50% - 16px);
  max-width: calc(50% - 16px);
}

/* Full-width override */
.full-width {
  flex-basis: 100%;
}

Flexbox vs Grid: When to Use Each

Scenario Use Flexbox Use Grid
Navigation bar
Card list in a row
Complex 2D layout
Center an element
Form layout ✅ (simple) ✅ (complex)
Page layout (header/main/footer)
Item auto-placement

Flexbox mastery is essential for any web developer. The combination of flex: 1 for growing items, gap for spacing, align-items for cross-axis alignment, and flex-wrap: wrap for responsive layouts covers 90% of real-world use cases.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇬🇧 English🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা