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:
A few concepts are introduced with CSS Grid, and it’s a good idea to get familiar with the vocabulary:
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>
</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:
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:
👉 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.
Unfortunately Grid Layout is still not available at large. Here’s the current availability:
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!
To enable CSS Grid in Chrome, go to chrome://flags/, look for Experimental Web Platform features and enable it.
Grid layout is now supported out of the box with Firefox 52 and up.
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:
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.
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.
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!