Slide 1

Slide 1 text

How CSS Grid Changes Everything (in 15 mins?) W3C Developer Meetup @jenSimmons

Slide 2

Slide 2 text

Follow @jensimmons for slides

Slide 3

Slide 3 text

Layout on the Web

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

photo by Brad Frost, 2012, CC BY 2.0

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

The Official Timeline of Web Page Layout The No-Layout Layout Table-based Layouts Hand-coded Float Layouts Framework Layouts Amazing Future!

Slide 29

Slide 29 text

CSS Grid Flexbox Alignment Writing Modes Multicolumn Viewport Units Transforms Object Fit Clip-path Masking Shape-outside Initial-letter Flow Floats Block Inline Inline-block Display:table Margin Negative margins Padding everything else in CSS

Slide 30

Slide 30 text

This new CSS changes everything in web layout.

Slide 31

Slide 31 text

labs.jensimmons.com

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Grid makes it much easier to implement the same old layouts.

Slide 36

Slide 36 text

Grid makes it possible to do layouts that were not possible before.

Slide 37

Slide 37 text

Nature of CSS Grid

Slide 38

Slide 38 text

explicit vs. implicit

Slide 39

Slide 39 text

You define !e size and/or number of rows and/or columns Let !e browser define number or size of rows or columns

Slide 40

Slide 40 text

Place each "em 
 into a specific 
 cell or area Let !e browser place each "em using auto-placement algor"hm

Slide 41

Slide 41 text

Rows *and* Columns

Slide 42

Slide 42 text

ROWS!!!!!!!!!!!

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Tracks don’t have to all be the same size.

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Content sized by the size of a track.

Slide 49

Slide 49 text

Tracks sized by the size of content.

Slide 50

Slide 50 text

portion of available 
 space — 2 parts set by 
 content size fixed portion of available 
 space — 1 part

Slide 51

Slide 51 text

Grid Code

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Grid Container

Slide 55

Slide 55 text

Grid Items

Slide 56

Slide 56 text

… … body { display: grid; }

Slide 57

Slide 57 text

… … body { display: grid; } article { display: grid; }

Slide 58

Slide 58 text

… … body { display: grid; } main { display: subgrid; } article { display: subgrid; }

Slide 59

Slide 59 text

… … body { display: grid; } article { display: grid; }

Slide 60

Slide 60 text

Headline Here This is teaser text. It comes in different lengths. Headline Here This is teaser text. It comes in different lengths. Here we see more content filling the box up all the way. Headline This is teaser text. Headline That is Longer & Wraps This is teaser text. It comes in different lengths. Headline This Headline This is teaser text. It comes in different lengths.

Slide 61

Slide 61 text

Headline Here This is teaser text. It comes in different lengths. Headline Here This is teaser text. It comes in different lengths. Here we see more content filling the box up all the way. Headline This is teaser text. Headline That is Longer & Wraps This is teaser text. It comes in different lengths. Headline This Headline This is teaser text. It comes in different lengths.

Slide 62

Slide 62 text

Headline Here This is teaser text. It comes in different lengths. Headline Here This is teaser text. It comes in different lengths. Here we see more content filling the box up all the way. Headline This is teaser text. Headline That is Longer & Wraps This is teaser text. It comes in different lengths. Headline This Headline This is teaser text. It comes in different lengths.

Slide 63

Slide 63 text

Headline Here This is teaser text. It comes in different lengths. Headline Here This is teaser text. It comes in different lengths. Here we see more content filling the box up all the way. Headline This is teaser text. Headline That is Longer & Wraps This is teaser text. It comes in different lengths. Headline This Headline This is teaser text. It comes in different lengths.

Slide 64

Slide 64 text

Headline Here This is teaser text. It comes in different lengths. Headline Here This is teaser text. It comes in different lengths. Here we see more content filling the box up all the way. Headline This is teaser text. Headline That is Longer & Wraps This is teaser text. It comes in different lengths. Headline This Headline This is teaser text. It comes in different lengths.

Slide 65

Slide 65 text

labs.jensimmons.com

Slide 66

Slide 66 text

Container Item Item Item

Slide 67

Slide 67 text

Terminology

Slide 68

Slide 68 text

Grid Container

Slide 69

Slide 69 text

Grid Items

Slide 70

Slide 70 text

Grid Line Grid Track Grid Track Grid Line Grid Line Grid Line Grid Line Grid Area Grid Cell

Slide 71

Slide 71 text

Grid Line Grid Track Grid Line Grid Line Grid Line Grid Line Grid Gap Grid Gap Grid Gap Grid Track

Slide 72

Slide 72 text

4 Examples

Slide 73

Slide 73 text

labs.jensimmons.com/2017/01-003.html

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Slide 76

Slide 76 text

ul { display: grid; grid-template-columns: repeat(4, 100px); grid-gap: 4px; } li { // nothing }

Slide 77

Slide 77 text

ul { display: grid; grid-template-columns: repeat(4, 100px); grid-gap: 4px; } li { // nothing }

Slide 78

Slide 78 text

ul { display: grid; grid-template-columns: repeat(4, 100px); grid-gap: 4px; } /* or */ ul { display: grid; grid-template-columns: 100px 100px 100px 100px; grid-gap: 4px; }

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

ul { display: grid; grid-template-columns: repeat(5, 1fr); grid-gap: 0.25em; } /* or */ ul { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr 1fr; grid-gap: 0.25em; }

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

ul { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); grid-gap: 0.25rem; }

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

nightly.mozilla.org

Slide 86

Slide 86 text

ul { display: grid; grid-template-columns: repeat(4, 1fr); }

Slide 87

Slide 87 text

li:nth-child(1) { grid-column: 2 / 3; grid-row: 1 / 2; }

Slide 88

Slide 88 text

li:nth-child(1) { grid-column: 2 / 3; grid-row: 1 / 2; } li:nth-child(2) { grid-column: 4 / 5; grid-row: 2 / 3; } li:nth-child(3) { grid-column: 3 / 4; grid-row: 3 / 4; } li:nth-child(4) {…} li:nth-child(5) {…}

Slide 89

Slide 89 text

‘fr’ unit

Slide 90

Slide 90 text

.grid-container { display: grid; grid-template-columns: 1fr 1fr 1fr; } some other syntax options (each with different results) grid-template-columns: 1fr 5fr 2.5fr; grid-template-columns: 8em 1fr 300px; grid-template-columns: 1fr 1fr 2fr 3fr 5fr 8fr 13fr 21fr 34fr; grid-template-columns: repeat(7, 1fr); grid-template-columns: repeat(3, 200px 1fr 25%); grid-template-columns: repeat(auto-fill, 10rem); grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); grid-template-columns: 4rem 2fr repeat(5, 1fr) 300px;

Slide 91

Slide 91 text

px em % pixels (or rem) percents 60px 10em 20%

Slide 92

Slide 92 text

min-content max-content fr minmax()

Slide 93

Slide 93 text

fr unit = “fraction”

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

100% 33% 33% 33%

Slide 96

Slide 96 text

100% 33.33333% 33.33333% 33.33333%

Slide 97

Slide 97 text

100% 33.33333% 33.33333% 33.33333%

Slide 98

Slide 98 text

100% 33% 33% 33% 2% 2%

Slide 99

Slide 99 text

100% 32% 32% 32% 2% 2% 100% – 4% = 96% = 32% 3 3

Slide 100

Slide 100 text

100% 31.333% 31.333% 31.333% 3% 3% 100% – 6% = 94% = 31.333% 3 3

Slide 101

Slide 101 text

100% 31.666% 31.666% 31.666% 2.5% 2.5% 100% – 5% = 95% = 31.666666666666666% 3 3

Slide 102

Slide 102 text

.box { width: calc(100-(2*2em)/3)%;} 100% x% x% x% 2em 2em @media (min-width: 600px) { .box { width: calc(100-(2*2em)/3)%;}} @media (min-width: 800px) { .box { width: calc(100-(3*2em)/4)%;}} @media (min-width: 400px) { .box { width: calc(100-(1*2em)/2)%;}}

Slide 103

Slide 103 text

100% 1fr 1fr 1fr 2em 2em

Slide 104

Slide 104 text

1fr 1fr 1fr 2em 2em 1fr + 1fr + 1fr = 3fr total therefore, 1fr = 1/3 of the space

Slide 105

Slide 105 text

1fr 1fr 1fr 2em 2em 1fr + 1fr + 1fr + 1fr = 4fr total therefore, now 1fr = 1/4 of the space 1fr 2em

Slide 106

Slide 106 text

100px 1fr 1fr 1fr 1fr 100px

Slide 107

Slide 107 text

2fr 1fr 50px 1fr min-content

Slide 108

Slide 108 text

6fr 2.4fr 1fr 2.4fr 2fr

Slide 109

Slide 109 text

What about old browsers?

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

6

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

You must support browsers that 
 do not understand 
 CSS Grid.

Slide 114

Slide 114 text

@supports (foo: value) { // some CSS code here }

Slide 115

Slide 115 text

// code for non-Grid browsers @supports (display: grid) { // undo some of the above // then do your Grid layout }

Slide 116

Slide 116 text

hacks.mozilla.org/2016/08/using-feature-queries-in-css

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

6

Slide 119

Slide 119 text

You have two choices for 
 Internet Explorer (& old Edge): 1) Leverage the 2012 Grid implementation. 2) Pretend IE has no Grid.

Slide 120

Slide 120 text

You have two choices for 
 Internet Explorer (& old Edge): 1) Use old -ms-* syntax. 2) Or don’t.

Slide 121

Slide 121 text

display: grid; grid-template-columns: repeat(4, 100px); display: -ms-grid; -ms-grid-columns: (100px)[4];

Slide 122

Slide 122 text

Autoprefixer

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

rachelandrew.co.uk/archives/2016/11/26/should-i-try- to-use-the-ie-implementation-of-css-grid-layout

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

For more…

Slide 131

Slide 131 text

jensimmons.com/post/feb-27-2017/learn-css-grid

Slide 132

Slide 132 text

developer.mozilla.org/docs/Web/CSS/CSS_Grid_Layout

Slide 133

Slide 133 text

Modern Layouts: 
 Getting Out of Our Ruts Revolutionize Your Page: 
 Real Art Direction on the Web 2015 2016 available on jensimmons.com

Slide 134

Slide 134 text

jensimmons.com @jensimmons layout.land labs.jensimmons.com Thanks!