CSS Grid
CSS Grid Layout is a two-dimensional layout system that enables precise control over both rows and columns simultaneously, allowing complex layouts with explicit grid positioning and automatic content placement.
What is CSS Grid?
CSS Grid is a two-dimensional layout system that places elements into rows and columns at the same time. Set display: grid on a container, define tracks with grid-template-columns and grid-template-rows, and the children fill the cells automatically.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
/* Responsive cards with no media queries */
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
}Interactive CSS Grid Demo
Code Editor
<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">Item 4</div>
<div class="grid-item">Item 5</div>
<div class="grid-item">Item 6</div>
</div>Preview
Grid Fundamentals
CSS Grid Layout is the most powerful layout system available in CSS. It's a 2-dimensional system, meaning it can handle both columns and rows, unlike flexbox which is largely a 1-dimensional system.
Core Concepts
Grid Container: Parent element with display: grid
Grid Items: Direct children of the container
Grid Lines: Dividing lines of the grid structure
Grid Tracks: Space between two grid lines
Grid Areas: Rectangular space bounded by lines
Grid Cells: Single units of the grid
Essential Properties
Container Properties
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto 200px auto;
gap: 20px;Define grid structure and spacing between items
Item Properties
grid-column: 1 / 3;
grid-row: span 2;
grid-area: header;
justify-self: center;Position and align individual grid items
Grid Template Areas
grid-template-areas:
"header header"
"sidebar main"
"footer footer";Create semantic layout structures
Fractional Units
grid-template-columns:
1fr 2fr 1fr;
/* Flexible fractions */Distribute available space proportionally
Auto-fit & Minmax
grid-template-columns:
repeat(auto-fit,
minmax(200px, 1fr));Create responsive grids automatically
Grid Layout Examples
Basic Grid Layout
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}Creates a simple 3-column grid with equal-width columns and consistent spacing.
Page Layout with Grid Areas
.page-layout {
display: grid;
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
grid-template-columns: 200px 1fr 150px;
min-height: 100vh;
}
.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }A complete page layout using named grid areas for semantic structure.
Responsive Card Grid
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
padding: 2rem;
}
/* Cards automatically wrap to new rows */
/* Minimum 300px, maximum equal fractions */Auto-fitting cards that maintain minimum width and distribute space evenly.
Advanced Features
Auto-placement Algorithm
grid-auto-flow: row | column | dense;Controls how auto-placed items flow into the grid
grid-auto-columns: minmax(100px, auto);Sets size of implicitly created columns
Best Practices
Best Practices
- • Use Grid for two-dimensional layouts
- • Combine with Flexbox for component layout
- • Use named lines and areas for clarity
- • Test with varying content amounts
- • Consider fallbacks for older browsers
Common Use Cases
- • Page layouts and templates
- • Complex UI components
- • Image galleries and portfolios
- • Dashboard and admin interfaces
- • Magazine-style layouts
CSS Grid — step by step
Defining a Grid
display: grid turns an element into a grid container. grid-template-columns defines column tracks (e.g., 1fr 1fr 1fr for three equal columns). grid-template-rows defines row tracks. The fr unit distributes available space proportionally.
🎯 Quick Check
What does the fr unit represent in CSS Grid?
CSS Grid: summary
Key points
- The
frunit means a fraction of the free space:1fr 2frgives the second column twice the width of the first. repeat(auto-fit, minmax(250px, 1fr))creates as many columns as fit, each at least 250px wide. It's the classic responsive card grid.- Items can span several tracks with
grid-column: span 2or be placed exactly with line numbers, likegrid-column: 1 / 3. grid-template-areaslets you name regions (header, sidebar, main) and draw the layout as text.gapsets the space between rows and columns; there's no need for margins on grid items.
Common mistakes
- Mixing up
auto-fitandauto-fill:auto-fitcollapses empty tracks so items stretch, whileauto-fillkeeps the empty tracks. - Forgetting that grid line numbers start at 1, and that
-1refers to the last line. - Using Grid for a single row of items whose widths should follow their content. Flexbox is simpler there.
Frequently asked questions
›What does 1fr mean in CSS Grid?
fr is a fractional unit. 1fr takes one share of the space left after fixed-size tracks and gaps, so grid-template-columns: 1fr 1fr 1fr makes three equal columns.
›How do I make a responsive grid without media queries?
Use grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)). The browser fits as many 250px-minimum columns as the width allows and stretches them to fill the row.
›Should I use Grid or Flexbox?
Use Grid when you're controlling rows and columns together, such as page layouts, galleries and dashboards. Use Flexbox for one row or column whose items size by content. They work well together: a Grid page layout with Flexbox inside each card is common.
›How do I make an item span multiple columns?
Set grid-column: span 2 on the item, or give exact lines with grid-column: 1 / 3. Use grid-row the same way for rows.