🎨
CSS Grid Layout
Intermediate
90 XP
35 min
Lesson Content
CSS Grid Layout
CSS Grid is a powerful two-dimensional layout system that allows you to create complex layouts with rows and columns. Unlike Flexbox (which is one-dimensional), Grid lets you control both rows and columns simultaneously.
Basic Grid Setup
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;
}Key Properties
- grid-template-columns: Defines column sizes (e.g., 1fr 1fr, 200px auto, repeat(3, 1fr))
- grid-template-rows: Defines row sizes
- gap: Space between grid items
- grid-column: Span items across columns
- grid-row: Span items across rows
Example: 3-Column Layout
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
}Example Code
Build a 2-column grid with gap spacing
<!DOCTYPE html>
<html><head><style>
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.item {
background: #e5e7eb;
padding: 15px;
border-radius: 5px;
}
</style></head><body>
<div class="grid">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
</body></html>Expected Output:
Item 1
Study Tips
- •Read the theory content thoroughly before practicing
- •Review the example code to understand key concepts
- •Proceed to the Practice tab when you're ready to code