Getting Started with CSS Grid
🎯 Introduction
As web layouts have become more complex, traditional techniques have become harder to work with. Flexbox solves many one-dimensional layout problems, but has limits when coordinating two dimensions. CSS Grid makes it easier to control rows and columns together. This beginner's guide introduces Grid concepts and core properties, then shows how to build common page layouts from scratch.
With these skills, you can build responsive grids, three-by-three grids, complex card arrangements, and entire page structures, improving development efficiency and maintainability.
🧱 What is CSS Grid?
CSS Grid is a two-dimensional layout system introduced with CSS3. It divides an area into rows and columns, then places content into the resulting cells. Think of a grid as a chessboard or table.
Compared with floats, positioning, and Flexbox, Grid's main advantage is its ability to coordinate rows and columns simultaneously rather than arrange content in just one dimension.
The Core Ideas Behind CSS Grid
- Define a grid container.
- Define rows and columns inside it.
- Place child elements (grid items) into grid cells.
- Let items span multiple rows or columns to create complex layouts.
📐 Grid Concepts and Properties
Start with these important concepts:
| Term | Description |
|---|---|
| Grid container | A parent declared with display: grid |
| Grid item | A direct child of the container |
| Grid line | A horizontal or vertical line dividing the grid |
| Grid cell | A single cell bounded by adjacent grid lines |
| Grid area | A rectangular area covering one or more adjacent cells |
Container Properties
| Property | Purpose | Example |
|---|---|---|
| display | Creates a grid container | display: grid; |
| grid-template-columns | Defines column widths and the number of columns | grid-template-columns: 100px 1fr 2fr; |
| grid-template-rows | Defines row heights and the number of rows | grid-template-rows: 200px auto; |
| grid-gap (grid-row-gap, grid-column-gap) | Sets row and column gaps | grid-gap: 10px 20px; |
| justify-items | Aligns items horizontally within their grid areas | justify-items: center; |
| align-items | Aligns items vertically within their grid areas | align-items: start; |
Item Properties
| Property | Purpose | Example |
|---|---|---|
| grid-column-start/end | Sets an item's starting and ending column lines | grid-column-start: 2; |
| grid-row-start/end | Sets an item's starting and ending row lines | grid-row-end: span 2; |
| grid-area | Places an item in an area that can span multiple cells | grid-area: 1 / 2 / 3 / 4; |
| justify-self | Aligns an individual item horizontally within its grid area | justify-self: end; |
| align-self | Aligns an individual item vertically within its grid area | align-self: center; |
🧩 Common Layouts with Grid
1. A Simple Three-Column Layout
<div class="container">
<div class="item">Left Column</div>
<div class="item">Main Content</div>
<div class="item">Right Column</div>
</div>
<style>
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-gap: 10px;
height: 200px;
}
.item {
background-color: #a0c4ff;
padding: 10px;
border: 1px solid #333;
text-align: center;
}
</style>
2. A Header–Content–Footer Layout
<div class="container">
<header class="header">Header</header>
<main class="main">Main Content</main>
<footer class="footer">Footer</footer>
</div>
<style>
.container {
display: grid;
grid-template-rows: 60px 1fr 40px;
height: 300px;
grid-gap: 5px;
}
.header,
.main,
.footer {
background-color: #ffadad;
padding: 10px;
border: 1px solid #333;
text-align: center;
}
</style>
3. A Three-by-Three Grid
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
</div>
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
grid-gap: 10px;
width: 320px;
}
.item {
background-color: #ffd6a5;
border: 1px solid #333;
text-align: center;
line-height: 100px;
font-size: 24px;
}
</style>
🔍 Grid vs. Flexbox
| Aspect | CSS Grid | Flexbox |
|---|---|---|
| Dimensions | Two-dimensional (rows and columns) | One-dimensional (a row or a column) |
| Layout control | Precise control over row heights, column widths, and cell placement | Well suited to flowing content |
| Spanning rows and columns | Supported | Not supported |
| Best suited to | Complex grids and overall page layouts | Simple linear layouts and one-dimensional lists |
| Learning curve | Relatively more complex | Easy to get started |
🛠️ Common Grid Properties
| Property | Purpose | Notes |
|---|---|---|
| display: grid | Creates a grid container | Required |
| grid-template-columns | Sets column count and widths | Accepts px, fr, %, auto, and other sizing values |
| grid-template-rows | Sets row count and heights | Accepts the same sizing values |
| grid-gap | Sets row and column gaps | Shorthand for grid-row-gap and grid-column-gap |
| grid-column-start/end | Sets an item's starting and ending column lines | Accepts line numbers and the span keyword |
| grid-row-start/end | Sets an item's starting and ending row lines | Accepts line numbers and the span keyword |
| grid-area | Defines the area an item occupies | grid-area: row-start / col-start / row-end / col-end |
| justify-items | Aligns items horizontally | start, center, end, stretch |
| align-items | Aligns items vertically | start, center, end, stretch |
| justify-self | Aligns one item horizontally | Same alignment values as above |
| align-self | Aligns one item vertically | Same alignment values as above |
❓ Knowledge Check
Q1: What is the main difference between Grid and Flexbox?
Grid is two-dimensional and controls rows and columns together. Flexbox is one-dimensional and arranges items along a row or column. Each suits different layout needs.
Q2: What is the fr unit?
fr represents a fraction of the grid container's available space. A track sized at 1fr receives one share, making it easy to distribute space proportionally.
Q3: How can an item span multiple rows and columns?
Set its grid-column-start/end and grid-row-start/end properties, or use the grid-area shorthand.
🧪 Exercises
Exercise 1: Create a Four-Column Responsive Grid
Use Grid to create four equal-width columns with 15px gaps and a fixed height of 150px.
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
</div>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 15px;
height: 150px;
}
.grid-item {
background-color: #9bf6ff;
border: 1px solid #333;
text-align: center;
line-height: 150px;
font-weight: bold;
}
</style>
Copy the code into your editor and preview the result!
Exercise 2: Create a Card Layout with a Two-Column Card
Build a card list where the first card spans two columns and each remaining card occupies one column.
<div class="card-container">
<div class="card card-large">Large Card</div>
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
<div class="card">Card 4</div>
</div>
<style>
.card-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 12px;
}
.card {
background-color: #b5ead7;
border: 1px solid #333;
padding: 20px;
text-align: center;
font-size: 18px;
}
.card-large {
grid-column: span 2;
background-color: #ffcbf2;
}
</style>
Copy the code into your editor and preview the result!
🎉 You have learned CSS Grid fundamentals, core properties, common layout patterns, and how to choose between Grid and Flexbox. With practice, Grid will become a powerful tool for building page layouts!
