Building Challenging Layouts with CSS Grid

A presentation at Women Who Code in January 2019 in Bristol, UK by Michelle Barker

Slide 1

Slide 1

Building Challenging Layouts with CSS Grid

Slide 2

Slide 2

Slide 3

Slide 3

Slide 4

Slide 4

Why do we need CSS Grid?

Slide 5

Slide 5

Intrinsic Web Design

Slide 6

Slide 6

Grid terminology

Slide 7

Slide 7

Grid display: grid;

Slide 8

Slide 8

Tracks grid-template-rows

Slide 9

Slide 9

Tracks grid-template-columns

Slide 10

Slide 10

Cells

Slide 11

Slide 11

Grid areas

Slide 12

Slide 12

Gutters column-gap (Formerly grid-column-gap)

Slide 13

Slide 13

Gutters row-gap (Formerly grid-row-gap)

Slide 14

Slide 14

Defining a grid

Slide 15

Slide 15

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 16

Slide 16

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

Slide 17

Slide 17

codepen.io/michellebarker/pen/eLZwVg

Slide 18

Slide 18

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 19

Slide 19

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

Slide 20

Slide 20

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

Slide 21

Slide 21

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

Slide 22

Slide 22

Fr a fraction of the leftover space in the grid container

Slide 23

Slide 23

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

Slide 24

Slide 24

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 25

Slide 25

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

Slide 26

Slide 26

Implicit / Explicit tracks

Slide 27

Slide 27

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

Slide 28

Slide 28

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

Slide 29

Slide 29

codepen.io/michellebarker/pen/eLZwVg

Slide 30

Slide 30

Placing items

Slide 31

Slide 31

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 32

Slide 32

codepen.io/michellebarker/pen/gdrVXP

Slide 33

Slide 33

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

Slide 34

Slide 34

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

Slide 35

Slide 35

codepen.io/michellebarker/pen/YOqmjP

Slide 36

Slide 36

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

Slide 37

Slide 37

codepen.io/michellebarker/pen/YOqmjP

Slide 38

Slide 38

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 39

Slide 39

codepen.io/michellebarker/pen/YOqmjP

Slide 40

Slide 40

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 41

Slide 41

codepen.io/michellebarker/pen/YOqmjP

Slide 42

Slide 42

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

Slide 43

Slide 43

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

Slide 44

Slide 44

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

Slide 45

Slide 45

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

Slide 46

Slide 46

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 47

Slide 47

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

Slide 48

Slide 48

Naming lines header { grid-column: header-start / header-end; } main { grid-column: main-start / main-end; grid-row: span 3; }

Slide 49

Slide 49

Naming lines header { grid-column: header; } main { grid-column: main grid-row: span 3; } codepen.io/michellebarker/pen/rqZevv

Slide 50

Slide 50

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 .”; gap: 20px; }

Slide 51

Slide 51

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

Slide 52

Slide 52

CSS Grid for Complex Components A Case Study wbsl.com

Slide 53

Slide 53

Slide 54

Slide 54

Slide 55

Slide 55

<div class="grid"> <h2 class="grid__heading"></h2> <figure class="grid__media"></figure> <div class="grid__body"></div> </div>

Slide 56

Slide 56

Slide 57

Slide 57

Slide 58

Slide 58

Requirements - 24-column grid - 9 variants - Grid items can align with content wrapper, inner columns or outer edge - Heading must be vertically centred - Text can be above or below the heading - Text should align to top/bottom of image unless the content is longer than the available space - Responsive (of course)

Slide 59

Slide 59

Unknowns

  • Image size and/or aspect ratio (could be landscape or portrait) - Length of heading - Length of text

Slide 60

Slide 60

Requirements - 24-column grid - 9 variants - Grid items can align with content wrapper, inner columns or outer edge - Heading must be vertically centred - Text can be above or below the heading (controlled in CMS) - Text should align to top/bottom of image unless the content is longer than the available space - Responsive (of course)

Slide 61

Slide 61

Requirements - 24-column grid - 9 variants - Grid items can align with content wrapper, inner columns or outer edge - Heading must be vertically centred - Text can be above or below the heading (controlled in CMS) - Text should align to top/bottom of image unless the content is longer than the available space - Responsive (of course)

Slide 62

Slide 62

Slide 63

Slide 63

Slide 64

Slide 64

Slide 65

Slide 65

.grid { display: grid; grid-template-columns: repeat(26, 1fr); column-gap: 20px; }

Slide 66

Slide 66

Slide 67

Slide 67

1fr 1fr repeat(24, 60px)

Slide 68

Slide 68

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

Slide 69

Slide 69

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

Slide 70

Slide 70

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

Slide 71

Slide 71

.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; }

Slide 72

Slide 72

Requirements ✓ 24-column grid - 9 variants ✓ Grid items can align with content wrapper, inner columns or outer edge - Heading must be vertically centred

Text can be above or below the heading (controlled in CMS)

Text should align to top/bottom of image unless the content is longer than the available space

Responsive (of course)

Slide 73

Slide 73

.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; }

Slide 74

Slide 74

1fr auto 1fr

Slide 75

Slide 75

.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; }

Slide 76

Slide 76

.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; } align-items: center; // Vertically centre our image and heading

Slide 77

Slide 77

Slide 78

Slide 78

.grid { display: 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; gap: 40px 20px; align-items: center; }

Slide 79

Slide 79

Placing items .grid__media { 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; }

Slide 80

Slide 80

Slide 81

Slide 81

Slide 82

Slide 82

Slide 83

Slide 83

.grid { display: 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: auto auto auto; gap: 40px 20px; align-items: center; }

Slide 84

Slide 84

.grid { display: 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: auto 40px auto 40px auto; gap: 0 20px; align-items: center; }

Slide 85

Slide 85

.grid { display: 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: auto 40px auto 40px auto; gap: 0 20px; }

Slide 86

Slide 86

.grid { display: 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 auto 40px auto 40px auto; gap: 0 20px; }

Slide 87

Slide 87

1fr auto 40px auto 40px auto

Slide 88

Slide 88

Slide 89

Slide 89

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

Slide 90

Slide 90

.grid__media { grid-column: wrapper-start / 14; grid-row: media; } .grid__body { grid-column: span 5 / -3; grid-row: text; align-self: flex-start; } .grid__heading { grid-column: 5 / -5; grid-row: heading; }

Slide 91

Slide 91

Slide 92

Slide 92

Slide 93

Slide 93

Slide 94

Slide 94

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

Slide 95

Slide 95

.grid--text-bottom { grid-template-rows: [media-start] auto 40px [heading-start] auto [heading-end] 40px [text-start] auto [media-end] 1fr [text-end]; // hidden row } .grid--text-bottom .grid__body { align-self: flex-end; }

Slide 96

Slide 96

.grid--text-bottom

Slide 97

Slide 97

Variants /* Second variant of our component */ .grid--2 { .grid__media { grid-column: 13 / outer-end; } .grid__body { grid-column: wrapper-start / span 5; } }

Slide 98

Slide 98

Slide 99

Slide 99

Slide 100

Slide 100

Requirements ✓ 24-column grid ✓ 9 variants ✓ Grid items can align with content wrapper, inner columns or outer edge ✓ Heading must be vertically centered ✓ Text can be above or below the heading (controlled in CMS) ✓ Text should align to top/bottom of image unless the content is longer than the available space - Responsive (of course)

Slide 101

Slide 101

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

Slide 102

Slide 102

CSS Variables

Dynamic variables

Slide 103

Slide 103

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

Slide 104

Slide 104

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

Slide 105

Slide 105

Using variables for item placement .grid__media { grid-column: var(--mediaColStart) / var(--mediaColEnd); grid-row: media; } .grid__body { grid-column: var(--textColStart) / span 5; grid-row: text-top; }

Slide 106

Slide 106

Using variables for item placement .grid { --mediaColStart: wrapper-start; --mediaColEnd: 14; --textColStart: wrapper-end; } .grid—-2 { --mediaColStart: 13; --mediaColEnd: wrapper-end; --textColStart: wrapper-start; }

Slide 107

Slide 107

Requirements ✓ 24-column grid ✓ 9 variants ✓ Grid items can align with content wrapper, inner columns or outer edge ✓ Heading must be vertically centred ✓ Text can be above or below the heading (controlled in CMS) ✓ Text should align to top/bottom of image unless the content is longer than the available space - Responsive (of course)

Slide 108

Slide 108

https://codepen.io/michellebarker/pen/yGzWme

Slide 109

Slide 109

Wrapping up

Slide 110

Slide 110

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 111

Slide 111

Thank you for listening @mbarker_84 / @CSSInRealLife