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

CSS Grid Mastery

22 min read
CSS Gridgrid-template-areasfr unitrepeat()minmax()auto-fillauto-fitsubgrid

Two-Dimensional Layouts

CSS Grid is designed for two-dimensional layouts — rows AND columns simultaneously. It's the most powerful layout system in CSS.

Grid vs Flexbox — When to Use Which

FeatureFlexboxCSS Grid
Dimensions1D (row OR column)2D (rows AND columns)
Content-first✅ Items determine layout❌ Grid defines layout
Layout-first❌ Can be hacked✅ Define areas, place items
Equal heightsOnly in same rowAcross entire grid
Overlapping itemsNot possible✅ Grid areas can overlap
Best forNavbars, card rows, toolbarsPage layouts, dashboards, galleries

Core Concepts

Tracks: Rows and columns defined by grid-template-rows and grid-template-columns.

fr unit: A fraction of available space. 1fr 2fr means the second column gets twice the space.

repeat(): Shorthand for repeated tracks: repeat(3, 1fr) = three equal columns.

minmax(): Set minimum and maximum track sizes: minmax(200px, 1fr).

auto-fill vs auto-fit:

KeywordBehaviorEmpty Tracks
auto-fillCreates as many tracks as fitKeeps empty tracks (space reserved)
auto-fitCreates as many tracks as fitCollapses empty tracks (items stretch)

Grid Template Areas

The most readable way to define layouts:

.layout {
  display: grid;
  grid-template-areas:
    "header  header  header"
    "sidebar content aside"
    "footer  footer  footer";
  grid-template-columns: 250px 1fr 200px;
  grid-template-rows: auto 1fr auto;
}

Subgrid

Subgrid allows a child grid to inherit its parent's track definitions, solving the alignment problem across nested grids.

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.