Slide 1

Slide 1 text

LET’S LEARN CSSGRID

Slide 2

Slide 2 text

Hi there! I’m Tim Smith

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Agenda

Slide 7

Slide 7 text

Agenda 1. Problems we’ve had with layout

Slide 8

Slide 8 text

Agenda 1. Problems we’ve had with layout 2. What is CSS Grid and how does it solve these problems?

Slide 9

Slide 9 text

Agenda 1. Problems we’ve had with layout 2. What is CSS Grid and how does it solve these problems? 3. Why do I need to learn this?

Slide 10

Slide 10 text

Agenda 1. Problems we’ve had with layout 2. What is CSS Grid and how does it solve these problems? 3. Why do I need to learn this? 4. Resources

Slide 11

Slide 11 text

A History of Layout Issues

Slide 12

Slide 12 text

Floats

Slide 13

Slide 13 text

Floats Suck

Slide 14

Slide 14 text

CSS Frameworks

Slide 15

Slide 15 text

CSS Frameworks

Slide 16

Slide 16 text

Separation of Concerns

Slide 17

Slide 17 text

Floats Aren’t Bad

Slide 18

Slide 18 text

They’re Not for Complex Layouts

Slide 19

Slide 19 text

Why?

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Flexbox

Slide 22

Slide 22 text

Flexbox is Flexible

Slide 23

Slide 23 text

Main Column and Sidebar Layout Aside Main Content

Slide 24

Slide 24 text

Main Column and Sidebar Layout Aside Main Content And our CSS.

Slide 25

Slide 25 text

Main Column and Sidebar Layout Aside Main Content And our CSS. body { display: flex; } aside { background-color: #eee; flex: 1 1 30%; } main { background-color: #ddd; flex: 1 1 70%; }

Slide 26

Slide 26 text

The two columns are completely flexible.

Slide 27

Slide 27 text

Flexbox Card Layout
  • 1
  • 2
  • 3
  • 4
  • 5

Slide 28

Slide 28 text

Flexbox Card Layout
  • 1
  • 2
  • 3
  • 4
  • 5
And now our CSS.

Slide 29

Slide 29 text

Flexbox Card Layout
  • 1
  • 2
  • 3
  • 4
  • 5
And now our CSS. .boxes { display: flex; flex-wrap: wrap; } .boxes li { flex: 1 1 30%; background: #eee; margin: 0 .5rem 1rem; }

Slide 30

Slide 30 text

This is not what I want!

Slide 31

Slide 31 text

Math

Slide 32

Slide 32 text

You Need Stuff Like This li:nth-child(2n+2) { margin-right: 0; }

Slide 33

Slide 33 text

You Need Stuff Like This li:nth-child(2n+2) { margin-right: 0; } Oh wait! What about bigger screens?

Slide 34

Slide 34 text

You Need Stuff Like This li:nth-child(2n+2) { margin-right: 0; } Oh wait! What about bigger screens? @media screen and (min-width: 40em) { li:nth-child(2n+2) { margin-right: 1.5rem; } li:nth-child(3n+3) { margin-right: 0; } }

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Flexbox Is Awesome

Slide 37

Slide 37 text

Vertical Align!

Slide 38

Slide 38 text

We’re Using Flexbox Wrong

Slide 39

Slide 39 text

CSS Grid to the Rescue

Slide 40

Slide 40 text

CSS Grid is Not a Float Killer

Slide 41

Slide 41 text

CSS Grid is Not a Flexbox Killer

Slide 42

Slide 42 text

You Don’t Need a Framework

Slide 43

Slide 43 text

CSS Grid Is the Framework

Slide 44

Slide 44 text

CSS Grid is Friendly

Slide 45

Slide 45 text

Great Use of Flexbox

Slide 46

Slide 46 text

Great Use of Floats

Slide 47

Slide 47 text

CSS Grid What Is It Good For?

Slide 48

Slide 48 text

“My super simple theory at the moment — if you are putting widths on flex items, you are doing it wrong. Use Grid. (Let’s see if that holds.) —Jen Simmons

Slide 49

Slide 49 text

CSS Grid Basics

Slide 50

Slide 50 text

display: grid;

Slide 51

Slide 51 text

grid-template-columns

Slide 52

Slide 52 text

fr

Slide 53

Slide 53 text

grid-gap

Slide 54

Slide 54 text

Let’s See It in Action

Slide 55

Slide 55 text

Holy Grail Layout

Slide 56

Slide 56 text

Set Up Header Aside Main Content Footer

Slide 57

Slide 57 text

Set Up Header Aside Main Content Footer body { display: grid; grid-template-columns: 2fr 3fr; grid-gap: 2rem; }

Slide 58

Slide 58 text

Let’s Place Our Items header { grid-column: 1 / span 2; } aside { grid-column-end: span 1; grid-row: 2; } main { grid-column: 2 / span 1; grid-row: 2; } footer { grid-column: 1 / span 2; grid-row: 3; }

Slide 59

Slide 59 text

Grid Lines

Slide 60

Slide 60 text

Rewrite with Grid Areas body { display: grid; grid-template-columns: 2fr 3fr; grid-gap: 2rem; grid-template-areas: "header header" "aside main" "footer footer"; } header { grid-area: header; } aside { grid-area: aside; } main { grid-area: main; } footer { grid-area: footer; }

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

What If? body { grid-template-areas: ". header" "aside main" ". footer"; }

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Easily Responsive body { display: grid; grid-gap: 2rem; grid-template-areas: "header" "main" "aside" "footer"; } @media and screen (min-width: 40em) { body { grid-template-columns: 2fr 3fr; grid-template-areas: "header header" "aside main" "footer footer"; } }

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Card Layout

Slide 68

Slide 68 text

Setup
  • Box 1
  • Box 2
  • Box 3
  • Box 4
  • Box 5
  • Box 6

Slide 69

Slide 69 text

Setup
  • Box 1
  • Box 2
  • Box 3
  • Box 4
  • Box 5
  • Box 6
Now let’s get these puppies on a Grid.

Slide 70

Slide 70 text

Setup
  • Box 1
  • Box 2
  • Box 3
  • Box 4
  • Box 5
  • Box 6
Now let’s get these puppies on a Grid. .boxes { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); grid-gap: 1rem; }

Slide 71

Slide 71 text

Whoa! Hold Up.

Slide 72

Slide 72 text

Let’s Dissect This .boxes { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); grid-gap: 1rem; }

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Mind. Blown.

Slide 75

Slide 75 text

Special Portfolio-Inspired Layout

Slide 76

Slide 76 text

Setup
  • Item One
  • Item Two
  • Item Three
  • Item Four
  • Item Five

Slide 77

Slide 77 text

Setup
  • Item One
  • Item Two
  • Item Three
  • Item Four
  • Item Five
And setup our Grid container.

Slide 78

Slide 78 text

Setup
  • Item One
  • Item Two
  • Item Three
  • Item Four
  • Item Five
And setup our Grid container. .portfolio { display: grid; grid-auto-rows: minmax(100px, auto); grid-gap: 2px; grid-template-columns: repeat(6, 1fr); }

Slide 79

Slide 79 text

grid-auto-rows

Slide 80

Slide 80 text

Let’s Place Our Items .portfolio li:nth-child(1) { grid-column: 1 / -1; } .portfolio li:nth-child(2) { grid-column: 1 / span 2; grid-row-end: span 3; } .portfolio li:nth-child(3) { grid-column: 3 / span 4; grid-row-end: span 2; } .portfolio li:nth-child(4), .portfolio li:nth-child(5) { grid-column: span 2; }

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Holy Jack City, Batman

Slide 83

Slide 83 text

The Power of CSS Grid

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

Why Now?

Slide 86

Slide 86 text

It’s Our Job

Slide 87

Slide 87 text

Simpler Code

Slide 88

Slide 88 text

New Amazing Layouts

Slide 89

Slide 89 text

I Delayed Learning Flexbox and Regret It

Slide 90

Slide 90 text

Feature Queries

Slide 91

Slide 91 text

Like This @supports (display:grid) { /* Grid CSS in here */ } @supports not (display:grid) { /* Fallback CSS here */ }

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Browsers Ignore CSS They Don’t Understand

Slide 94

Slide 94 text

What Did We Learn?

Slide 95

Slide 95 text

1 CSS Grid Is Awesome

Slide 96

Slide 96 text

2 CSS Grid Is Friendly

Slide 97

Slide 97 text

3 Use CSS Grid Today

Slide 98

Slide 98 text

Only the Beginning

Slide 99

Slide 99 text

Rachel Andrew gridbyexample.com

Slide 100

Slide 100 text

Jen Simmons labs.jensimmons.com

Slide 101

Slide 101 text

Mozilla Developer Network developer.mozilla.org

Slide 102

Slide 102 text

Questions? ttimsmith.com · @smithtimmytim

Slide 103

Slide 103 text

Thanks! ttimsmith.com · @smithtimmytim