Skip to main content
CSS for Beginners
CHAPTER 08 Beginner

CSS Grid Complete Guide

Updated: May 10, 2026
5 min read

# CSS Grid Complete Guide

1. Introduction

While Flexbox is a one-dimensional system (rows OR columns), CSS Grid is a two-dimensional system. It allows you to design complex layouts using both rows AND columns simultaneously. It is the most powerful layout system available in CSS.

2. Learning Objectives

  • Define a grid container and grid tracks.
  • Place items perfectly across multiple rows and columns.
  • Use fr units to divide space proportionally.
  • Build responsive grids using auto-fit and minmax().

3. Detailed Explanations

Activating CSS Grid

Apply display: grid to a container. By default, it will just stack items, so we need to define columns.
css
123456
.container {
    display: grid;
    /* Creates 3 columns, each 200px wide */
    grid-template-columns: 200px 200px 200px;
    gap: 20px;
}

The Magic of fr Units

fr stands for "fraction". It distributes *available* space.
css
12345
.container {
    display: grid;
    /* First col is 1 fraction, second is 2 fractions (twice as wide) */
    grid-template-columns: 1fr 2fr;
}

Grid Template Areas

You can name sections of your grid to create highly readable, structural layouts like a dashboard or blog.
css
1234567891011121314
.layout {
    display: grid;
    grid-template-areas:
        "header header"
        "sidebar content"
        "footer footer";
    grid-template-columns: 250px 1fr;
}

/* Assign elements to those named areas */
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer { grid-area: footer; }

Spanning Rows and Columns

You can force a child item to stretch across multiple lines.
css
1234567
.wide-item {
    grid-column: span 2; /* Stretches across 2 columns */
}

.tall-item {
    grid-row: span 3; /* Stretches down 3 rows */
}
The most famous CSS Grid trick is creating a responsive gallery without writing *any* media queries!
html
123456
<div class="gallery">
    <div class="photo">1</div>
    <div class="photo">2</div>
    <div class="photo">3</div>
    <div class="photo">4</div>
</div>
css
12345678910111213141516171819202122
.gallery {
    display: grid;
    /* 
      auto-fit: Add as many columns as will fit.
      minmax(250px, 1fr): Each column must be at least 250px, but can grow.
    */
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 15px;
    padding: 15px;
}

.photo {
    height: 250px;
    background-color: #e2e8f0;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    font-weight: bold;
    color: #94a3b8;
}

5. MCQs

Q1: Which CSS Grid unit distributes a fraction of the available space? A) px B) % C) em D) fr *Answer: D*

6. Summary

CSS Grid is the ultimate tool for page structure. Use it for your high-level layouts (Header, Sidebar, Main, Footer) and complex, irregular image galleries. Remember: Grid for 2D structure, Flexbox for 1D alignment!

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·