Slide 1

Slide 1 text

Prototyping Layout with CSS Grid Refresh DC • September 2017

Slide 2

Slide 2 text

Hello! Jess Eldredge
 Senior web developer, Slate @jessabean

Slide 3

Slide 3 text

Grid system:
 the foundation of design

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

Design challenge

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

Our requirements • Responsive: grid context changes across breakpoints • Use CSS Grid! • Fallback for browsers that don’t support grid

Slide 14

Slide 14 text

What is CSS Grid?

Slide 15

Slide 15 text

Floats: float containers left/right, no equal heights, remember to clear!

Slide 16

Slide 16 text

Flexbox: 1 dimension, anything in a straight line, distribute space evenly

Slide 17

Slide 17 text

CSS Grid: 2 dimensions, children can be positioned anywhere

Slide 18

Slide 18 text

Planning layout

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

Setting up the grid

Slide 23

Slide 23 text

Markup for grid 1

Slide 24

Slide 24 text

Centering our content wrapper .wrapper { max-width: 20em; margin-left: auto; margin-right: auto; } @media screen and min-width(48em) { .wrapper { max-width: 38em; } } @media screen and min-width(64em) { .wrapper { max-width: 55em; } }

Slide 25

Slide 25 text

Setting up grid: display .grid { display: grid; }

Slide 26

Slide 26 text

Setting up grid: columns .grid { display: grid; grid-template-columns: 40px 40px 40px; }

Slide 27

Slide 27 text

Setting up grid: columns .grid { display: grid; grid-template-columns: 40px 40px 40px; } 1fr 1fr 1fr;

Slide 28

Slide 28 text

Setting up grid: columns .grid { display: grid; grid-template-columns: 40px 40px 40px; } 1fr 1fr 1fr; 1fr 2fr 1fr;

Slide 29

Slide 29 text

Setting up grid: columns .grid { display: grid; grid-template-columns: 40px 40px 40px; } 1fr 1fr 1fr; 1fr 2fr 1fr; 100px 25% 1fr;

Slide 30

Slide 30 text

Setting up grid: columns .grid { display: grid; grid-template-columns: repeat(6, 1fr); }

Slide 31

Slide 31 text

Setting up grid: rows .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-template-rows: repeat(2, 1fr); }

Slide 32

Slide 32 text

Setting up grid: rows .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-template-rows: repeat(2, 1fr); } // Alternative syntax: // grid-template-rows: 75px 125px; // grid-template-rows: 100px 1fr;

Slide 33

Slide 33 text

Setting up grid: gutters .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 0.75em; }

Slide 34

Slide 34 text

Setting up grid: gutters .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 0.75em; } // Shorthand is equivalent to: // grid-column-gap: 0.75em; // grid-row-gap: 0.75em;

Slide 35

Slide 35 text

Setting up grid: gutters .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 0.75em; } // Shorthand is equivalent to: // grid-column-gap: 0.75em; // grid-row-gap: 0.75em;

Slide 36

Slide 36 text

@media screen and (min-width: 48em) { .grid { grid-template-columns: repeat(12, 1fr); } } @media screen and (min-width: 64em) { .grid { grid-template-columns: repeat(15, 1fr); grid-gap: 1.25em; } } Responsive grid settings

Slide 37

Slide 37 text

DEMO: Basic grid

Slide 38

Slide 38 text

DEMO: Basic grid

Slide 39

Slide 39 text

DEMO: Basic grid

Slide 40

Slide 40 text

Placing items on the grid

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Placing grid items .grid > * { grid-column-start: 1; grid-column-end: 7; } // Alternative shorthand: // grid-column: 1 / 7;

Slide 43

Slide 43 text

Placing grid items .grid > * { grid-column-start: 1; grid-column-end: 7; } // Alternative shorthand: // grid-column: 1 / 7;

Slide 44

Slide 44 text

Placing grid items .grid > * { grid-column-start: 1; grid-column-end: 7; } // Alternative shorthand: // grid-column: 1 / 7;

Slide 45

Slide 45 text

Placing grid items .grid > * { grid-column-start: 1; grid-column-end: 7; } // Alternative shorthand: // grid-column: 1 / 7;

Slide 46

Slide 46 text

Shorthand: span syntax .grid > * { grid-column: 1 / span 6;
 }

Slide 47

Slide 47 text

Shorthand: span syntax .grid > * { grid-column: 1 / span 6;
 } // Shorthand is equivalent to: // grid-column-start: 1; // grid-column-end: span 6;

Slide 48

Slide 48 text

DEMO: Grid 1 small screen

Slide 49

Slide 49 text

Explicit vs implicit grid .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 0.75em; } .grid > * { grid-column: 1 / span 6; }

Slide 50

Slide 50 text

Explicit vs implicit grid .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 0.75em; } .grid > * { grid-column: 1 / span 6; }

Slide 51

Slide 51 text

Explicit vs implicit grid .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 0.75em; } .grid > * { grid-column: 1 / span 6; }

Slide 52

Slide 52 text

Explicit vs implicit grid .grid { display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 0.75em; } .grid > * { grid-column: 1 / span 6; }

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Placing first items on the grid .heading { grid-column: 1 / span 9; } .see-more { grid-column: 10 / span 3; } @media screen and (min-width: 48em)

Slide 55

Slide 55 text

Placing first items on the grid .heading { grid-column: 1 / span 9; } .see-more { grid-column: 10 / span 3; } @media screen and (min-width: 48em)

Slide 56

Slide 56 text

Explicitly place link in the first row .heading { grid-column: 1 / span 9; grid-row: 1; } .see-more { grid-column: 10 / span 3; grid-row: 1; } @media screen and (min-width: 48em)

Slide 57

Slide 57 text

Placing quotes on the grid @media screen and (min-width: 48em) .quote-1 { grid-column: 1 / span 12; } .quote-2 { grid-column: 1 / span 4; } .quote-3 { grid-column: 5 / span 4; } .quote-4 { grid-column: 9 / span 4; }

Slide 58

Slide 58 text

Placing quotes on the grid @media screen and (min-width: 48em) .quote-1 { grid-column: 1 / span 12; } .quote-2 { grid-column: 1 / span 4; } .quote-3 { grid-column: 5 / span 4; } .quote-4 { grid-column: 9 / span 4; }

Slide 59

Slide 59 text

DEMO: Grid 1 small screen

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Placing heading and link again .heading { grid-column: 1 / span 12; } .see-more { grid-column: 13 / span 3; } @media screen and (min-width: 64em)

Slide 62

Slide 62 text

Placing quotes .quote-1 { grid-column: 1 / span 12; } .quote-2 { grid-column: 13 / span 3; } .quote-3 { grid-column: 13 / span 3; } .quote-4 { grid-column: 13 / span 3; } @media screen and (min-width: 64em)

Slide 63

Slide 63 text

Spanning rows .quote-1 { grid-column: 1 / span 12; grid-row: 2 / span 3; } @media screen and (min-width: 64em)

Slide 64

Slide 64 text

DEMO: Grid 1 small screen

Slide 65

Slide 65 text

Adding flexbox media object
.media { display: flex; flex-direction: column; } .media-content { flex: 1 1 auto; } html css

Slide 66

Slide 66 text

DEMO: Grid 1 items with media

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Markup for grid 2

Slide 71

Slide 71 text

Medium screens: placing quotes .quote-5 { grid-column: 1 / span 6; } .quote-6 { grid-column: 7 / span 3; } .quote-7 { grid-column: 10 / span 3; } @media screen and (min-width: 48em)

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Large screens: placing quotes @media screen and (min-width: 64em) .quote-5 { grid-column: 9 / span 7; } .quote-6 { grid-column: 1 / span 4; } .quote-7 { grid-column: 5 / span 4; }

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Revisiting the implicit grid

Slide 78

Slide 78 text

CSS Grid auto placement • Explicit grid
 Rules dictated by grid-template-*, grid-column, grid-gap, etc. • Implicit grid
 CSS Grid creates new tracks for the items that fall out of explicitly defined bounds • grid-auto-flow
 The property that controls the auto-placement algorithm for the implicit grid

Slide 79

Slide 79 text

https://codepen.io/jessabean/pen/OxLwdB .demo-grid { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-auto-flow: row; } .item1 { grid-column: 2 / span 1; }

Slide 80

Slide 80 text

https://codepen.io/jessabean/pen/yzBqry .demo-grid { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-auto-flow: column; } .item1 { grid-column: 2 / span 1; }

Slide 81

Slide 81 text

https://codepen.io/jessabean/pen/yzBqry .item3 { grid-row: 3; } .demo-grid { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-auto-flow: column; } .item1 { grid-column: 2 / span 1; }

Slide 82

Slide 82 text

https://codepen.io/jessabean/pen/yzBqry .item3 { grid-row: 3; } .demo-grid { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-auto-flow: column; } .item1 { grid-column: 2 / span 1; }

Slide 83

Slide 83 text

https://codepen.io/jessabean/pen/jGNpRg .demo-grid { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-auto-flow: row dense; } .item1 { grid-column: 2 / span 1; }

Slide 84

Slide 84 text

https://codepen.io/jessabean/pen/YrzKLX .demo-grid { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-auto-flow: column dense; } .item1 { grid-column: 2 / span 1; } .item3 { grid-row: 3; }

Slide 85

Slide 85 text

Update auto-flow mode for Dave Chappelle’s grid .grid-2 { grid-auto-flow: row dense; }

Slide 86

Slide 86 text

DEMO: Final layout with both grids

Slide 87

Slide 87 text

We did it!!! Did we?

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

Nested grids aren’t aware of parent tracks .quote-5 { display: grid; }

Slide 90

Slide 90 text

Nested grids aren’t aware of parent tracks .quote-5 { display: grid; } grid-template-columns:?????;

Slide 91

Slide 91 text

Nested grid inception .quote-5 { display: grid; grid-template-columns: repeat(7, 1fr); } @media screen and (min-width: 48em) { .quote-5 { … } } @media screen and (min-width: 64em) { .quote-5 { … } }

Slide 92

Slide 92 text

Subgrid doesn’t exist in the Level 1 spec yet

Slide 93

Slide 93 text

Re-inventing the grid

Slide 94

Slide 94 text

We need to calculate “1fr” explicitly .grid { grid-template-columns: repeat(6, 1fr); grid-gap: 0.75em; } @media screen and (min-width: 48em) { .grid { grid-template-columns: repeat(12, 1fr); } } @media screen and (min-width: 64em) { .grid{ grid-template-columns: repeat(15, 1fr); grid-gap: 1.25em; } }

Slide 95

Slide 95 text

Step 1: The 3 magic ingredients

Slide 96

Slide 96 text

Establish settings for each breakpoint in a Sass map $grid-config: ( sm: ( ), md: ( ), lg: ( ) );

Slide 97

Slide 97 text

Establish settings for each breakpoint in a Sass map $grid-config: ( sm: ( ), md: ( ), lg: ( ) ); container: 20em, column-count: 6, gutter: 0.75em

Slide 98

Slide 98 text

Establish settings for each breakpoint in a Sass map $grid-config: ( sm: ( ), md: ( ), lg: ( ) ); container: 20em, column-count: 6, gutter: 0.75em container: 38em, column-count: 12, gutter: 0.75em container: 55em, column-count: 15, gutter: 1.25em

Slide 99

Slide 99 text

Step 2: Calculate “1fr”

Slide 100

Slide 100 text

A function to calculate the width of 1 column @function calc-column-width($container, $column-count, $gutter) { }

Slide 101

Slide 101 text

A function to calculate the width of 1 column @function calc-column-width($container, $column-count, $gutter) { } $total-gutters = ($column-count - 1) * $gutter;

Slide 102

Slide 102 text

A function to calculate the width of 1 column @function calc-column-width($container, $column-count, $gutter) { } $total-gutters = ($column-count - 1) * $gutter; $column-width = ($container - $total-gutters) / $column-count;

Slide 103

Slide 103 text

A function to calculate the width of 1 column @function calc-column-width($container, $column-count, $gutter) { } $total-gutters = ($column-count - 1) * $gutter; $column-width = ($container - $total-gutters) / $column-count; @return $column-width;

Slide 104

Slide 104 text

Step 3: Calculate column span

Slide 105

Slide 105 text

A function to calculate the column span // Calculate a fixed width that spans columns defined by CSS Grid @function span-columns($breakpoint, $column-span) { }

Slide 106

Slide 106 text

Assigning breakpoint settings object to a variable @function span-columns($breakpoint, $column-span) { } // Get the $grid-config settings for the specified breakpoint $settings: map-get($grid-config, $breakpoint); $settings: ( container-width: 20em, column-count: 6, gutter-size: 0.75em )

Slide 107

Slide 107 text

Fetching container width, column count, and gutter width @function span-columns($breakpoint, $column-span) { } … // Get the individual properties nested in the breakpoint $container: map-get($settings, container); $column-count: map-get($settings, column-count); $gutter: map-get($settings, gutter); $container: 20em; $totalcolumns: 6; $gutter: 0.75em;

Slide 108

Slide 108 text

Using our column width function @function span-columns($breakpoint, $column-span) { } … … // Calculate individual column size based on config settings $column-width: calc-column-width($container, $column-count, $gutter);

Slide 109

Slide 109 text

@function span-columns($breakpoint, $column-span) { } Calculating total width of spanned columns @return ($column-width * $column-span) + ($gutter * ($column-span - 1)); … … …

Slide 110

Slide 110 text

@function span-columns($breakpoint, $column-span) { } Calculating total width of spanned columns @return ($column-width * $column-span) + ($gutter * ($column-span - 1)); … … … width of columns

Slide 111

Slide 111 text

@function span-columns($breakpoint, $column-span) { } Calculating total width of spanned columns @return ($column-width * $column-span) + ($gutter * ($column-span - 1)); … … … width of columns width of gutters +

Slide 112

Slide 112 text

Step 4: Use span-columns function

Slide 113

Slide 113 text

Using our function to span our grid columns .media-2 { flex-direction: row; } .media-2 .media-image { flex-basis: span-columns(lg, 4); }

Slide 114

Slide 114 text

DEMO: Final FINAL layout with 2 grids

Slide 115

Slide 115 text

Progressive enhancement

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

Autoprefixer for CSS Grid: not recommended autoprefixer( { grid: false } ); IE limitations No auto-placement of grid items. No grid-gap support. No shorthand support for column start / end. No grid template areas.

Slide 118

Slide 118 text

Using feature queries to target CSS Grid at modern browsers @supports (display: grid) { // css grid styles go here }

Slide 119

Slide 119 text

Using feature queries to target CSS Grid at modern browsers @supports (display: grid) { // css grid styles go here } .grid { display: flex; }

Slide 120

Slide 120 text

Additional styles for the flexbox fallback .grid { display: flex; flex-wrap: wrap; flex-direction: column; margin-top: -0.375em; margin-left: -0.375em; margin-right: -0.375em; } .grid > * { flex: 1 1 auto; margin: 0.375em; }

Slide 121

Slide 121 text

Using our span-columns function to set width/flex-basis .grid { flex-direction: row; } .quote-1 { width: 100%; } .quote-2, .quote-3, .quote-4 { width: span-columns(md, 4); } @media screen and (min-width: 48em)

Slide 122

Slide 122 text

Setting widths at the largest breakpoint .quote-1 { width: span-columns(lg, 12); } .quote-2, .quote-3, .quote-4 { width: span-columns(lg, 3); } @media screen and (min-width: 64em)

Slide 123

Slide 123 text

Flexbox cannot handle 2-dimensional layout

Slide 124

Slide 124 text

Revisiting the flexbox design: a different experience for IE

Slide 125

Slide 125 text

There is a lot more to CSS Grid

Slide 126

Slide 126 text

• Naming column lines • grid-auto-rows/grid-auto-columns • grid-template-areas • auto-fill and auto-fit • minmax More CSS Grid features

Slide 127

Slide 127 text

• Source order and semantics are still important • We should use grid placement for visual display, but be mindful of document order for speech and keyboard navigation • Tab order will follow document order, not visual order CSS Grid and accessibility

Slide 128

Slide 128 text

You can use CSS Grid TODAY

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

https://codepen.io/jessabean/pen/dzgRZW

Slide 132

Slide 132 text

http://labs.jensimmons.com/2017/01-005.html

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

https://gridsetapp.com/specs/fonmon/?gridset=show

Slide 135

Slide 135 text

The definition of “grid system” will change

Slide 136

Slide 136 text

https://www.rachelandrew.co.uk/archives/2017/07/01/you-do-not-need-a-css-grid-based-grid-system/

Slide 137

Slide 137 text

– Miriam Suzanne http://oddbird.net/2017/06/28/susy3/ “If you have the browser-support matrix to start using the CSS Grid module directly, you should do it, and forget about Susy.”

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

No content

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

Resources • Rachel Andrew: https://gridbyexample.com/learn/ • Jen Simmons: http://jensimmons.com/post/feb-27-2017/learn-css-grid • MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout • Igalia: https://blogs.igalia.com/mrego/2015/02/25/grid-auto-placement-is-ready/

Slide 153

Slide 153 text

Thank you! Links to demo code https://glitch.com/~grid-prototype-refresh-dc
 
 https://github.com/jessabean/grid-prototype-refresh-dc Slides https://speakerdeck.com/jessabean/prototyping-layout-with-css-grid-refreshdc
 @jessabean | [email protected]