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 Grid Mastery: Complex Layouts Made Simple
XLinkedInReddit
MediumFrontend Engineering

CSS Grid Mastery: Complex Layouts Made Simple

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • Grid vs Flexbox
  • The Fundamentals
  • Responsive Without Media Queries
  • Advanced Techniques
  • Subgrid
  • Dense Packing
  • Common Gotchas

CSS Grid replaced countless JavaScript layout hacks. At Google, Grid is our go-to for any 2D layout challenge.

Grid vs Flexbox

Flexbox: 1-dimensional (row OR column). Grid: 2-dimensional (rows AND columns simultaneously). Use Flexbox for components, Grid for page layouts.

The Fundamentals

.dashboard {
  display: grid;
  grid-template-columns: 250px 1fr 300px;
  grid-template-rows: 60px 1fr 40px;
  grid-template-areas:
    "sidebar header aside"
    "sidebar main aside"
    "sidebar footer aside";
  gap: 16px;
  height: 100vh;
}

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

Responsive Without Media Queries

/* Auto-fill: creates as many columns as fit */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
}

/* Auto-fit: stretches items to fill space */
.stretch-grid {
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

Advanced Techniques

Subgrid

Lets child grids align to the parent grid's tracks. Game-changer for card layouts where you need content to align across cards.

.card {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 3; /* Card spans 3 rows of parent */
}

Dense Packing

.masonry-like {
  grid-auto-flow: dense; /* Fill holes in the grid */
}

Common Gotchas

  • 1fr respects content size — use minmax(0, 1fr) to truly divide space equally
  • Grid items stretch by default — use place-self: start to prevent it
  • Grid areas must form a rectangle — L-shapes won't work

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

  • Grid vs Flexbox
  • The Fundamentals
  • Responsive Without Media Queries
  • Advanced Techniques
  • Subgrid
  • Dense Packing
  • Common 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.