CSS Grid: Two-Dimensional Layout
Grid tracks, fr units, areas, and when Grid beats Flexbox.
On this page
30‑second interview answer
CSS Grid is a two-dimensional layout system (rows and columns together). You define grid tracks with grid-template-columns/rows and place items with lines, spans, or named areas. Use Grid for page layouts and complex alignment; use Flexbox for one-dimensional components.
Grid mental model
- You create a grid container:
display: grid - Define columns/rows (tracks)
- Place items
Example:
.container {
display: grid;
grid-template-columns: 240px 1fr;
gap: 16px;
}This is perfect for sidebar + content.
fr unit (interview favorite)
1fr means “one fraction of remaining space”.
grid-template-columns: 1fr 2fr;Second column gets double space.
grid-template-areas (super readable)
.container {
display: grid;
grid-template-columns: 1fr 300px;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}
header { grid-area: header; }
main { grid-area: main; }
aside { grid-area: sidebar; }
footer { grid-area: footer; }Centering (even easier)
.container {
display: grid;
place-items: center;
}Flex vs Grid (how to answer)
- Grid: 2D layout, page structure, aligned rows/cols
- Flex: 1D layout, components, distribution and alignment
Common mistakes
- Trying to use Grid when Flex is simpler
- Over-nesting grids
- Not using
gap
Mini Q&A
Q1: What is Grid good for?
- 2D layouts, page-level structure.
Q2: What does 1fr mean?
- a fraction of remaining space.
Q3: How to center in Grid?
place-items: center.
Summary checklist
- I can define grid tracks.
- I can explain fr units.
- I can use grid areas.
- I know when to choose Grid vs Flex.
Looking for structured, offline interview prep?
Free online lessons • Portable Amazon Kindle / Paperback
Frontend Interview Guide (2025 Edition)
By Rajesh Dhiman
Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.
JavaScript Interview Guide
By Rajesh Dhiman
Deep dive into the Event Loop, microtask draining, closures, prototypal inheritance, and asynchronous execution patterns.
Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate, we may earn from qualifying purchases.
Written by Rajesh Dhiman
Authored by Rajesh Dhiman and engineered at Eunix Tech. Focused on deep-dive frontend engineering, JavaScript runtime internals, React architecture, and web performance.