Instructions: Click on the "CSS" button in each example to see the wonderful code behind the example.
Can I Use grid? Data on support for the grid feature across the major browsers from caniuse.com.
CSS has always been used to layout web pages, but was never very good at it. Flexbox helped out, but it's intended for simpler one-dimensional layouts, not complex two-dimensional ones (Flexbox and Grid actually work very well together).
Let's open up these pages in Firefox
See the Pen Grid Lab 1 by David Whitaker (@damwhit) on CodePen.
See the Pen Grid Lab 2 by David Whitaker (@damwhit) on CodePen.
Nothing yet. We need to specify our grid-template
grid-template-columns
PropertySee the Pen Grid Lab 3 by David Whitaker (@damwhit) on CodePen.
We can give each column fixed units
fr
UnitSee the Pen Grid Lab 4 by David Whitaker (@damwhit) on CodePen.
We can also use a new unit that will take up a fraction of the grid.
Your turn - check out the grid in the 'Inspector' area of your firefox devtools
repeat()
NotationSee the Pen Grid Lab 5 by David Whitaker (@damwhit) on CodePen.
grid-template-rows
PropertySee the Pen Grid Lab 6 by David Whitaker (@damwhit) on CodePen.
Thus far, our grid rows have been implicity set. But you can also explicitly set them using this property.
grid-gap
PropertiesSee the Pen Grid Lab 7 by David Whitaker (@damwhit) on CodePen.
Your turn - How do you add space between only rows? What about between only columns?
See the Pen Grid Lab 8 by David Whitaker (@damwhit) on CodePen.
See the Pen Grid Lab 9 by David Whitaker (@damwhit) on CodePen.
grid-auto-flow: dense
See the Pen Grid Lab 10 by David Whitaker (@damwhit) on CodePen.
Note that this will change the position of elements and therefore may not be great for accessibility.
auto-fit
See the Pen Grid Lab 11 by David Whitaker (@damwhit) on CodePen.
auto-fit
allows us to make our grid adaptive
minmax
See the Pen Grid Lab 8 by David Whitaker (@damwhit) on CodePen.
The addition of minmax()
makes things responsive
grid-template-areas
See the Pen Grid Lab 8 by David Whitaker (@damwhit) on CodePen.
Use grid-template-areas
in conjunction with the grid-area
to name your areas