Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Specify many grid properties at once with the grid shorthand

    Rory SmithRory Smith

    Using the grid shorthand property, we can set all of the explicit grid properties (grid-template-rows, grid-template-columns, and grid-template-areas), all the implicit grid properties (grid-auto-rows, grid-auto-columns, and grid-auto-flow), and the gutter properties (grid-column-gap and grid-row-gap) in a single declaration.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    In this example, we have 20 grid items in our container. The container is display grid taking up all the available height and a 20 pixel gutter. Let's specify some grid-template-rows and grid-template-columns for our grid.

    To do that, we're going to use the grid property, which is a shorthand property. We're going to explicitly declare two rows and two columns, and the remaining rows and columns will be implicitly set.

    The first row will be 3fr, the second will be 14fr. The first column will be 4fr, the second column will be 2fr. There we have our grid-template-rows and grid-template-columns.

    This is the first row, which is 3fr, and this is the second row, which is 14fr. This is the first column, which is 4fr, and the second column 2fr. Using the grid shorthand property in this way allows us to specify grid-template-rows and grid-template-columns in one declaration.

    We can do more there as well. All our rows here are being implicitly set because we've only been explicit about the first two rows. Let's have it so that our grid-auto-flows. Now using auto-flow, we're actually setting grid-auto-rows.

    Now we've got 3fr, 14fr, 3fr, 14fr, etc. We could do the same thing for our columns. Now, we have two explicit rows 3fr and 14fr. Our columns are auto flowing, 4fr, 2fr, etc.

    If we specify auto-flow for either of the two things that we're passing to grid, then it sets them as grid-auto-rows or grid-auto-columns. It will also set grid-auto-flow to either row or column depending on where we place it in the grid shorthand.

    If we inspect this, now we can see our grid-template-rows set, and our grid-auto-columns set. The grid-auto-flow property is set to column. Now the items in our grid are rendering in a column direction instead of the default row, the grid-template-columns are set to initial.

    If we removed auto-flow, then we'd have grid-template-rows and grid-template-columns back. Therefore, grid-auto-columns and grid-auto-flow go back to initial. We can also use the dense keyword for auto-flow using this grid shorthand property.

    To demonstrate that, let's target one of our grid items here. We'll tell it to span three rows. Now our second grid item is spanning three rows. We'll set auto-flow so that we have some grid-auto-columns.

    We notice that there's a gap here. If we wanted to fill the gap with grid items that come later we could use dense. Now, the gap is over here because we're filling this space with items that fit inside it. An important thing to note about this grid shorthand property is that it initializes some properties for us. Two of those are grid-column gap and grid-row gap, which were our gutter properties.

    If we move this up above the grid property, then we wouldn't get our gutter, because it's being set to initial by the grid shorthand. The same can be set for some other values here unless we override them ourselves with the values that we set using the grid shorthand.

    Having said that, we can't actually set grid-column gap, grid-row gap, or grid-template areas to anything else using the grid property. It will simply set them all to initial.