CSS Grid: the new layout system of the web

A presentation at Fenders Perth in January 2018 in Perth WA, Australia by Anton Ball

Slide 1

Slide 1

Anton Ball Fenders, January 2018

Slide 2

Slide 2

Anton Ball Senior UI Integrator at Velpic

Slide 3

Slide 3

What’s Grid? CSS Grid Layout is a CSS layout method designed for the two-dimensional layout of items on a webpage or application. Rachel Andrew

Slide 4

Slide 4

Basic grid

Slide 5

Slide 5

What’s this?

Slide 6

Slide 6

What’s this?

Slide 7

Slide 7

What’s this?

Slide 8

Slide 8

What’s this?

Slide 9

Slide 9

Example HTML

Slide 10

Slide 10

So what’s new? • display: grid • grid-template-columns/grid-template-rows • grid-gap Where the magic happens. All children to grid items The number and size of the columns/rows. There’s shorthand syntax…but I wont use it Essentially the margin between cells. You’ll know gutter if you have used a grid framework before.

Slide 11

Slide 11

That’s interesting… if you’re making a static gallery Lets look at new units and CSS functions and make our grid fluid

Slide 12

Slide 12

New unit on the block, fraction (fr)

Slide 13

Slide 13

Setting up rows and columns is repetitive If we had a lot more columns than we are currently making, this will get painful. Enter repeat.

Slide 14

Slide 14

repeat()

Slide 15

Slide 15

Cells could get tiny, so minmax() Sets the sizes no smaller than the min and no greater than the max. You can get a long way with this without needing media queries!

Slide 16

Slide 16

minmax()

Slide 17

Slide 17

All together now You aren’t limited to just one of the units we have discussed. You can mix them all together.

Slide 18

Slide 18

All together now

Slide 19

Slide 19

Row/Column further reading • grid-column: span • grid-template-column: auto/auto-fit • grid-auto-rows/columns You can define how many columns to span Enables a lot of responsiveness without media queries Create implicit grid tracks

Slide 20

Slide 20

Positioning areas

Slide 21

Slide 21

Positioning areas

Slide 22

Slide 22

Grid column/row

Slide 23

Slide 23

Overlap 1 2 3 4 2 3 4 5

Slide 24

Slide 24

The holy grail at least it was.

Slide 25

Slide 25

The holy grail

Slide 26

Slide 26

We can do better the numbering can be tricky, naming is a nicer option

Slide 27

Slide 27

The named holy grail

Slide 28

Slide 28

Responsive

Slide 29

Slide 29

Named lines You can name each line in a row or column

Slide 30

Slide 30

Named lines

Slide 31

Slide 31

Named lines

Slide 32

Slide 32

Named lines

Slide 33

Slide 33

Support It’s not bad

Slide 34

Slide 34

Use in production • There’s no Polyfill • @supports(display: grid) • JS load a fallback Grid is too complex Fallback will likely be Flex - depends on your project There’s efficiency to be gained Stripe does this, but…

Slide 35

Slide 35

Further Reading • Rachel Andrew • gridbyexample.com/ • Wes Bos is releasing a grid course • mozilladevelopers.github.io/playground/ anything - book, talks, blogs, but especially:

Slide 36

Slide 36

Thank you Examples on CodePen: https://codepen.io/collection/nLxmgY/