As of the release of Microsoft Edge 16, the latest versions of all the major browsers now support CSS Grid. Now is definitely the time to learn Grid.
Not only is CSS Grid a great way to save time during development (and give front end developers some new tools to play with), it also allows web designers to think outside the box. Though grid may be made of “boxes” it brings us closer to being able to move away from the box style of web development we’ve been using since the 90s and push the envelope more.
Even if you have to support older browsers it is still possible to use it and add fallback code for older browsers. The Grid by Example site by Rachel Andrews (A CSS Working Group member who worked on the official spec for grid) is a great resource and includes examples of how to support older browsers as well.
Building a simple grid layout with CSS Grid
The markup and styling for a basic three column grid are as follows:
The grid that is made in this example is only due to the properties used on the .grid class:
This property is to make the element a grid element and its child elements grid children.
grid-template-columns: 1fr 1fr 1fr;
The grid-template-columns property lets you define how many columns your grid should have and how wide they are. With grid you can use a new unit called fr (fractional unit). In this example with three 1fr units we are setting each grid item to take up the same amount of space and have three columns per line. You can also use fr to make each column a different width. For example 1fr 1fr 2fr would make the first two columns the same size and make the third column double the size of the first two columns. You can also use other CSS units for this property and mix them. For example 100px 200px 1fr would have the first column at 100 pixels, the second column at 200px and the third column would take up the remainder of space.
Grid gap allows you to set space between each column and row (grid-column-gap and grid-row-gap). It is much easier to use than floats or flexbox because it does not put unwanted padding around the outside of the grid.
Using grid functions to make a responsive grid with ease
When building a responsive grid with flexbox or floats you generally need a lot of breakpoints. Grid doesn’t take away the need for breakpoints completely but, using some more complex grid features, it can greatly reduce the amount needed.
See the Pen CSS Grids - Multi Size Grid Responsive No Breakpoints! by Kristy Yeaton (@kristyjy) on CodePen.
The minmax and the repeat functions are what eliminates the need to write media queries and still be able to change the number of columns depending on screen size.
The repeat function takes two arguments; how many times to repeat, and the sizes to be repeated. The above example uses the keyword “auto-fit” which means it should fit as many columns as it can in the row, and if all items are on the first row those items will grow to fit the full browser window width. There is a similar keyword called “auto-fill” that works the same way except it will not increase the size of the columns if they fill the width of the browser window. Try changing the value in the codepen example above to see it in action.
We are using the minmax function as the second argument in our repeat function. This allows us to tell the browser the width of the grid item should always be between the sizes of the first and second argument.
The 2-dimensional grid
The reason flexbox doesn’t work the way we want here is because it is considered a one dimensional grid solution. CSS Grid on the other hand is a two dimensional grid and you can have a single item span multiple rows and columns.
The key rules for this layout are as follows:
The grid auto flow rule allows us to tell the browser how to automatically align the grid’s children. The dense option will try to fill in any white space it finds by changing the source order. (Try changing the auto flow to row on the example above)
The auto flow rows property allows us to set a default value for row height. This is very important for this kind of layout because not being able to use 1fr to keep them all the same height can cause issues. This grid is simple enough so you shouldn’t see a difference when removing it but with more complex layouts this makes a huge difference.
grid-row-end + grid-column-end
These properties allow us to assign a grid item to multiple rows and/or columns. For example the smallest image in the example above does not have these properties and with default to spanning 1 row and 1 column. The second size spans 1 column but 2 rows so needs the css rule: grid-row-end: 2 span;
Pushing our layouts further
Images can be a bit of a challenge to work with for the style of grid used above. It’s important to make sure each image you plan to use has a somewhat large highest common denominator. In this case I kept it pretty simple and used various combinations of 400px and 800px sized images. You can go much more complex and build something like our team page but know it can be a bit of a challenge to not end up with unwanted white space.
The reason we need to be so specific with our math here is even if our result may not look like a grid we still need to use an underlying grid to lay out everything. This is where the highest common denominator comes in. For example our team page is built on 4 columns but with many small rows inside of each grid item. All of the rows for each item need to be able to be the same size to remain a grid and using the highest common denominator for our image heights allow us to determine how many rows should be inside of each item.
This screenshot was taken from Firefox using their grid inspector tools. This allows us to see how the underlying grid is laid out and is very helpful for debugging issues with your grid CSS. It also really shows how the grid-auto-rows property is a lifesaver for this kind of grid.
When removing only the grid-auto-rows property this was the result. Notice how each row is a different height so we are unable to reliably predict where each item will start or begin. It also causes the large block of white space under Justyna’s title.
Yes we can
So give CSS Grid a try. I think you’ll find that the days of having to tell a designer “no the web can’t do that” are growing short. I’m definitely looking forward to seeing the innovation modern CSS properties like grid can bring.