blog/3
CSS & Styling6 min read

CSS Grid vs Flexbox: When to Use Which

By Marin Cholakov12/05/2024
CSSLayoutFlexboxGridFrontend
CSS Grid vs Flexbox: When to Use Which

Understanding when to use CSS Grid versus Flexbox is crucial for modern web development. Both are powerful layout tools, but they excel in different scenarios.

Flexbox: One-Dimensional Layouts

Flexbox is perfect for:

  • Navigation bars
  • Card layouts
  • Centering content
  • Distributing space between items

Example: Navigation Bar

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
}

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

Flex Properties Deep Dive

Container Properties

.flex-container {
  display: flex;
  flex-direction: row | row-reverse | column | column-reverse;
  flex-wrap: nowrap | wrap | wrap-reverse;
  justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
  align-items: stretch | flex-start | flex-end | center | baseline;
  align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}

Item Properties

.flex-item {
  flex-grow: 1; /* Ability to grow */
  flex-shrink: 1; /* Ability to shrink */
  flex-basis: auto; /* Initial size */
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

CSS Grid: Two-Dimensional Layouts

CSS Grid excels at:

  • Complex page layouts
  • Dashboard interfaces
  • Gallery layouts
  • Responsive design patterns

Example: Dashboard Layout

.dashboard {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main aside"
    "footer footer footer";
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 250px 1fr 200px;
  min-height: 100vh;
  gap: 1rem;
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }

Advanced Grid Techniques

Responsive Grid

.responsive-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 2rem;
}

@media (max-width: 768px) {
  .responsive-grid {
    grid-template-columns: 1fr;
  }
}

Grid Lines and Spans

.grid-item {
  grid-column: 1 / 3; /* Span from line 1 to line 3 */
  grid-row: 2 / span 2; /* Start at line 2, span 2 rows */
}

Combining Both

The real power comes from combining both techniques:

.card-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
}

.card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 1.5rem;
  border: 1px solid #e1e1e1;
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.card-content {
  flex-grow: 1;
}

.card-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

Decision Matrix

Use Case Flexbox Grid Example
Single row/column Navigation bar
Multiple rows/columns Page layout
Content-driven Tag list
Layout-driven Dashboard
Component spacing Button groups
Page structure Header/main/footer
Centering Modal dialogs
Equal heights Card grids

Practical Examples

Flexbox: Perfect for Forms

.form-group {
  display: flex;
  gap: 1rem;
  align-items: flex-end;
}

.form-group input {
  flex: 1;
}

.form-group button {
  flex-shrink: 0;
}

Grid: Ideal for Media Layouts

.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
  padding: 1rem;
}

.media-item:nth-child(1) {
  grid-column: span 2;
  grid-row: span 2;
}

Performance Considerations

  1. Flexbox is generally faster for simple layouts
  2. Grid can be more performant for complex layouts
  3. Both are well-optimized in modern browsers
  4. Choose based on layout needs, not performance

Browser Support

Both Flexbox and Grid have excellent modern browser support:

  • Flexbox: 99%+ support
  • Grid: 95%+ support

For older browsers, provide fallbacks:

.layout {
  /* Fallback */
  display: block;
  
  /* Modern browsers */
  display: grid;
  grid-template-columns: 1fr 2fr;
}

Choose the right tool for the job, and don't be afraid to combine them for optimal results. The key is understanding that Flexbox excels at one-dimensional layouts while Grid shines with two-dimensional layouts.

Share this post