Master CSS From Scratch

Clear, interactive, and structured CSS lessons designed for beginners.

CSS Grid

CSS Grid is a powerful layout system used to create two-dimensional web layouts using rows and columns.

Unlike Flexbox, which is primarily designed around one dimension at a time, CSS Grid allows developers to control both rows and columns together.

What Is CSS Grid?

CSS Grid provides a structured way to arrange elements into rows and columns.

A Grid layout consists of a Grid Container and Grid Items.

The Grid Container defines the overall layout, while its direct children become Grid Items.

Basic Grid Syntax

.container {
    display: grid;
}

Setting display: grid; turns an element into a Grid Container.

CSS Grid Structure

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

CSS Grid organizes elements into rows and columns.

Rows and Columns

A Grid layout is built using horizontal rows and vertical columns.

Rows

Rows arrange Grid Items horizontally from top to bottom.

Columns

Columns arrange Grid Items vertically from left to right.

Example 1: Three Equal Columns

.grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
}

The 1fr 1fr 1fr value creates three equal columns.

Output

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

Understanding the fr Unit

The fr unit represents a fraction of the available space inside the Grid Container.

.grid-container {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
}

The available space is divided into 4 fractions.

The first column receives 1 fraction, the second receives 2 fractions, and the third receives 1 fraction.

Therefore, the proportional layout is: 1 : 2 : 1.

Grid Gap

The gap property creates space between Grid Items.

.grid-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
}

gap: 20px; creates a 20-pixel gap between the Grid Items.

Using repeat()

The repeat() function makes repeated Grid column or row definitions easier to write.

.grid-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}

This is equivalent to:

.grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
}

Important CSS Grid Properties

Property Purpose
display Creates a Grid Container.
grid-template-columns Defines the Grid columns.
grid-template-rows Defines the Grid rows.
gap Creates spacing between Grid Items.
grid-column Controls the column position or span of a Grid Item.
grid-row Controls the row position or span of a Grid Item.

Grid Container and Grid Items

Grid Container

The parent element with display: grid; becomes the Grid Container.

Grid Items

The direct children of the Grid Container become Grid Items.

CSS Grid vs Flexbox

Feature Flexbox Grid
Layout One-dimensional Two-dimensional
Main Focus Row or column Rows and columns
Common Use Navigation and alignment Page and card layouts

Example 🤓🌍

CSS Grid is commonly used for dashboards, course cards, product galleries, image galleries, and complete webpage layouts.

.course-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
}

.course-card {
    padding: 20px;
    border: 1px solid #2563eb;
}

This creates a three-column course layout with 20 pixels of space between the cards.

Advantages of CSS Grid

Two-Dimensional Layout

Grid can control rows and columns together.

Responsive Design

Grid makes it easier to create layouts that adapt to screen sizes.

Precise Control

Developers can control the position and size of Grid Items precisely.

Important: CSS Grid is especially useful when a layout requires control over both rows and columns. Flexbox and Grid can also be used together in the same webpage.

Summary

CSS Grid is a two-dimensional layout system based on rows and columns. The parent element becomes a Grid Container using display: grid;. Properties such as grid-template-columns, grid-template-rows, gap, and the fr unit provide flexible control over modern web layouts.