CSS Grid lets you build two-dimensional layouts with rows and columns. Define a grid container, place items in cells, and create responsive page layouts without floats or positioning hacks.
01
Container
display: grid.
02
Columns
grid-template.
03
Areas
Named regions.
04
Span
grid-column.
05
fr unit
Flexible size.
06
2D layout
Rows + cols.
Fundamentals
Introduction
The CSS Grid Layout Module is a powerful system for creating web layouts with rows and columns. Unlike Flexbox, which handles one dimension at a time, Grid controls both dimensions simultaneously.
What Is CSS Grid?
CSS Grid divides a container into tracks (rows and columns) and places child elements into cells. You get precise control over placement, spanning, gaps, and responsive reflow.
Grid is ideal for page layouts (header, sidebar, main, footer), photo galleries, dashboards, and card grids that need to adapt to screen size.
💡
Beginner Tip
Start with display: grid, grid-template-columns: repeat(3, 1fr), and gap: 1rem. That alone creates a clean three-column layout. Add grid-template-areas when you need named page regions.
Basic Concepts
Grid container — Parent with display: grid.
Grid item — Direct child placed on the grid.
Grid tracks — Rows and columns that form the grid.
Grid lines — Dividing lines between tracks (numbered from 1).
Grid area — Named rectangular region spanning multiple cells.
Foundation
📝 Syntax
Define a basic three-column grid with equal-width tracks:
auto-fit creates as many columns as fit. minmax(120px, 1fr) ensures each column is at least 120px but grows to fill space. Columns wrap when the container shrinks.
Tips
💬 Usage Tips
Start simple — Three equal columns teach the basics before complex areas.
Use fr for flexibility — Prefer 1fr over percentages for equal columns.
gap over margins — gap handles spacing between all cells cleanly.
DevTools overlay — Firefox and Chrome show grid lines for debugging.
Combine with Flexbox — Grid for page structure; Flexbox inside components.
Watch Out
⚠️ Common Pitfalls
Only direct children — Grid applies to immediate children only.
Overlapping items — Poorly placed spans can cause items to overlap.
Over-complexity — Start simple; add areas and spans as needed.
Using Grid for navbars — Flexbox is often simpler for one-row navigation.
Forgetting minmax minimum — Too-small min values can squash content on mobile.
A11y
♿ Accessibility
Source order — Visual grid placement should not break logical DOM order for screen readers.
Semantic HTML — Use <header>, <nav>, <main>, <footer> in grid layouts.
Reading order — Avoid rearranging content in ways that confuse assistive technology.
Focus visibility — Grid items still need visible focus styles for keyboard users.
🧠 How CSS Grid Works
1
display: grid on parent
The container becomes a grid formatting context with rows and columns.
Container
2
Tracks are defined
grid-template-columns and grid-template-rows set track sizes.
Tracks
3
Items placed in cells
Children auto-place or use grid-area / grid-column for precise placement.
Place
=
▦
Two-dimensional layout
Rows and columns align content in a structured grid.
Compatibility
🖥 Browser Compatibility
CSS Grid has universal support in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile browsers.
✓ Baseline · Universal support
CSS Grid Layout Module
Grid is production-ready in every current browser. No vendor prefixes needed.
100%Modern browsers
Google ChromeFull support
Full support
Mozilla FirefoxFull support
Full support
Apple SafariFull support
Full support
Microsoft EdgeFull support
Full support
OperaFull support
Full support
CSS Grid100% supported
Bottom line: CSS Grid is safe for production. Legacy IE issues are irrelevant for modern development.
Wrap Up
🎉 Conclusion
CSS Grid is essential for modern two-dimensional layouts. With display: grid, grid-template-columns, and gap, you can build page structures and card grids that adapt to any screen size.
Practice the four examples above, then explore grid-template-areas for full page layouts and auto-fit for responsive galleries. Pair Grid with Flexbox for the best of both worlds.
Rearrange DOM order visually without a11y consideration
Over-engineer layouts before trying simple columns
Forget that only direct children are grid items
Rely on floats when Grid solves the problem cleanly
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about CSS Grid
Use these points when building two-dimensional layouts.
5
Core concepts
grid01
Container
display: grid.
Start
fr02
fr unit
Flexible size.
Sizing
area03
Areas
Named regions.
Layout
span04
Span
grid-column.
Place
2D05
Two axes
Rows + cols.
Scope
❓ Frequently Asked Questions
CSS Grid is a two-dimensional layout system. You define rows and columns on a grid container, then place child elements into cells. It is ideal for page layouts, card grids, and any design that needs both horizontal and vertical structure.
Set display: grid or display: inline-grid on the parent element. That parent becomes a grid container, and its direct children become grid items placed into the grid.
Grid handles two dimensions — rows and columns together. Flexbox handles one dimension — a row or a column. Use Grid for page layouts and card grids; use Flexbox for navbars and simple alignment flows.
fr means fraction of available space. In grid-template-columns: 1fr 2fr 1fr, the middle column gets twice as much free space as the side columns.
Yes. CSS Grid is supported in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile browsers. It is safe for production use.