Things I’ve Learned About CSS Grid Layout

The following is a guest post by Oliver Williams. Oliver has been working with CSS grid layout and has learned quite a bit along the way. In this article he’s going to hop around to different concepts that he’s learned on that journey. I like this idea of learning little bite-sized chunks about grid layout, through isolated examples where possible. That makes learning the whole thing a bit less intimidating.

CSS grid looks set to land in browsers in early 2017. Until then you will need to enable grid for it to work (except in Firefox Nightly where it is enabled by default). Chrome Canary currently has the best implementation. Meanwhile, Firefox has an invaluable add-on called CSS Grid Inspector, which can display the lines of your grid. It is currently the only browser to have such a tool.

In Chrome enter `chrome://flags` into your address bar, find ‘Enable experimental Web Platform features’ and click enable. IE and Edge have old implementations different from the current spec, and are therefore not recommended for experimenting with grid at this point in time.

You can’t have tetris-shaped grid areas

You’ll work this out pretty quickly for yourself.

Your grid areas can only be rectangles (like the left), not arbitrary polygons (like the right).

Grid is designed to be used with flexbox, not instead of it

Grid and flexbox can act in similar ways. You may have seen people using flexbox to construct grid systems but that’s not what flexbox was designed for. It’s worth reading Jake Archibald’s blog post Don’t use flexbox for overall page layout.

Flexbox is essentially for laying out items in a single dimension – in a row OR a column. Grid is for layout of items in two dimensions – rows AND columns.

They can be combined as well. You can turn a grid item into a flex container. You can turn a flex item into a grid.

Let’s take one useful example. We want to vertically center the text inside a grid item but we want the background (whether a color, a gradient or an image) to cover the items entire grid area. We can use the align-items property with a value of center but now the background doesn’t fill the whole area of the item. The default for align-items is stretch — as soon as you set it to any other value, it no longer fills the space. Let’s instead leave it set to stretch and turn our grid item into a flex container.

Using negative line numbers can be really helpful

Imagine a current CSS grid framework with a 12 column grid. At small screens, rather than reducing the number of columns, the content is told to span all twelve columns, giving the impression of being a single full-width column.

There are likely some elements we want to span across the whole viewport for all screen sizes, like perhaps the header, footer, or some hero images.

For small screens we could write:

.wide {
grid-column: 1 / 3; /* start at 1, end at 3 */
}

However, once we make our media query these elements will cover only the first two columns of twelve columns. We could include the new desired grid-column-end value of 13 in the same media query but there’s a far easier method. Just set the end value to -1 and it will span all columns, however many there happen to be. Like:

Grid areas create implicit line names

Named grid template areas and grid line numbers are two ways of placing content on the grid but you can use both placement methods in the same grid. You can also make use of the implicit line names that are created whenever you designate a grid-area.

.grid {
grid-template-areas: "main main sidebar sidebar";
}

With this code we just created four implicit line names: main-start, main-end, sidebar-start, and sidebar-end.

This could be useful if you want to overlap content either over several grid areas or in a particular subsection of one grid area.

This syntax can become somewhat unwieldy if you have a lot of empty space in your design (Too many periods! Periods, in lieu of a name, signify an empty cell.) You may not know that there is another way to define grid areas. We can name grid lines whatever we like. However, if we follow the naming convention [name-start] and [name-end] we can create grid areas. Here’s an example:

You probably wouldn’t lay out a whole page of content with this method, but if you want to combine grid-area placement with line number based placement, it’s nice to know about.

Use vmin for an equal sized box layout

CSS grid allows you to use any size unit for your rows and columns. Want equally-sized boxes but also want them to be responsive? If you want content that resizes with the container, you can use viewport units.

This would work perfectly well on desktop and laptop but on mobile phones where the height of the screen exceeds the width, our content will spill over creating a horizontal scroll bar. Dudley Storey recently wrote a blog post about the usefulness of a lesser-known css unit: vmin. This unit will be a percentage of the viewport width on a portrait-oriented screen and a percentage of the viewport height on a landscape-oriented screen.

Absolute positioning

When you absolutely position a grid item, rather than its positioning context being its container (i.e. the entire grid) we can position it in relation to its specified grid-column and grid-row start and end lines. As usual, position: absolute removes an element from the flow of the document (i.e. it is ignored by other elements). This makes absolute positioning useful if you want to overlap grid items without disrupting the grids auto-placement algorithm. Auto-placement goes out of its way not to overlap items unless you explicitly declare both a grid-column-start and grid-row-start value for every item.

Try deleting position: absolute; from the div in this example and think about how many items you’d have to define grid-column and grid-row properties for!

Order works differently from how you might think

If you’ve used the flexbox order property, then you already have this covered. All grid items have a default order value of 0. So order: 1; applied to a grid item would make after everything else, not before.

Unfortunately that isn’t going to work. If max is less in value than min, it is ignored. The fr is invalid as a minimum value in minmax(). However, it’s a very easy behaviour to achieve. The word auto, when used as a value in grid-template-columns or grid-template-rows, will make the column or row as large as its content.

Things are way easier if you name your grid lines

The easiest naming convention makes use of the grids auto-numbering. We don’t have to type [col2], we can just type the name col followed by a number.

.griditem1 {
grid-column-start: col 2;
}

Combined with the span keyword we can stop thinking about line numbers and start thinking in terms of the first column we want our content to fill and how many columns we want to take up, which is far more intuitive.

The fr unit removes the need for math

Let’s say we want to split our grid into four equal columns. Its easy to work out the percentages. We just write grid-template-columns: 25% 25% 25% 25%.

What if we then want to make use of the grid-gap property? grid-gap: 10px. We have three gaps between our columns, so our grid width will now be 100% + 30px and unwanted horizontal scrolling will appear with some content overspilling the screen on the right side. We could make use of calc to solve our problem, but using the fr unit is far easier: grid-template-columns: 1fr 1fr 1fr 1fr.

The second thing I hate about grid

There is no way to force the auto-placement algorithm to leave some columns or rows empty

The grid-gap values provides an easy way for spacing our content. While we can specify a separate value to divide our columns and our rows with grid-row-gap and grid-column-gap, these values must be uniform. If we want to separate our first and second row by ten pixels and our second and third row by 50 pixels, grid offers us no way of doing this, other than by creating rows and keeping them empty.
You may have seen the periods/dots in the grid-template-area syntax:

This could have provided a simple way to control the auto-placement algorithm to stop it from placing items in these areas. Unfortunately it doesn’t work like that: this syntax simply denotes that we don’t want to turn the third row into a named grid-area. Auto-placed items will still end up there.

Twelve columns is the default of web design. The Bootstrap grid uses 12 columns and just about every other grid framework currently out there. There is a good reason for this: twelve is divisible by both three and four, giving more flexibility in how we can lay out content across the page. We can divide our content evenly into 12 parts, 6 parts, 4 parts, 3 parts, or in half.

While some people like the familiarity that comes with consistently using the same grid for every project, there is no need to have more columns than you actually need and you should build the grid that’s right for your content and desired layout rather than a one-size-fits all set-up.

Take a look at the examples on Gridset. Gridset is a useful tool for making grids, but once the native CSS grid module lands you won’t need tools like this. It does showcase some excellently-designed grids.