CSS Grid: A How-To Guide

A presentation at Frontstack in February 2019 in Lagos, Nigeria by Jemima Abu

Slide 1

Slide 1

CSS GRID: A How-To Guide

Slide 2

Slide 2

Who? Me? - Jemima Abu Front end developer. Nerd. Aspiring cat lady.

Slide 3

Slide 3

What’s CSS Grid?

Slide 4

Slide 4

HEADER CONTENT SIDEBAR FOOTER Basically this.

Slide 5

Slide 5

Okay but really, what is CSS Grid?

Slide 6

Slide 6

● ● ● A 2-dimensional layout system I’m you, but better. Created specifically for layouts Allows both rows and columns Who are you? Grid ● Basically, flexbox but better Flexbox

Slide 7

Slide 7

Sounds good, how does it work?

Slide 8

Slide 8

Grid is applied to parent element: .parent { display: grid; }

Slide 9

Slide 9

Rows and columns are specified using the following properties: .parent { display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px; }

Slide 10

Slide 10

The repeat property is used to simplify repetitive layout: .parent { display: grid; grid-template-columns: repeat( 3, 100px ); grid-template-rows: repeat( 2, 100px ); }

Slide 11

Slide 11

The gap property is used to space out elements in the grid: .parent { display: grid; grid-template-columns: repeat( 3, 100px ); grid-template-rows: repeat( 2, 100px ); grid-gap: 10px; }

Slide 12

Slide 12

Oh okay cool… is that all?

Slide 13

Slide 13

The awesomeness of CSS Grid

Slide 14

Slide 14

Grid-template-areas .parent { display: grid; grid-template-areas: “a a a” “b c c” “b c c”; grid-gap: 10px; } .a { grid-area: a; } .b { grid-area: b; } .c { grid-area: c; } Allows you to literally specify content placement

Slide 15

Slide 15

Grid-template-areas .parent { display: grid; Evenly spaces out elements and fits to specified width grid-template-areas: “a a a” “b c c” a a a b c c b c c “b c c”; grid-gap: 10px; } .a { grid-area: a; } .b { grid-area: b; } .c { grid-area: c; }

Slide 16

Slide 16

Grid-template-areas Also allows for blank placement .parent { a display: grid; grid-template-areas: “. a .” “. . c” c “b . .”; } Use the full-stop character “.” to specify an empty cell b

Slide 17

Slide 17

‘fr’ unit Fractional unit that fills up part of available space .parent { display: grid; grid-template-columns: 1fr 1fr 1fr; }

Slide 18

Slide 18

‘fr’ unit Works with grid-gap .parent { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 10px; }

Slide 19

Slide 19

minmax() Specifies range for minimum and maximum sizes of elements in a div .parent { display: grid; grid-template-columns: minmax(25px, 50px) minmax(50px, 100px) minmax(10px, 1fr); }

Slide 20

Slide 20

minmax() With a parent width of 600px

Slide 21

Slide 21

minmax() With a parent width of 600px

Slide 22

Slide 22

minmax() With a parent width of 600px

Slide 23

Slide 23

minmax() With a parent width of 50px

Slide 24

Slide 24

minmax() With a parent width of 50px

Slide 25

Slide 25

minmax() With a parent width of 50px

Slide 26

Slide 26

auto-fill auto-fit Fills row with as many columns as can fill the row, including empty columns (?) Fills row with existing columns and expands them to take up available space (?) .parent { .parent { display: grid; display: grid; grid-template-columns: grid-template-columns: repeat(auto-fill, repeat(auto-fit, minmax(50px,1fr)); minmax(50px,1fr)); } }

Slide 27

Slide 27

Wow, that’s amazing. So how can it be used?

Slide 28

Slide 28

Regular layouts

Slide 29

Slide 29

Irregular Layouts

Slide 30

Slide 30

“That’s CSS?!” Layout

Slide 31

Slide 31

Okay I’m convinced, CSS Grid is awesome. Now let’s see some code!

Slide 32

Slide 32

Demo Time! Yay, I was out of slide content

Slide 33

Slide 33

Demo Page: https://jemimaabu.github.io/css-grid Open Source: https://github.com/jemimaabu/css-grid

Slide 34

Slide 34

Slide 35

Slide 35

Resources ● ● ● ● ● ● ● https://css-tricks.com/snippets/css/complete-guide-grid/ https://learncssgrid.com/ https://tympanus.net/codrops/css_reference/grid/ https://gridbyexample.com/examples/ https://www.awwwards.com/30-grid-based-websites.html https://www.smashingmagazine.com/2017/10/css-grid-challenge-2017-wi nners/ https://speckyboy.com/creative-examples-css-grid-layouts/

Slide 36

Slide 36

www.jemimaabu.com (Website) @jemimaabu (Everywhere else)