When Grids Go Wrong: Debugging CSS Grid

A presentation at London CSS in August 2019 in London, UK by Michelle Barker

Slide 1

Slide 1

When Grids Go Wrong (Debugging CSS Grid) @MicheBarks

Slide 2

Slide 2

• • • @MicheBarks Front End Developer at Ordoo Mozilla Tech Speaker Author of CSS { In Real Life }

Slide 3

Slide 3

.sidebar { float: left; width: 200px; } .clearfix { overflow: auto; } .clearfix::after { content: ”; clear: both; display: table; } @MicheBarks

Slide 4

Slide 4

CSS Grid @MicheBarks

Slide 5

Slide 5

CSS Grid @MicheBarks +

Slide 6

Slide 6

Scaffolding (building the grid) .grid { display: grid; grid-template-rows: repeat(4, 8rem); grid-template-columns: repeat(4, 12rem); gap: 1rem; } @MicheBarks

Slide 7

Slide 7

@MicheBarks

Slide 8

Slide 8

Placing items .a { grid-row: 1 / 3; grid-column: 1 / 4; } @MicheBarks

Slide 9

Slide 9

Placing items .a { grid-row: 1 / span 2; grid-column: 1 / span 3; } @MicheBarks

Slide 10

Slide 10

Placing items .a { grid-row: span 2 / 3; grid-column: span 3 / 4; } @MicheBarks

Slide 11

Slide 11

Placing items .a { grid-row: span 2; grid-column: span 3; } @MicheBarks

Slide 12

Slide 12

Placing items .grid { display: grid; grid-template-rows: [a-start] 8rem 8rem [a-end] 8rem 8rem; } grid-template-columns: repeat(4, 12rem); gap: 1rem; .a { grid-row: a; grid-column: 1 / 4; } @MicheBarks

Slide 13

Slide 13

Placing items .grid { display: grid; grid-template-rows: [a-start] 8rem 8rem [a-end] 8rem 8rem; grid-template-columns: repeat(4, 12rem); gap: 1rem; } .a { grid-row: a; } @MicheBarks grid-column: 1 / 4;

Slide 14

Slide 14

Placing items .grid { display: grid; grid-template-rows: [a-start] 8rem 8rem [a-end] 8rem 8rem; grid-template-columns: [a-start] 12rem 12rem 12rem [a-end] 12rem; } gap: 1rem; .a { grid-area: a; } @MicheBarks

Slide 15

Slide 15

Placing items .grid { display: grid; grid-template-rows: [a-start] 8rem 8rem [a-end] 8rem 8rem; grid-template-columns: [a-start] 12rem 12rem 12rem [a-end] 12rem; gap: 1rem; } .a { grid-area: a; } @MicheBarks

Slide 16

Slide 16

Placing items .a { grid-area: 1 / 1 / 3 / 4; } /* grid-row-start / grid-column-start / grid-row-end / grid-column-end */ @MicheBarks

Slide 17

Slide 17

Placing items .grid { display: grid; grid-template-rows: [a-start] 8rem 8rem [a-start] 8rem 8rem; grid-template-columns: [a-start] 12rem 12rem 12rem [a-end] 12rem; grid-template-areas: ‘a a a .’ ‘a a a .’ ‘… .’ ‘… .’; } @MicheBarks gap: 1rem;

Slide 18

Slide 18

@MicheBarks

Slide 19

Slide 19

Placing items .grid { display: grid; grid-template-rows: [a-start] 8rem 8rem [a-end] 8rem 8rem; grid-template-columns: [a-start] 12rem 12rem 12rem [a-end] 12rem; } @MicheBarks grid-template-areas: ‘… .’ ‘. . b b’ ‘. . b b’ ‘. . b b’; gap: 1rem;

Slide 20

Slide 20

Placing items .grid { display: grid; grid-template-rows: [a-start] 8rem 8rem [a-end] 8rem 8rem; grid-template-columns: [a-start] 12rem 12rem 12rem [a-end] 12rem; grid-template-areas: ‘… .’ ‘. . b b’ ‘. . b b’ ‘. . b b’; } @MicheBarks gap: 1rem;

Slide 21

Slide 21

Auto placement @MicheBarks

Slide 22

Slide 22

Auto placement @MicheBarks

Slide 23

Slide 23

Accidental implicit tracks @MicheBarks

Slide 24

Slide 24

grid-column: 3 / span 2; @MicheBarks

Slide 25

Slide 25

grid-column: 3 / span 3; @MicheBarks

Slide 26

Slide 26

grid-column: 3 / span 3; @MicheBarks

Slide 27

Slide 27

@MicheBarks

Slide 28

Slide 28

.grid { grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(3, 8rem); } .a { grid-column: span 4; grid-row: 1 / span 2; } .b { grid-column: span 4; grid-row: 2 / span 2; } @MicheBarks

Slide 29

Slide 29

.grid { grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 8rem); } .a { grid-column: span 4; } grid-row: 1 / span 2; .b { grid-column: span 4; } @MicheBarks grid-row: 2 / span 2;

Slide 30

Slide 30

.grid { grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 8rem); } .a { grid-column: span 4; grid-row: 1 / 3; } .b { grid-column: span 4; grid-row: 2 / 4; } @MicheBarks

Slide 31

Slide 31

@MicheBarks

Slide 32

Slide 32

@MicheBarks

Slide 33

Slide 33

.grid { grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 8rem); } .a { grid-column: span 4; } grid-row: 1 / span 2; .b { grid-column: span 4; } @MicheBarks grid-row: 2 / span 2;

Slide 34

Slide 34

.grid { grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 8rem); } .a { grid-column: 1 / span 4; } grid-row: 1 / span 2; .b { grid-column: 1 / span 4; } @MicheBarks grid-row: 2 / span 2;

Slide 35

Slide 35

.grid { grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 8rem); } .a { grid-column: 1 / -1; } grid-row: 1 / span 2; .b { grid-column: 1 / -1; } @MicheBarks grid-row: 2 / span 2;

Slide 36

Slide 36

Equal-width columns @MicheBarks

Slide 37

Slide 37

.grid { display: grid; grid-template-columns: repeat(4, 1fr); } @MicheBarks gap: 40px;

Slide 38

Slide 38

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

Slide 39

Slide 39

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

Slide 40

Slide 40

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

Slide 41

Slide 41

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

Slide 42

Slide 42

@MicheBarks

Slide 43

Slide 43

Extrinsic sizing Context-based @MicheBarks

Slide 44

Slide 44

Intrinsic sizing Content-based @MicheBarks

Slide 45

Slide 45

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

Slide 46

Slide 46

grid-template-columns: repeat(4, minmax(0, 1fr)); @MicheBarks

Slide 47

Slide 47

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

Slide 48

Slide 48

grid-template-columns: repeat(4, 0.5fr); @MicheBarks

Slide 49

Slide 49

grid-template-columns: repeat(4, 0.2fr); @MicheBarks

Slide 50

Slide 50

Alignment between Grid cells @MicheBarks

Slide 51

Slide 51

Align-items Align-content Justify-items Justify-content Align-self Justify-self @MicheBarks

Slide 52

Slide 52

@MicheBarks

Slide 53

Slide 53

align-items: center; @MicheBarks

Slide 54

Slide 54

display: grid; display: flex; @MicheBarks

Slide 55

Slide 55

@MicheBarks

Slide 56

Slide 56

@MicheBarks

Slide 57

Slide 57

.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; } .item { grid-row: span 3; display: grid; grid-template-rows: subgrid; } @MicheBarks

Slide 58

Slide 58

When not to use Grid @MicheBarks

Slide 59

Slide 59

⚠ Tables @MicheBarks

Slide 60

Slide 60

❌ table { display: grid; } @MicheBarks

Slide 61

Slide 61

❌ tr { display: grid; } @MicheBarks

Slide 62

Slide 62

⚠ When you don’t want items to align in rows or columns @MicheBarks

Slide 63

Slide 63

Flexbox @MicheBarks

Slide 64

Slide 64

Grid @MicheBarks

Slide 65

Slide 65

⚠ When your fallback layout takes twice as long @MicheBarks

Slide 66

Slide 66

This won’t work in IE11 .grid { display: grid; grid-template-columns: repeat(3, 1fr); } @supports not (display: grid) { .grid { display: flex; flex-wrap: wrap; } } @MicheBarks

Slide 67

Slide 67

This will work .grid { display: flex; flex-wrap: wrap; } @supports (display: grid) { .grid { display: grid; grid-template-columns: repeat(3, 1fr); } } @MicheBarks

Slide 68

Slide 68

CSS { In Real Life } css-irl.info @MicheBarks