A few weeks ago I held a CSS Grid Layout workshop. Since I’m, like most of us, also pretty new to the topic, I learned a lot while preparing the slides and demos.
I decided to share some of the stuff that was particularly interesting to me, with you.
Have fun!
Negative values lower than -1 may be used for grid-row-end and grid-column-end
In a lot of code examples and tutorials you will see that you can use grid-column-start: 1
and grid-column-end: -1
(or the shorthand grid-column: 1 / -1
) to span an element from the first line to the last. My friend Max made me aware that it’s possible to use lower values than -1
as well.
.grid-item {
grid-column: 1 / -2;
}
For example, you can set grid-column: 1 / -2
to span the cells between the first and the second to last line.
See the Pen Grid item from first to second to last by Manuel Matuzovic (@matuzo) on CodePen.
It’s possible to use negative values in grid-column/row-start
Another interesting thing about negative values is that you can use them on grid-column/row-start
as well. The difference between positive and negative values is that with negative values the placement will come from the opposite side. If you set grid-column-start: -2
the item will be placed on the second to last line.
.item {
grid-column-start: -3;
grid-row: -2;
}
See the Pen Negative values in grid-column/row-start by Manuel Matuzovic (@matuzo) on CodePen.
Generated content pseudo-elements (::before and ::after) are treated as grid items
It may seem obvious that pseudo-elements generated with CSS become grid items if they’re within a grid container, but I wasn’t sure about that. So I created a quick demo to verify it. In the following Pen you can see that generated elements become grid- and flex-items if they are within a corresponding container.
See the Pen Experiment: Pseudo elements as grid items by Manuel Matuzovic (@matuzo) on CodePen.
Animating CSS Grid Layout properties
According to the CSS Grid Layout Module Level 1 specification there are 5 animatable grid properties:
grid-gap
,grid-row-gap
,grid-column-gap
grid-template-columns
,grid-template-rows
Currently only the animation of grid-gap
, grid-row-gap
, grid-column-gap
is implemented and only in Firefox and Firefox Mobile. I wrote a post about animating CSS Grid Layout properties, where you’ll find some more details and a demo.
The value of grid-column/row-end can be lower than the start value
In level 4 of the CSS Grid Garden game I learned that the value of grid-column-end
or grid-row-end
may be lower than the respective start equivalent.
.item:first-child {
grid-column-end: 2;
grid-column-start: 4;
}
The item in the above code will start on the 4th line and end on the 2nd, or in other words, start on the 2nd line and end on the 4th.
See the Pen Lower grid-column-end value than grid-column-start by Manuel Matuzovic (@matuzo) on CodePen.
Using the `span` keyword on grid-column/row-start and grid-column/row-end
A grid item by default spans a single cell. If you want to change that, the span
keyword can be quite convenient. For example setting grid-column-start: 1
and grid-column-end: span 2
will make the grid item span two cells, from the first to the third line.
You can also use the span keyword with grid-column-start
. If you set grid-column-end: -1
and grid-column-start: span 2
the grid-item will be placed at the last line and span 2 cells, from the last to third to last line.
See the Pen CSS Grid Layout: span keyword by Manuel Matuzovic (@matuzo) on CodePen.
grid-template-areas and implicit named lines
If you create template areas in a grid, you automatically get four implicit named lines, two naming the row-start
and row-end
and two for the column-start
and column-end
. By adding the -start
or -end
suffix to the name of the area, they’re applicable like any other named line.
.grid {
display: grid;
grid-template-columns: 1fr 200px 200px;
grid-template-areas:
"header header header"
"articles favorites posts"
}
.footer {
grid-column-start: favorites-start;
grid-column-end: posts-end;
}
See an example for implicit named lines in this Pen.
Grid is available in the insider version of Microsoft Edge
Support for CSS Grid Layout is pretty great since all major browsers, except IE and Edge, support it. For a lot of projects you can start using CSS Grid Layouts today. Support for Microsoft Edge will probably come pretty soon, because it’s already available in the insider version of Microsoft Edge.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
57 | 52 | 11* | 16 | 10.1 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
115 | 115 | 115 | 10.3 |
If you want to learn more about Grids check out The Complete Guide to Grid, Getting Started with CSS Grid, Grid By Example and my Collection of Grid demos on CodePen.
Animating CSS Grid Layouts! WOW! This is going to be amazing! Didn;t know about this one. Thanks a ton!
Absolutely! I’m looking forward to
grid-template-columns
andgrid-template-rows
being animatable.Lovely stuff! :)
Thanks! :)
Grid (the old spec, of course) is implemented in IE10 too – not that it matters much, as its market share is meaningless.
It’s a shame for those animatable properties that just don’t get enough love from vendors. Let’s see if Microsoft will do things right when they’ll release the updated version.
I’d like to see properties like
grid-row-start
and the others being animated too! But I fear the animation would be too complex to compute.Yes, of course, thanks for pointing it out. I was referring to the new spec because support in IE 10/11 and Edge is very limited. Most of the properties aren’t supported and there’s no auto-placement. In most cases Progressive Enhancement is the best way to deal with those browsers.
Could you add an explanation for
grid-auto-columns: 40px;
in the “Using thespan
keyword on grid-column/row-start and grid-column/row-end” section? It creates a 5. column to the left.Without an explanation, it might be confusing to some readers why the item’s widths don’t match the
span 3
andspan 2
ratio.Thanks for your feedback, that’s a great idea. We’ll update the post and even go more into detail about the implicit and explicit grid.
Thanks for thoughtfully breaking down the word-salad that is css grid a bit. Something is amiss in the supporting codepen in “grid-template-areas and implicit named lines.” The ads don’t show in Chrome. The class “ad” on the ads items make them disappear. More than that, i’m lost. But this article helped a lot.
Thanks for the feedback!
I guess that’s happening because you are using an adblocker in your browser. Sorry, I didn’t take that into account. I’ve changed the classnames, the Pen should work know.
Do you have an ad blocker installed? They often use classes to determine what elements should be removed