One of the best ways for anyone to learn a topic is to teach that topic. To that end, I’m giving a presentation for my local front-end development meetup this week. In preparation, I’m writing this CSS cheat sheet / blog.

Before we start, let’s recite the CSS Grid Cheat Sheet Creed.

This is my CSS Grid cheat sheet. There are many like it, but this one is mine. My cheat sheet is my best friend. It is my life. I must master it as I must master my life. Without me, my cheat sheet is useless. Without my cheat sheet, I am useless…

My Perspective

I’ve been doing web development for a long time (since 2001), and am familiar with most of the web’s underlying technologies.

I’m a big believer in not adding unneeded complexity, so I typically shy away from traditional grid systems such as Susy, Singularity, and Bootstrap Grid — unless the design is very grid centric.

My traditional approach to layout has been set the width to a percentage value — lets say 94% , and then set the max-width of the container to something like 1200px or whatever the design calls for. Then I can layout the internal structure through flexbox or floats, using a combination of percentages, pixels, and the CSS calc() function.

The beauty of CSS Grid is that it allows us the control that traditional grid systems have, but with no added libraries, CSS files, Sass mixins, or complexity.

Tips on getting started with grid

CSS Grid is pretty easy once you get started with it, but just like CSS, it gets more complicated the deeper you dive. The good news is that you don’t need to dive very deep to reach some awesome power. For what it’s worth, I think Grid is easier to learn than was flexbox.

As with any new syntax, I highly recommend having (or better yet, creating) a cheat sheet for quick reference. Although I created this blog post, there are many other great resources.

I also highly recommend Rachel Andrew’s website, Grid by Example, which has a plethora of common use cases, and an excellent free video series to get you up and running.

Now let’s get to the meat and potatoes of this post… the syntax.

Grid Container Syntax

First, we’ll start with the syntax of the grid container. The markup is pretty basic and looks like this:

< div class = " grid " > < div class = " grid-child " > </ div > < div class = " grid-child " > </ div > < div class = " grid-child " > </ div > < div class = " grid-child " > </ div > < div class = " grid-child " > </ div > </ div >

Initialize CSS Grid

We have to initialize the grid in order to use all of these awesome grid properties.

.grid { display : grid ; }

Set up columns

This sets up three 200px columns.

.grid { grid-template-columns : 200px 200px 200px ; }

Set up rows

Similarly, grid-template-rows sets up your rows.

.grid { grid-template-rows : 200px 200px 200px ; grid-template-rows : auto auto auto ; }

Shortcut syntax for setting up columns and rows

The following will set up four 50px rows and three 200px columns.

.grid { display : grid ; grid-template : 50px 50px 50px 50px / 200px 200px 200px ; }

Repeat syntax

You can accomplish the same thing by using repeat syntax. This also creates three 200px columns.

.grid { display : grid ; grid-template-columns : repeat ( 3, 200px ) ; }

You can mix and match the regular syntax and repeat syntax. This example creates one 300px column, and three 200px columns.

.grid { display : grid ; grid-template-columns : 300px repeat ( 3, 200px ) ; }

Grid Gap

Instead of margins, you specify the gap between grid children with the grid-gap , grid-row-gap , and grid-column-gap properties.

.grid { display : grid ; grid-gap : 20px ; grid-gap : 20px 40px ; grid-row-gap : 2vw ; grid-column-gap : 10% ; }

MinMax syntax

minmax allows you to set a minimum and maximum width of your columns. Doing so helps you avoid using media queries. You can use minmax inside of repeat .

.grid { display : grid ; grid-template-columns : 200px repeat ( 3, minmax ( 100px, 200px ) ) ; }

FR unit

The “fr” in the fr unit stands for fraction. Grid will create an amount of identical width units equal to the sum of all the fr units in the row, and then it will assign the width of each column based on how many fr units are assigned to it.

So, if I have a three columns that each have 1fr width, they will each be 33.33% wide. If they each have 2fr , their width will be the same because they’re still all equal to each other.

If the first column is 2fr and the next two columns are each 1fr , then the first column will take up 50%, and the next two will each take up 25%.

.grid { display : grid ; grid-template-columns : repeat ( 3, 1fr ) ; grid-template-columns : repeat ( 3, 2fr ) ; grid-template-columns : 2fr repeat ( 2, 1fr ) ; }

Auto-fill property

auto-fill automatically adds columns to the grid layout according to the container width. The example below will create 5 200px columns. Note that these columns can be empty if elements don’t exist to fill them in.

.grid { display : grid ; width : 1000px ; grid-template-columns : repeat ( auto-fill, 200px ) ; }

Auto-fill + minmax + fr units = Awesome!

The syntax below will automatically create as many 200px columns in the row as possible. But, once it cannot fit any more in, it’ll stretch each column out equally to fill out the entire width of the container! 😎

.grid { display : grid ; grid-template-columns : repeat ( auto-fill, minmax ( 200px, 1fr ) ) ; }

Auto-fit + minmax + fr units = Awesome!

Auto-FILL is different than auto-FIT. Auto-fill will automatically create as many grid tracks as possible (includeing empty grid tracks). Auto-fit will only create as many grid tracks as there us child items.

So the following HTML with the following CSS will generate a 3 box layout (while the code above will generate as 5 columns for a 1000px container).

< style > .grid { display : grid ; grid-template-columns : repeat ( auto-fit, minmax ( 200px, 1fr ) ) ; } </ style > < div class = " grid " > < p > lorem </ p > < p > ipsum </ p > < p > don't know the rest </ p > </ div >

Grid-auto-rows and grid-auto-columns

grid-auto-rows and grid-auto-columns specify the height (track size) of the auto-generated columns and rows. This can be in in any unit including fr . The initial value for these properties is auto , which sets the height equal to the largest content item.

.grid { display : grid ; grid-auto-rows : 30px ; }

Masonry layout using grid

If you have grid elements that are taking up multiple columns and/or rows, you can encounter situations where “holes” in the grid appear.

You can fix this by using grid-auto-flow: dense; . Note that this will cause the elements to appear out of the original source order.

.grid { display : grid ; grid-auto-flow : dense ; }

Grid Child Syntax

The following properties apply to children of grid containers.

Align tracks within a grid

A track is either a column or a row. What if the grid is wider or higher than your grid tracks? How do you control where the tracks go?

You can use our old flexbox friends justify-content and align-content ! The values to use are start , center , and end .

.grid { display : grid ; width : 1000px ; } .grid-child { grid-template-columns : repeat ( 3, 300px ) ; justify-content : end ; align-items : end ; }

You can also use our flexbox friends space-between and space-around .

.grid-child { justify-content : space-between ; }

Start a child on a specific column

This grid child will start on the second column.

.grid-child { grid-column-start : 2 ; }

Have a child span multiple columns

The following rule will have the child take up two columns

.grid-child { grid-column-end : span 2 ; grid-column-start : 2 ; grid-column-end : span 2 ; grid-column-start : 2 ; grid-column-end : 4 ; grid-column : 2 / 4 ; grid-column : 2 / span 2 ; }

Have a child span multiple rows

Similarly, the following rule will have the child take up two rows

.grid-child { grid-row-end : span 2 ; }

Always put a child into a certain row

I can see this being is useful for advertisements.

Note that you can reference rows from the end by using negative integers. So grid-row: -2 will reference the second to last row.

.grid-child { grid-row : 2 ; }

Have child element take up the full width of the row

The following snippet will place the .grid-child element into the third row and have it span the entire width of the row.

Note that I’m setting grid-column-end to -1 . Negative numbers selects the columns (or rows) from the end. So -1 will always select the last column (or row).

.grid-child { grid-row : 3 ; grid-column-start : 1 ; grid-column-end : -1 ; grid-column : 1 / -1 ; }

Place a child in a certain column and have it span multiple columns

.grid-child { grid-column : 2 / span 2 ; }

Helpful syntax

Because this is web development, there are many approaches to accomplish the same thing. Here are some cool helpful syntax types.

Named lines

You can use names instead of numbers for your lines. Note that you’re not naming your regions, you’re naming the lines around the regions. Then when you assign a child element, you assign it by name instead of number.

.grid { display : grid ; grid-template-columns : 1fr [content-start] 3fr [content-end] ; grid-template-rows : auto [content-start] auto [content-end] ; } .grid-child { grid-column : content-start ; grid-row : content_start ; }

Named lines with media queries == 🤘

The cool thing about named lines is that it makes responsive web design much simpler. When defining placement on the .grid-child , you do not need to add media queries for this element — it will automatically follow the named lines.

.grid { grid-template-columns : [content-start] 1fr [content-end] ; } @media ( min-width : 700px ) { .grid { grid-template-columns : 1fr [content-start] 3fr [content-end] ; } } .grid-child { grid-column : content-start ; }

Named Areas

You can name your grid children, and then place them ascii-art style within a new CSS property called grid-template-areas . In the example below, I’m using semantic element types to illustrate where we want the content to be placed.

header { grid-area : header ; } aside { grid-area : sidebar ; } main { grid-area : content ; } footer { grid-area : footer ; }

Once the areas are named, we line em up in an ascii-art like grid.

.grid { display : grid ; grid-template-areas : 'header' 'content' 'sidebar' 'footer' ; } @media ( min-width : 700px ) { .grid { grid-template-areas : 'header header' 'sidebar content' 'footer footer' ; } } @media ( min-width : 1000px ) { .grid { grid-template-areas : 'sidebar header header' 'sidebar content content' '. footer footer' ; } }

Magic Lines

When defining named areas, CSS Grid will automatically generate -start and -end lines for each region!

.grid { grid-template-areas : 'sidebar header header' 'sidebar content content' '. footer footer' ; }

So for the grid above, the browser will automatically generate sidebar-start , sidebar-end , header-start , header-end , content-start , content-end , footer-start , and footer-end named lines!

Pretty neat!

This also works in reverse, where if you name your lines with the pattern of <region>-start and <region>-end, it will automatically name the region area!

Grid-template shorthand

We already know that we can define your track widths/heights with shortcut syntax in the grid-template area:

.grid { display : grid ; grid-template : 50px 50px 50px 50px / 200px 200px 200px ; }

But we can also define your grid areas and column widths / row heights all in one property.

.grid { display : grid ; grid-template : 'head head head head' 200px 'side1 main main side2' 300px / repeat ( 3, 1fr ) ; }

This syntax is confusing as hell if you haven’t seen it.

What’s going on is the named areas are being mapped to the grid template. After the named areas are placed, the track widths are defined.

So on the first line, after the head area is set to span 4 columns, the row height is set to 200px . On the second line, the side1 , main , and side2 regions are placed. After that, the row height is set to 300px , and the columns are set to three 1fr columns.

For more information, see this video from Rachel Andrew’s Grid by Example series:

Developer Tooling for CSS Grid

Both Chrome and Firefox have developer tools support for CSS Grid. When hovering your mouse above the grid container, both browser’s developer tools will highlight the track and the lines of the grid. Firefox’s developer tools have a leg up, though, because you can toggle on and off the highlighting by clicking on a grid icon that appears next to the display: grid; declaration. With Chrome’s developer tools, you have to keep your mouse highlighted above the element in order to view the tracks and lines.

Firefox's Grid Developer Tools showing an example from Grid by Example

That being said, the developer tools could be better. When hovering the mouse above named areas, it would be nice if the areas would be highlighted. The same goes for named lines — hovering over the named lines declaration should highlight the appropriate line. I filed some feature requests for both Chrome and Firefox to get these features implemented.

In addition when using named areas, the entire value is placed on one line — totally negating the ascii-art matrix where you lined up the grid. DevTools should preserve line breaks and white space. I also filed featured requests these at: Chrome and Firefox.

Furthermore, it would be really useful if the browsers integrated a grid builder, similar to what they have for bezier-curve editor, or color-picker within devtools.

Should I be using CSS Grid now?

As long as you’re not supporting Internet Explorer 11, then the answer is a resounding… sure! Whether to support IE11 is a decision that your team has to make after looking at your website’s visitor logs.

In the United States, over 85% of browsers support Grid, although your website’s demographic might be dramatically different. Keep in mind the percentage of browsers supporting Grid is only going to continue to increase as time goes on.

Should I rewrite my current application to use CSS Grid?

If you’re current web app is working perfectly, should you rewrite?

The benefits of doing so would be a simpler code base — you’re able to rip out your current grid system, and replace your current layout system(s).

The downsides of rewriting are lesser browser support, the probability of introduced bugs, not to mention the hours spent for the rewriting and for the quality control.

In my opinion, if it ain’t broke, don’t fix it. But, only you know your web app, and can make the correct decision.

Want to get started? Below are some CodePen links to play with!

Thanks to Wes Ruvalcaba and John Tucker for helping to review this post.