This article was updated on 23rd March, 2017. Specifically: browser support for CSS Grid Layout.
In this article, we are going to learn seven ways in which you can place elements in a web page using the Grid Layout module.
Previously, SitePoint published Introducing the CSS Grid Layout. Recently, I also wrote Where Things Are at in the CSS Grid Layout Working Draft.
Here, the focus will be entirely on specific ways in which you can lay out elements on the web using CSS Grid. Now, let’s go over each one of them.
Addressing Browser Support for Grid Layout
At this time, Grid Layout doesn’t yet have consistent browser support. However, as of March 2017, both latest versions of Chrome and Firefox browsers have been released with CSS Grid Layout support by default. IE still supports the old implementation, Opera needs the Experimental Web Platform flag turned on, and Safari has no support at all. To properly work with all the examples in this article, I suggest you use either Chrome or Firefox. For readers who for some reason find it problematic to use these browsers, I have added a screenshot to show the final result of each technique.
#1 Specifying Everything in Individual Properties
This is the version we have been using to place the elements in our previous articles. This method is verbose but easy to understand. Basically, the left/right and top/bottom bounds of an element are specified using
grid-row-end properties. If an element is only going to span one row or column, you can omit the
-end properties, this way you will have to write a little less CSS.
In the demo below, element A has been placed in the second row and second column using the following CSS:
The same effect could be achieved by using:
Even though the CSS in our first example was readable and easy to understand, we had to use four different properties to place a single element. Instead of using four properties, we can just use two —
grid-row. Both these properties will take two values separated by a slash where the first value will determine the start line and the second value will determine the end line of our element.
Here is the syntax you need to use with these properties:
grid-row: row-start / row-end;
grid-column: col-start / col-end;
To place item C in the bottom right corner of our grid, we can use the following CSS:
grid-row: 2 / 4;
grid-column: 2 / 4;
Continue reading %Seven Ways You Can Place Elements Using CSS Grid Layout%