CSS Grid Layout: Introduction

Updated on April 4, 2022


CSS Grid Layout: Introduction

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Flexbox is great, but its main purpose is to help us with positioning elements in one dimension, either horizontal or vertical. For whole page, two-dimension layout, we’ve been mostly relying on frameworks like Bootstrap or Foundation to provide us with classes that we can use to create a grid layout. A new module, CSS Grid Layout, set to be available in browsers very soon, should change all of that.

Let’s explore CSS Grid with a very brief overview. First, a few key points to keep in mind:

  • Like with Flexbox, source order doesn’t matter with CSS Grid and items can easily be repositioned depending on the size of the viewport.
  • You can start using grid layout now and use feature detection with @supports so that it’s only applied in supporting browsers.
  • Some layouts that are impossible to accomplish even with the grid system of modern CSS frameworks will be possible with CSS Grid.


A few concepts are introduced with CSS Grid, and it’s a good idea to get familiar with the vocabulary:

  • Grid line: items are positioned on the grid with the lines where the item starts and ends, so grid lines are central to how CSS Grid works. Column grid lines are numbered from left to right starting with 1 and row grid lines are numbered from top to bottom starting with 1.

Grid Row Line 2

Grid Line

  • Grid track: The space between two grid lines.

Grid track

Grid track

  • Grid area: An area defined by any 4 grid lines.

Grid area

Grid area

  • Grid cell: The space between two consecutive column and row grid lines. The smallest unit possible on a grid.

Grid cell

Grid cell

Display: grid on the parent

A little bit like with flexbox, the grid is organized with a container element and children elements that become grid items. You simply set display: grid on the container element. The grid rows and columns are defined with grid-template-columns and grid-template-rows.

Let’s start with the following markup:

<div class="container">
  <div class="box box-1">Box 1</div>
  <div class="box box-2">Box 2</div>
  <div class="box box-3">Box 3</div>
  <div class="box box-4">Box 4</div>
  <div class="box box-5">Box 5</div>
  <div class="box box-6">Box 6</div>
  <div class="box box-7">Box 7</div>

Here’s how we can define the CSS for the container:

.container {
  display: grid;

  grid-template-columns: 150px 150px 80px;
  grid-template-rows: 100px auto;

  grid-gap: 10px 15px;

With this we already get something interesting, even without setting any properties on the grid items:

CSS grid example 1

Notice how we added 10px horizontal gaps and 15px vertical gaps between the cells using grid-gap.

Now we can go further by defining the start and end lines of specific items. Items that are not explicitly placed on the grid will be placed according to an algorithm. We use grid-column-start, grid-column-end, grid-row-start and grid-row-end to define where an item starts and ends on the grid:

.box-1 {
  grid-column-start: 1;
  grid-column-end: 3;

.box-3 {
  grid-column-start: 1;
  grid-column-end: 3;
  grid-row-start: 2;
  grid-row-end: 4;

Or we can use the grid-column and grid-row shorthands for the same result:

.box-1 {
  grid-column: 1 / 3;

.box-3 {
  grid-column: 1 / 3;
  grid-row: 2 / 4;

Here’s the resulting grid now:

CSS grid example 2

👉 Gaps don’t add new grid lines, so items that are next to each other are really touching the same line, even if a gap is separating them.

CSS Grid Layout today

Unfortunately Grid Layout is still not available at large. Here’s the current availability:

  • Supported by default in Firefox since version 52.
  • Fully supported in the latest Chrome and Opera behind a flag.
  • Supported in IE and Edge, but with an older syntax.
  • Available in the latest Safari Technology Preview.

Chrome should be shipping a version with Grid enabled by default in March 2017. So it’s coming really soon, and 2017 will most likely be the year of CSS Grid!

Enable in Chrome

To enable CSS Grid in Chrome, go to chrome://flags/, look for Experimental Web Platform features and enable it.

Enable in Firefox

Grid layout is now supported out of the box with Firefox 52 and up.

Learning More

We kept this post short and sweet, but there are many new concepts to learn with CSS Grid, so we split many of them into their own post:

Further reading

Here are a few great resources to learn the ins and outs of CSS Grid:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

About the authors
Default avatar


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel