The Foundation: CSS Grid Container

Unlock true two-dimensional layout by mastering the core of CSS Grid: the container. Learn to define columns, rows, and gaps.

Lesson ProgressStep 1 of 8
1
2
3
4
0 EXP

Welcome! CSS Grid lets us build complex 2D layouts. It all starts by creating a 'grid container' with one simple property.

.container {
  /* Our items are stacked by default... */
}

The 'display: grid' Property

To begin building a grid layout, you must first designate an element as a grid container. This is the single most important step. You do this by setting its `display` property.

  • display: grid;: This turns the element into a block-level grid container. It will take up the full width available, like a `div`.
  • display: inline-grid;: This turns the element into an inline-level grid container. It will only take up as much width as its content, allowing text or other elements to flow around it.

Once you apply this property, the **direct children** of this element become grid items, and you can now position them on a grid. Without this declaration, all other `grid-*` properties will be ignored.

System Check

What is the key difference between `display: grid` and `display: inline-grid`?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏛️
Grid Architect

Define your first grid container using 'display: grid'.

📊
Column Commander

Create a flexible layout using 'grid-template-columns'.

Fr Master

Prove your mastery of the 'fr' unit for flexible layouts.

Mission: Build a Holy Grail Sidebar

Create a 3-column layout. The first and third columns should be 100px wide, and the middle column should take up the remaining space (1fr). Add a 20px gap.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Properties

While CSS order doesn't always matter for execution, there's a logical order for *thinking*. Drag the properties into the correct logical sequence.

grid-template-columns: 1fr 1fr;
display: grid;
gap: 10px;

Challenge: Complete the Syntax

Fill in the missing properties and values to create a 3-column flexible grid with spacing.

.container {
display:;
grid-template-columns:;
: 10px;
}

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "3-Column Layout" project for feedback from other Net-Runners.

Beyond the Basics: Mastering CSS Grid for Modern Layouts

You've learned the fundamentals: `display: grid`, `grid-template-columns`, and `grid-template-rows`. You can successfully build a simple grid. But the true power of CSS Grid lies in its more advanced features, which allow you to create responsive, maintainable, and complex layouts with surprisingly little code.

The most common question developers ask is, "When do I use Grid and when do I use Flexbox?" The simplest answer is: **Flexbox is for one-dimensional layouts** (a single row or a single column), while **Grid is for two-dimensional layouts** (rows *and* columns at the same time).

1. The `fr` Unit vs. Percentages: A Clear Winner

You might be tempted to use percentages to define columns, like `grid-template-columns: 25% 75%;`. This works, but it has a hidden flaw: **percentages don't account for `gap`**.

If you have `grid-template-columns: 50% 50%;` and add `gap: 20px;`, your container's total width will be `50% + 50% + 20px`, which is wider than 100% and will cause a horizontal overflow.

The **`fr` unit** is smarter. It represents a fraction of the **available space**. This means it calculates the space *after* accounting for gaps, padding, and fixed-width tracks.

`grid-template-columns: 1fr 1fr;` with `gap: 20px;` works perfectly. The browser calculates the total gap size, subtracts it from the container width, and divides the remaining space into two equal fractions.

2. Responsive Layouts (Without Media Queries!)

This is perhaps the most powerful feature of modern CSS Grid. You can create a fully responsive grid of items that reflows automatically based on the container width, all with a single line of code.

The magic combination is `repeat()`, `auto-fit` (or `auto-fill`), and `minmax()`.

.card-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1rem;
}

Let's break this down:

  • `repeat(auto-fit, ...)`: Tells the browser to fit as many columns as possible. `auto-fit` will collapse empty tracks to 0, allowing items to grow and fill the space.
  • `minmax(250px, 1fr)`: This is the column size. It says: "Make columns at least 250px wide. If there's extra space, distribute it equally (1fr) among them."

The result: On a wide screen, you might get 4 columns. On a tablet, you'll get 2. On a phone, you'll get 1. All without a single `@media` query.

3. Name Your Layout with `grid-template-areas`

For full-page layouts, `grid-template-areas` is incredibly intuitive. You "draw" your layout in your CSS.

✔️ The "Holy Grail" Layout

.container {
  display: grid;
  grid-template-columns: 150px 1fr 150px;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header header"
    "nav    main   ads"
    "footer footer footer";
  min-height: 100vh;
}

.header { grid-area: header; }
.nav    { grid-area: nav;    }
.main   { grid-area: main;   }
.ads    { grid-area: ads;    }
.footer { grid-area: footer; }

❌ Bad Practice

.container {
  grid-template-areas:
    "header header"
    "main   main   main"
    "footer footer";
}

This grid is invalid. All string rows in `grid-template-areas` must have the same number of columns.

Key Takeaway: Mastering `fr`, `repeat(auto-fit, minmax(...))`, and `grid-template-areas` elevates you from someone who *uses* Grid to someone who *architects* with Grid.

CSS Grid Glossary

Grid Container
The HTML element on which `display: grid` or `display: inline-grid` is applied. It's the parent of all grid items.
Grid Item
A direct child of the grid container. These are the elements that get placed into the grid.
Grid Line
The horizontal and vertical dividing lines that make up the grid's structure. They can be referenced by number or by name.
Grid Track
The space between two adjacent grid lines. A grid track is a column or a row.
Grid Cell
The smallest unit of the grid, defined by the intersection of a row and a column track.
Grid Area
A rectangular space made up of one or more grid cells, defined by four grid lines or a name (via `grid-template-areas`).
`grid-template-columns`
CSS property that defines the number and size of the columns in a grid container.
`grid-template-rows`
CSS property that defines the number and size of the rows in a grid container.
`fr` Unit
A "fractional" unit that represents a fraction of the available space in the grid container. It intelligently accounts for `gap`.
`gap` (or `grid-gap`)
A shorthand property for `row-gap` and `column-gap`, defining the size of the "gutters" or spacing *between* tracks.
`repeat()`
A CSS function used inside `grid-template-columns` or `grid-template-rows` to define a repeating pattern of tracks. Example: `repeat(3, 1fr)`.
`minmax()`
A CSS function that defines a size range, greater than or equal to a minimum and less than or equal to a maximum. Example: `minmax(100px, 1fr)`.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS Grid Layout specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!