CSS Grid Layout for Beginners

Frontend ~ 8 minutes

# Welcome to CSS Grid

Hello and welcome! In this tutorial, we will learn CSS Grid. Grid is a powerful tool for building page layouts. It lets us place items in rows and columns at the same time. Think of a chess board or a spreadsheet. That is how Grid sees the page.

Flexbox works in one direction. Grid works in two directions. So Grid is perfect for full page layouts, photo galleries and dashboards.

Do not worry if it looks new to you. We will build one small example, step by step.

What you will learn

  • What a grid container and grid items are
  • How to make columns and rows
  • How to use the fr unit and gap
  • How to place items and make the grid responsive

Let us begin by making a folder for our practice.

Create a practice folder
Code Copied to Clipboard

# Prepare the files

Great! Now, please open the grid-practice folder in your editor. Create two files: index.html and style.css.

As in Flexbox, Grid has a parent and children. The parent is the grid container. The children are the grid items. We turn on Grid on the parent, and then the children are placed on the grid lines.

In our HTML, we will make one container with six boxes inside. Six is a nice number, because it fits well in a grid of three columns and two rows.

Please write the HTML below. Do not forget to link your CSS file in the head.

The HTML file
Code Copied to Clipboard

# Make your first grid

Now we open style.css. To start a grid, we write display: grid; on the container. Then we describe the columns with grid-template-columns.

For example, grid-template-columns: 100px 100px 100px; makes three columns, each 100 pixels wide. But fixed sizes are not flexible. A better way is the fr unit. It means a fraction of the free space. So 1fr 1fr 1fr makes three equal columns, and they fill the full width.

We also use gap to put space between the boxes. It works for rows and columns together.

Helpful grid properties

  • display: grid turns on Grid
  • grid-template-columns sets the columns
  • grid-template-rows sets the rows
  • gap sets the space between cells

Please type this CSS and save the file.

Three equal columns
Code Copied to Clipboard

A shortcut with repeat

Writing 1fr 1fr 1fr is fine for three columns. But what about twelve columns? That is too long. CSS gives us a helper called repeat().

You can write repeat(3, 1fr) and it means the same thing as 1fr 1fr 1fr. The first number is how many times to repeat. The second part is the size.

You can also mix sizes. For example, 200px 1fr makes a fixed sidebar and a flexible main area. This is a very common page layout. For a full list of options, see the MDN Grid guide.

# Open the page

Let us see how it looks. Open your index.html file in the browser. You should see five blue boxes in a grid. Three are in the first row. Two are in the second row.

If you want a live preview server, you can use the command below. It needs Node.js. Please run it inside your project folder. Then open the address shown in the terminal.

If you do not have Node.js, please do not worry. You can simply double click the HTML file. After each change in your CSS, save the file and refresh the browser. You will see the layout update right away. Try changing the gap value, or try 2fr 1fr 1fr. You will see the first column grow wider. Small experiments like these teach you a lot.

Preview with a local server
Code Copied to Clipboard

# Place items and span cells

Sometimes, one item must be bigger than the others. For example, a page header should cover the full width. In Grid, we can make an item span many columns or rows.

The property grid-column: span 2; makes an item cover two columns. In the same way, grid-row: span 2; covers two rows. You can also use line numbers. Grid lines are numbered from 1. So grid-column: 1 / 4; means start at line 1 and end at line 4. That covers three columns.

We can also use grid-template-areas to name the parts of the page. It is like drawing your layout with words. Many people love this feature, because the CSS looks like the page.

Ways to place an item

  • grid-column: span 2 to cover two columns
  • grid-column: 1 / -1 to cover the full row
  • grid-area to use a named area

Let us make the first box cover the full width.

Span across columns
Code Copied to Clipboard

# Make the grid responsive

A good layout must work on phones too. Grid can do this without any media query. We use repeat(auto-fit, minmax(200px, 1fr)).

Let us read it slowly. The minmax(200px, 1fr) part says that each column must be at least 200 pixels wide, but it can grow to share the space. The auto-fit word tells the browser to fit as many columns as it can. On a wide screen, you may get four columns. On a small phone, you get just one.

This single line is one of the most useful tricks in CSS. It saves you from writing many rules for many screen sizes.

Why this pattern is great

  • It needs only one line of CSS
  • It works on every screen size
  • It keeps the boxes tidy and equal

Please replace your column rule with this new one and resize your browser window to see the magic.

A responsive grid
Code Copied to Clipboard
Mistakes to avoid

Please read these tips with care. They can save you from confusion.

First, remember that display: grid goes on the parent. The items do not need it. Only the direct children of the grid container become grid items. If a box is inside another box, it is not a grid item.

Second, do not mix up gap and margin. The gap property adds space only between cells. It does not add space on the outside edge. That is why we often add padding to the container too.

Third, grid lines start at 1, not 0. This surprises many beginners. Also, -1 means the last line. So 1 / -1 always covers the full width.

Quick checklist

  • Is display: grid on the parent?
  • Did you define the columns?
  • Are you using the fr unit for flexible sizes?
  • Do you know whether you need Grid or Flexbox?

A simple rule helps: use Flexbox for one direction, and Grid for two directions.

# Summary and next steps

Wonderful work! You have finished this tutorial. Let us remember the main ideas.

You learned that Grid works on a parent with display: grid. You made columns with grid-template-columns and used the flexible fr unit. You added gap, made items span many cells, and built a responsive layout with auto-fit and minmax.

What to do next

  • Build a full page layout with header, sidebar, main and footer
  • Try grid-template-areas to name your layout parts
  • Combine Grid and Flexbox in one project
  • Play the game Grid Garden

You can also read the official guide on MDN CSS Grid Layout. Practice a little every day. Soon, building layouts will feel easy and fun. Great job!