Have 30-200 Employees? Make $50k-$500k selling your data for AI Training.

Learn More
A Beginner’s Guide to CSS Grid Layout
SitePoint Premium
Stay Relevant and Grow Your Career in Tech
  • Premium Results
  • Publish articles on SitePoint
  • Daily curated jobs
  • Learning Paths
  • Discounts to dev tools
Start Free Trial

7 Day Free Trial. Cancel Anytime.

The CSS Grid Layout Module has revolutionized the way websites are built. It offers tools that allow for advanced layouts without the tricky hacks and inventive solutions of the past.

In this introduction to Grid, we’ll walk through the basics of how Grid layout works, and we’ll look at lots of simple examples of how to use it in practice.

Table of Contents
  1. Getting Started with Grid Layout
  2. Setting a Gap Between Grid Items
  3. Setting Up Grid Columns
  4. Organizing Grid Items into Rows and Columns
  5. Placing Grid Items on the Grid
  6. Placing Grid Items Using Named Grid Lines
  7. Placing Grid Items Using Named Grid Areas
  8. Using Media Queries with Grid Layout
  9. Changing the Display Order of Grid Items
  10. Responsive Grid Layout without Media Queries
  11. Overlapping Elements in a Grid
  12. Wrapping Up

Key Takeaways

  1. Introduction to CSS Grid Layout: The article provides a foundational understanding of CSS Grid as a powerful layout system in CSS, explaining how to transform elements into a grid framework of rows and columns for structured content placement. It covers the basics of turning an element into a grid container and its direct children into grid items.

  2. Grid Structure and Item Placement: Essential features of CSS Grid are explored, including defining grid structures using properties like grid-template-columns, grid-template-rows, and grid-template-areas. The tutorial demonstrates spacing grid items with the gap property and organizing them into specific rows and columns for a precise layout.

  3. Responsive Design with CSS Grid: The adaptability of CSS Grid for responsive web design is highlighted, showcasing how media queries can be used to alter grid layouts for different screen sizes. Advanced techniques such as overlapping grid items and creating responsive layouts without media queries are also discussed, emphasizing the versatility of CSS Grid in modern web design.

Getting Started with Grid Layout

A grid is a framework of columns and rows into which we can place content. (It’s a bit like a table layout, but on steroids!)

Getting started with Grid is as simple as doing this:

.container {
  display: grid;
}

Now, all of the direct children of the .container element will be “grid items”. To start with, they’ll just appear as a bunch of rows in a single column, as shown in the demo below.

See the Pen
CSS Grid Basics: display: grid
by SitePoint (@SitePoint)
on CodePen.

In the example above, we have a

element that acts as the container. Inside it we have several elements, which are now grid items:

header
main
footer

So far, we haven’t achieved much, as we would get the same result without display: grid.

Further reading:

Setting a Gap Between Grid Items

Let’s first space our divs apart a bit with the gap property:

.container {
  display: grid;
  gap: 10px;
}

See the Pen
CSS Grid Basics: gap
by SitePoint (@SitePoint)
on CodePen.

The gap property inserts space between the elements vertically and horizontally, as we’ll see shortly. (We can set different horizontal and vertical gaps if we need to.)

Further reading:

Setting Up Grid Columns

Currently, we have an “implicit” grid — meaning that the browser is just figuring out a grid for itself, as we haven’t specified any rows or columns yet. By default, we just get one column and four rows — one for each grid item. Let’s now specify some columns:

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

With grid-template-columns, we’re specifying that we want four columns each with a width of 1fr, or one fraction of the available space. (Instead of 1fr 1fr 1fr 1fr we could write repeat(4, 1fr) using the very handy repeat() function.)

Now our grid items are laid out in one row, as shown below.

Further reading:

Organizing Grid Items into Rows and Columns

Now let’s organize our grid items into rows and columns, as we might see them on a standard web page layout.

Firstly, we’ll specify three rows with the grid-template-rows property:

.container {
  display: grid;
  gap: 10px;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: auto auto auto;
}

If we add that line to the Pen above, not much will happen yet, as we haven’t specified how we want our grid items to fit into these rows and columns. (Again note that auto auto auto could be written as repeat(3, auto) using the repeat() function.)

Further reading:

Placing Grid Items on the Grid

Our browser’s developer tools come in very handy for understanding CSS Grid layout. If we inspect our code so far, we can see the horizontal and vertical grid lines that define our grid, as pictured below.

Horizontal and vertical grid lines, numbered from 1 to 4 in both directions

There are five vertical grid lines and four horizontal grid lines, all of them numbered. We can use these grid lines to specify how we want our grid items laid out.

Let’s first set the

element to span the four columns and one row:

header {
  grid-column: 1 / 5;
  grid-row: 1;
}

This tells the

to start at the grid column line numbered 1 and end at the column line numbered 5.It also tells the
to start at the first grid row line. Because an end line isn’t specified, it just spans to the next row line, so grid-row: 1 is equivalent to grid-row: 1 / 2.

Let’s do something similar to the

:

footer {
  grid-column: 1 / 5;
  grid-row: 3 / 4;
}

The only difference here is that we’ve set the

to sit between grid row lines 3 and 4.

Now let’s position the

Ralph MasonRalph Mason

Ralph is a freelance copyeditor, web designer and teacher at Page Affairs.

© 2000 – 2026 SitePoint Pty. Ltd.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.