Building Intelligent Layouts with CSS Grid

A presentation at WordCamp Bristol in May 2019 in Bristol, UK by Michelle Barker

Slide 1

Slide 1

Building Intelligent Layouts with CSS Grid @MicheBarks

Slide 2

Slide 2

“Intrinsic Web Design” Jen Simmons @MicheBarks

Slide 3

Slide 3

@MicheBarks

Slide 4

Slide 4

@MicheBarks

Slide 5

Slide 5

@MicheBarks

Slide 6

Slide 6

Why do we need Grid? 2D layout @MicheBarks

Slide 7

Slide 7

Why do we need Grid? Grid systems @MicheBarks

Slide 8

Slide 8

“Flexbox is great for taking a bunch of oddly-sized things and returning the most reasonable layout for those things” Rachel Andrew @MicheBarks

Slide 9

Slide 9

Why do we need Grid? Grid systems @MicheBarks

Slide 10

Slide 10

Why do we need Grid? Grid systems Fewer dependencies @MicheBarks

Slide 11

Slide 11

Why do we need Grid? Complete control @MicheBarks

Slide 12

Slide 12

Grid terminology @MicheBarks

Slide 13

Slide 13

Grid terminology Grid display: grid; @MicheBarks

Slide 14

Slide 14

Grid terminology Tracks grid-template-rows @MicheBarks

Slide 15

Slide 15

Grid terminology Tracks grid-template-columns @MicheBarks

Slide 16

Slide 16

Grid terminology Cells @MicheBarks

Slide 17

Slide 17

Grid terminology Grid areas @MicheBarks

Slide 18

Slide 18

Grid terminology Gutters column-gap (Formerly grid-column-gap) @MicheBarks

Slide 19

Slide 19

Grid terminology Gutters row-gap (Formerly grid-row-gap) @MicheBarks

Slide 20

Slide 20

Defining a grid @MicheBarks

Slide 21

Slide 21

CSS .grid { display: grid; } @MicheBarks codepen.io/michellebarker/pen/eLZwVg

Slide 22

Slide 22

CSS .grid { display: grid; grid-template-columns: 200px 200px 200px 200px; grid-template-rows: 150px 150px 150px 150px; } @MicheBarks codepen.io/michellebarker/pen/eLZwVg

Slide 23

Slide 23

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

Slide 24

Slide 24

@MicheBarks codepen.io/michellebarker/pen/eLZwVg

Slide 25

Slide 25

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

Slide 26

Slide 26

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

Slide 27

Slide 27

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

Slide 28

Slide 28

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

Slide 29

Slide 29

Fr = fraction units a fraction of the leftover space in the grid container @MicheBarks

Slide 30

Slide 30

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

Slide 31

Slide 31

grid-template-columns: repeat(3, 1fr) 2fr; 1fr @MicheBarks 1fr 1fr 2fr

Slide 32

Slide 32

CSS Fraction units (fr) .grid { display: grid; grid-template-columns: repeat(3, 200px) 1fr; } @MicheBarks grid-template-rows: repeat(4, 150px); gap: 20px;

Slide 33

Slide 33

200px @MicheBarks 200px 200px 1fr

Slide 34

Slide 34

Implicit vs Explicit tracks @MicheBarks

Slide 35

Slide 35

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

Slide 36

Slide 36

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

Slide 37

Slide 37

@MicheBarks codepen.io/michellebarker/pen/eLZwVg

Slide 38

Slide 38

Placing items @MicheBarks

Slide 39

Slide 39

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

Slide 40

Slide 40

@MicheBarks codepen.io/michellebarker/pen/gdrVXP

Slide 41

Slide 41

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

Slide 42

Slide 42

HTML Placing items by grid line <div class=”grid”> <header></header> <main></main> <aside></aside> </div> @MicheBarks

Slide 43

Slide 43

@MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 44

Slide 44

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

Slide 45

Slide 45

@MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 46

Slide 46

CSS 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; } @MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 47

Slide 47

@MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 48

Slide 48

CSS 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; } @MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 49

Slide 49

@MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 50

Slide 50

CSS Start line / end line main { grid-column: 1 / 4; grid-row: 2 / 4; } @MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 51

Slide 51

CSS Span (with auto-placement) main { grid-column: span 3; grid-row: 2 / 4; } @MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 52

Slide 52

CSS Start line / span main { grid-column: 1 / span 3; grid-row: 2 / 4; } @MicheBarks codepen.io/michellebarker/pen/YOQLLZ

Slide 53

Slide 53

CSS Span / end line main { grid-column: span 3 / 4; grid-row: 2 / 4; } @MicheBarks codepen.io/michellebarker/pen/YOqmjP

Slide 54

Slide 54

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

Slide 55

Slide 55

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

Slide 56

Slide 56

CSS header { grid-column: header; } main { grid-column: main; grid-row: span 3; } @MicheBarks codepen.io/michellebarker/pen/rqZevv

Slide 57

Slide 57

CSS grid-template-areas .grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 150px; grid-template-areas: gap: 20px; } @MicheBarks “h “m “m “m h m m m h m m m h” a” a” .”;

Slide 58

Slide 58

CSS grid-template-areas header { grid-area: h; } main { grid-area: m; } aside { grid-area: a; } @MicheBarks

Slide 59

Slide 59

Building a Complex Component A Case Study @MicheBarks

Slide 60

Slide 60

@MicheBarks

Slide 61

Slide 61

@MicheBarks

Slide 62

Slide 62

@MicheBarks

Slide 63

Slide 63

@MicheBarks

Slide 64

Slide 64

HTML

<div class=”grid”> <h2 class=”grid__heading”></h2> <figure class=”grid__image”></figure> <div class=”grid__body”></div> </div> @MicheBarks

Slide 65

Slide 65

Some rules @MicheBarks

Slide 66

Slide 66

Dynamic content @MicheBarks

Slide 67

Slide 67

The heading must always be vertically centred over the image @MicheBarks

Slide 68

Slide 68

The heading must always be vertically centred over the image …no matter the size of the image or the text @MicheBarks

Slide 69

Slide 69

Text block must align to the top of the image @MicheBarks

Slide 70

Slide 70

Text block must align to the top of the image …unless the text is too long @MicheBarks

Slide 71

Slide 71

Space must always be maintained between the text block and heading @MicheBarks

Slide 72

Slide 72

Context-aware @MicheBarks

Slide 73

Slide 73

CSS Grid 2D Layout @MicheBarks

Slide 74

Slide 74

@MicheBarks

Slide 75

Slide 75

@MicheBarks

Slide 76

Slide 76

@MicheBarks

Slide 77

Slide 77

@MicheBarks

Slide 78

Slide 78

grid-template-columns: repeat(26, 1fr); @MicheBarks

Slide 79

Slide 79

grid-template-columns: repeat(26, 1fr); @MicheBarks

Slide 80

Slide 80

1fr @MicheBarks repeat(24, 60px) 1fr

Slide 81

Slide 81

grid-template-columns: 1fr repeat(24, 60px) 1fr; @MicheBarks

Slide 82

Slide 82

minmax() minmax(20px, 1fr) Minimum size @MicheBarks Maximum size

Slide 83

Slide 83

minmax(20px, 1fr) minmax(0, 60px) @MicheBarks codepen.io/michellebarker/pen/wYGMPQ

Slide 84

Slide 84

grid-template-columns: minmax(20px, 1fr) repeat(24, minmax(0, 60px)) minmax(20px, 1fr); @MicheBarks

Slide 85

Slide 85

grid-template-columns: minmax(20px, 1fr) repeat(24, minmax(0, 60px)) minmax(20px, 1fr); @MicheBarks

Slide 86

Slide 86

grid-template-columns: minmax(20px, 1fr) repeat(24, minmax(0, 60px)) minmax(20px, 1fr); @MicheBarks

Slide 87

Slide 87

CSS .grid { display: grid; grid-template-columns: minmax(20px, 1fr) // flexible outer column repeat(24, minmax(0, 60px)) // 24 central columns minmax(20px, 1fr); // flexible outer column column-gap: 20px; } @MicheBarks

Slide 88

Slide 88

grid-template-rows: 1fr auto 1fr; @MicheBarks

Slide 89

Slide 89

CSS .grid { display: grid; grid-template-columns: minmax(20px, 1fr) repeat(24, minmax(0, 60px)) minmax(20px, 1fr); grid-template-rows: 1fr auto 1fr; } @MicheBarks column-gap: 20px;

Slide 90

Slide 90

CSS .grid { display: grid; grid-template-columns: minmax(20px, 1fr) repeat(24, minmax(0, 60px)) minmax(20px, 1fr); grid-template-rows: 1fr auto 1fr; gap: 40px 20px; } @MicheBarks

Slide 91

Slide 91

CSS Placing items .grid { grid-template-columns: [outer-start] minmax(20px, 1fr) [wrapper-start] repeat(24, minmax(0, 60px)) [wrapper-end] minmax(20px, 1fr) [outer-end]; grid-template-rows: 1fr [heading-start] auto [heading-end] 1fr; … } @MicheBarks

Slide 92

Slide 92

CSS Placing items .grid { grid-template-columns: [outer-start] minmax(20px, 1fr) [wrapper-start] repeat(24, minmax(0, 60px)) [wrapper-end] minmax(20px, 1fr) [outer-end]; grid-template-rows: 1fr [heading-start] auto [heading-end] 1fr; … } @MicheBarks

Slide 93

Slide 93

CSS Placing items .grid { grid-template-columns: [outer-start] minmax(20px, 1fr) [wrapper-start] repeat(24, minmax(0, 60px)) [wrapper-end] minmax(20px, 1fr) [outer-end]; grid-template-rows: 1fr [heading-start] auto [heading-end] 1fr; … } @MicheBarks

Slide 94

Slide 94

CSS .grid__image { grid-column: wrapper-start / 14; grid-row: 1 / -1; } .grid__body { grid-column: span 5 / wrapper-end; grid-row: 1; align-self: flex-start; } .grid__heading { grid-column: 5 / -5; grid-row: heading; } @MicheBarks

Slide 95

Slide 95

CSS .grid__image { grid-column: wrapper-start / 14; grid-row: 1 / -1; } .grid__body { grid-column: span 5 / wrapper-end; grid-row: 1; align-self: flex-start; } .grid__heading { grid-column: 5 / -5; } @MicheBarks grid-row: heading;

Slide 96

Slide 96

CSS .grid__image { grid-column: wrapper-start / 14; grid-row: 1 / -1; } .grid__body { grid-column: span 5 / wrapper-end; grid-row: 1; align-self: flex-start; } .grid__heading { grid-column: 5 / -5; grid-row: heading; } @MicheBarks

Slide 97

Slide 97

@MicheBarks

Slide 98

Slide 98

@MicheBarks

Slide 99

Slide 99

I didn’t solve this problem @MicheBarks

Slide 100

Slide 100

Gutter rows @MicheBarks

Slide 101

Slide 101

Secret hidden row @MicheBarks

Slide 102

Slide 102

grid-template-rows: 1fr auto 40px auto 40px auto; @MicheBarks

Slide 103

Slide 103

grid-template-rows: 1fr auto 40px auto 40px auto; @MicheBarks

Slide 104

Slide 104

@MicheBarks

Slide 105

Slide 105

@MicheBarks

Slide 106

Slide 106

CSS .grid { … grid-template-rows: [text-start] 1fr auto [text-end] 40px // gap [heading-start] auto [heading-start] 40px // gap auto; … } @MicheBarks

Slide 107

Slide 107

.grid__body { grid-row: text; } @MicheBarks

Slide 108

Slide 108

@MicheBarks

Slide 109

Slide 109

grid-template-rows: auto 40px auto 40px auto 1fr; Secret hidden row @MicheBarks

Slide 110

Slide 110

CSS .grid—text-bottom { grid-template-rows: auto 40px [heading-start] auto [heading-end] 40px [text-start] auto 1fr [text-end]; // hidden row } @MicheBarks

Slide 111

Slide 111

CSS .grid—text-bottom .grid__body { align-self: flex-end; } @MicheBarks

Slide 112

Slide 112

CSS .grid—text-bottom { grid-template-rows: [media-start] auto 40px [heading-start] auto [heading-end] 40px [text-start] auto [media-end] 1fr [text-end]; } Component variants .grid—text-bottom .grid__body { align-self: flex-end; } @MicheBarks @mbarker_84

Slide 113

Slide 113

SCSS /* Second variant of our component */ .grid—2 { .grid__image { grid-column: 13 / outer-end; } .grid__body { grid-column: wrapper-start / span 5; } } @MicheBarks

Slide 114

Slide 114

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

Slide 115

Slide 115

CSS Variables @MicheBarks

Dynamic variables

Slide 116

Slide 116

CSS Defining variables :root { —bgColor: red; } @MicheBarks

Slide 117

Slide 117

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

Slide 118

Slide 118

CSS .grid__image { grid-column: var(—imageColStart) / var(—-imageColEnd); grid-row: media; } .grid__body { grid-column: var(—textColStart) / span 5; grid-row: text; } @MicheBarks

Slide 119

Slide 119

CSS .grid__image { grid-column: var(—imageColStart) / var(—imageColEnd); } grid-row: media; .grid__body { grid-column: var(—textColStart) / span 5; grid-row: text; } @MicheBarks

Slide 120

Slide 120

CSS .grid__image { grid-column: var(—mediaColStart) / var(—mediaColEnd); grid-row: media; } .grid__body { grid-column: var(—textColStart) / span 5; } @MicheBarks grid-row: text;

Slide 121

Slide 121

CSS .grid { —imageColStart: wrapper-start; —imageColEnd: 14; —textColStart: -8; } .grid—-2 { —imageColStart: 13; —imageColEnd: outer-end; —textColStart: 3; } @MicheBarks

Slide 122

Slide 122

codepen.io/michellebarker/pen/yGzWme @MicheBarks

Slide 123

Slide 123

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 @MicheBarks

Slide 124

Slide 124

Thank you for listening @MicheBarks / @CSSInRealLife