Super-powered Layouts with CSS Grid and CSS Variables

A presentation at State Of The Browser 2018 in September 2018 in London, UK by Michelle Barker

Slide 1

Slide 1

Super-powered Layouts with CSS Grid + CSS Variables

Slide 2

Slide 2

CSS Grid (CSS Grid Layout Module Level 1)

Slide 3

Slide 3

Why not just use flexbox?

Slide 4

Slide 4

Grid terminology

Slide 5

Slide 5

Grid .grid { display: grid; }

Slide 6

Slide 6

Tracks (rows and columns)

Slide 7

Slide 7

Tracks (rows and columns)

Slide 8

Slide 8

Cells

Slide 9

Slide 9

Grid-areas

Slide 10

Slide 10

Gutters .grid { grid-column-gap: 20px; }

Slide 11

Slide 11

Gutters .grid { grid-row-gap: 20px; }

Slide 12

Slide 12

Defining a grid

Slide 13

Slide 13

On the container: .grid { display: grid; grid-template-columns: 200px 200px 200px 200px; grid-template-rows: 150px 150px 150px 150px; } codepen.io/michellebarker/pen/eLZwVg

Slide 14

Slide 14

On the container: .grid { display: grid; grid-template-columns: 200px 200px 200px 200px; grid-template-rows: 150px 150px 150px 150px; grid-column-gap: 20px; grid-row-gap: 20px; } codepen.io/michellebarker/pen/eLZwVg

Slide 15

Slide 15

codepen.io/michellebarker/pen/eLZwVg

Slide 16

Slide 16

repeat() .grid { display: grid; grid-template-columns: repeat(4, 200px); grid-template-rows: repeat(4, 150px); } grid-gap: 20px; codepen.io/michellebarker/pen/eLZwVg

Slide 17

Slide 17

Shorthand .grid { display: grid; grid-template: repeat(4, 150px) / repeat(4, 200px); } grid-gap: 20px; codepen.io/michellebarker/pen/eLZwVg

Slide 18

Slide 18

Shorthand .grid { display: grid; grid-template: repeat(4, 150px) / repeat(4, 200px); grid-gap: 20px; // grid-row-gap / grid-column-gap } codepen.io/michellebarker/pen/eLZwVg

Slide 19

Slide 19

Flexible units (fr) .grid { display: grid; grid-template-columns: repeat(4, 1fr); } grid-template-rows: 150px; grid-gap: 20px; codepen.io/michellebarker/pen/eLZwVg

Slide 20

Slide 20

Flexible units (fr) .grid { display: grid; grid-template-columns: repeat(3, 200px) 1fr; } grid-template-rows: 150px; grid-gap: 20px; codepen.io/michellebarker/pen/wEdjqX

Slide 21

Slide 21

Flexible units (fr) codepen.io/michellebarker/pen/wEdjqX

Slide 22

Slide 22

Implicit tracks .grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 150px; } grid-gap: 20px; codepen.io/michellebarker/pen/eLZwVg

Slide 23

Slide 23

codepen.io/michellebarker/pen/eLZwVg

Slide 24

Slide 24

Placing items

Slide 25

Slide 25

Auto-placement <div class="grid"> <div class="grid__item">1</div> <div class="grid__item">2</div> <div class="grid__item">3</div> </div> codepen.io/michellebarker/pen/gdrVXP

Slide 26

Slide 26

codepen.io/michellebarker/pen/gdrVXP

Slide 27

Slide 27

<header> <main> <aside> codepen.io/michellebarker/pen/YOqmjP

Slide 28

Slide 28

Placing items by grid line <div class="grid"> <header></header> <main></main> <aside></aside> </div>

Slide 29

Slide 29

codepen.io/michellebarker/pen/YOqmjP

Slide 30

Slide 30

Placing items by grid line header { grid-column-start: 1; grid-column-end: 5; } codepen.io/michellebarker/pen/YOqmjP

Slide 31

Slide 31

codepen.io/michellebarker/pen/YOqmjP

Slide 32

Slide 32

Placing items by grid line main { grid-column-start: 1; grid-column-end: 4; grid-row-start: 2; grid-row-end: 5; } aside { grid-column-start: 4; grid-column-end: 5; grid-row-start: 2; grid-row-end: 4; } codepen.io/michellebarker/pen/YOqmjP

Slide 33

Slide 33

codepen.io/michellebarker/pen/YOqmjP

Slide 34

Slide 34

Placing items by grid line header { grid-column: 1 / 5; } main { grid-column: 1 / 4; grid-row: 2 / 5; } aside { // grid-column: 4 / 5; - unnecessary as will be auto-placed grid-row: span 2; } codepen.io/michellebarker/pen/YOqmjP

Slide 35

Slide 35

codepen.io/michellebarker/pen/YOqmjP

Slide 36

Slide 36

Placing items by grid line main { grid-column: 1 / 4; grid-row: 2 / 4; } codepen.io/michellebarker/pen/YOqmjP

Slide 37

Slide 37

Placing items by grid line main { grid-column: span 3; grid-row: 2 / 4; } codepen.io/michellebarker/pen/YOqmjP

Slide 38

Slide 38

Placing items by grid line main { grid-column: 1 / span 3; grid-row: 2 / 4; } codepen.io/michellebarker/pen/YOQLLZ

Slide 39

Slide 39

Placing items by grid line main { grid-column: span 3 / 4; grid-row: 2 / 4; } codepen.io/michellebarker/pen/YOqmjP

Slide 40

Slide 40

Naming lines .grid { display: grid; grid-template-columns: [header-start main-start] repeat(3, 1fr) [main-end] 1fr [header-end]; grid-auto-rows: 150px; } codepen.io/michellebarker/pen/OobJaa

Slide 41

Slide 41

header-start main-start main-end header-end codepen.io/michellebarker/pen/OobJaa

Slide 42

Slide 42

Grid areas header { grid-column: header; } main { grid-column: main grid-row: 2 / 5; } aside { grid-row: span 2; } codepen.io/michellebarker/pen/OobJaa

Slide 43

Slide 43

grid-template-areas .grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 150px; grid-template-areas: "h h h h" "m m m a" "m m m a” "m m m a” "m m m .”; grid-gap: 20px; } codepen.io/michellebarker/pen/OobJqr

Slide 44

Slide 44

grid-template-areas header { grid-area: h; } main { grid-area: m; } aside { grid-area: a; } codepen.io/michellebarker/pen/OobJqr

Slide 45

Slide 45

CSS Variables (a.k.a. Custom Properties)

Slide 46

Slide 46

CSS Variables Preprocessor != Variables

Slide 47

Slide 47

CSS Variables

Dynamic variables

Slide 48

Slide 48

CSS Variables

Preprocessor = Variables let const

Slide 49

Slide 49

Declaring variables :root { --bgColor: red; }

Slide 50

Slide 50

Using variables .my-component { background-color: var(--bgColor); }

Slide 51

Slide 51

Defaults .my-component { background-color: var(--bgColor, orange); } .my-component:last-child { --bgColor: red; } codepen.io/michellebarker/pen/MXXQRq

Slide 52

Slide 52

Defaults .box { background-color: var(--bgColor, orange); } .purple { --bgColor: rebeccaPurple; } codepen.io/michellebarker/pen/rKKdBP

Slide 53

Slide 53

Defaults <div class="box"></div> .box { background-color: orange; }

<div class="purple"> <div class="box"></div> </div> .box { background-color: rebeccaPurple; } codepen.io/michellebarker/pen/rKKdBP

Slide 54

Slide 54

CSS Variables + CSS Grid

Slide 55

Slide 55

Managing complex grid declarations

Slide 56

Slide 56

Managing complex grid declarations .grid { display: grid; grid-template-columns: [outer-start] minmax(20px, 1fr) [wrapper-start] repeat(24, minmax(0, 30px)) [wrapper-end] minmax(20px, 1fr) [outer-end]; grid-template-rows: [outer-start] 1fr [text-top-end] 40px [heading-start] auto [heading-end] 40px [text-bottom-start] 1fr [outer-end]; grid-gap: 0 20px; }

Slide 57

Slide 57

Managing complex grid declarations :root { --colWidth: 30px; --gutter: 20px; @media (min-width: 1600px) { --colWidth: 50px; —-gutter: 30px; } }

Slide 58

Slide 58

Managing complex grid declarations .grid { display: grid; grid-template-columns: [outer-start] minmax(20px, 1fr) [wrapper-start] repeat(24, minmax(0, var(--colWidth))) [wrapper-end] minmax(20px, 1fr) [outer-end]; grid-template-rows: [outer-start] 1fr [text-top-end] 40px [heading-start] auto [heading-end] 40px [text-bottom-start] 1fr [outer-end]; grid-gap: 0 var(—-gutter); } codepen.io/michellebarker/pen/NzYbVO

Slide 59

Slide 59

Managing component variants

Slide 60

Slide 60

Managing component variants

<article class="grid"> <figure class=“grid__img"></figure> <div class=“grid__text”></div> </article> codepen.io/michellebarker/pen/XBPMZZ

Slide 61

Slide 61

Managing component variants codepen.io/michellebarker/pen/XBPMZZ

Slide 62

Slide 62

Managing component variants .grid { display: grid; grid-template-columns: [start] minmax(20px, 1fr) [wrapper-start] repeat(12, minmax(0, 70px)) [wrapper-end] minmax(20px, 1fr) [end]; grid-gap: 20px; } codepen.io/michellebarker/pen/XBPMZZ

Slide 63

Slide 63

Managing component variants .grid__img { grid-column: wrapper-start / 9; } .grid__text { grid-column: 10 / span 4; } codepen.io/michellebarker/pen/XBPMZZ

Slide 64

Slide 64

Managing component variants codepen.io/michellebarker/pen/XBPMZZ

Slide 65

Slide 65

Managing component variants .grid__img { grid-column: var(—-imgStart, wrapper-start) / span var(--imgSpan, 6); } .grid__text { grid-column: var(--textStart, 10) / span 4; } codepen.io/michellebarker/pen/XBPMZZ

Slide 66

Slide 66

Managing component variants .grid—-a { --textStart: 9; --imgStart: wrapper-start; } .grid—-b { —-textStart: 9; --imgStart: start; } .grid—-c { —-imgSpan: 6; } codepen.io/michellebarker/pen/XBPMZZ

Slide 67

Slide 67

CSS Variables + Javascript

Slide 68

Slide 68

Get property value: getComputedStyle(element).getPropertyValue("--myVariable") (Returns a string) Set property value: element.style.setProperty("--myVariable", 4)

Slide 69

Slide 69

Demo CSS Grid background generator codepen.io/michellebarker/pen/xabrLv

Slide 70

Slide 70

Resources Grid by Example (Rachel Andrew) gridbyexample.com Layout Land (Jen Simmons) layout.land CSS Grid Playground (MDN) mozilladevelopers.github.io/playground/css-grid CSS { In Real Life } css-irl.info

Slide 71

Slide 71

Thanks for listening @mbarker_84 / @CSSInRealLife