Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials CSS CSS Grid
CSS Beginner FREE

CSS Grid

Lesson 2 of 16 Beginner Interactive

CSS Grid is a two-dimensional layout system for creating complex page layouts with rows and columns.

Container Properties

  • display: grid — enables grid layout
  • grid-template-columns — define column tracks: 1fr 2fr, repeat(3, 1fr)
  • grid-template-rows — define row tracks
  • gap — space between grid items
  • grid-area — name areas for placement

Item Properties

  • grid-column — which columns to span
  • grid-row — which rows to span
  • grid-area — place into named area

The fr Unit

fr (fraction) distributes available space proportionally. 1fr 2fr gives the second column twice the space of the first.

Syntax

CSS
/* Grid Container */
.grid {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
    grid-template-rows: auto;
    gap: 16px;
}

/* Auto-fill responsive grid */
.auto-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 20px;
}

/* Grid Areas */
.layout {
    display: grid;
    grid-template-areas:
        "header header"
        "sidebar main"
        "footer footer";
    grid-template-columns: 200px 1fr;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }
CSS Grid Layout
HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>CSS Grid Layout</title>
    <style>
        .container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 12px;
        }
        .item {
            background: #7c3aed;
            color: #fff;
            padding: 16px;
            text-align: center;
        }
        .featured {
            grid-column: span 2;
            background: #10b981;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">1</div>
        <div class="item featured">featured — grid-column: span 2</div>
        <div class="item">3</div>
        <div class="item">4</div>
    </div>
</body>
</html>

Practice

1
Exercise

Create a 3-column responsive grid that collapses to 1 column on small screens.

Answer
.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 20px;
}
2
Exercise

Build a page layout with header, sidebar, main content, and footer using grid-template-areas.

Answer
.layout {
    display: grid;
    grid-template-areas:
        "header header"
        "sidebar main"
        "footer footer";
    grid-template-columns: 200px 1fr;
}

Quick Quiz

1

What does the fr unit represent in CSS Grid?

fr stands for fraction — it distributes available space proportionally among grid tracks.

2

Which property creates a responsive grid without media queries?

repeat(auto-fill, minmax(250px, 1fr)) automatically adjusts the number of columns based on available space.

Interview Questions

auto-fill creates as many tracks as fit in the container, even if empty. auto-fit collapses empty tracks to zero width, expanding filled tracks to fill the space. Use auto-fit when you want items to stretch to fill the container. Use auto-fill when you want to keep empty track slots.

Grid is two-dimensional (rows and columns simultaneously), while Flexbox is one-dimensional (row OR column). Grid is ideal for page-level layouts and complex 2D structures. Flexbox excels at component-level layouts, navigation bars, and aligning items in a single direction. They complement each other and are often used together.