Refactoring (the way we talk about) CSS

A presentation at XML Prague in February 2020 in Prague, Czechia by Rachel Andrew

Slide 1

Slide 1

Refactoring (the way we talk about) CSS. Rachel Andrew @ XML Prague Slides & resources https://noti.st/rachelandrew

Slide 2

Slide 2

Doing things on the web since 1996 Co-founder Perch CMS & Notist. Editor in Chief Smashing Magazine. Writer of many books. CSS Working Group Member representing Fronteers. Spec editor Multicol and Page Floats. MDN tech writer. Slides & resources https://noti.st/rachelandrew

Slide 3

Slide 3

I thought that I had teaching CSS layout all figured out Slides & resources https://noti.st/rachelandrew

Slide 4

Slide 4

Talking about CSS as a layout system Slides & resources https://noti.st/rachelandrew

Slide 5

Slide 5

Talking about CSS as a layout system Slides & resources https://noti.st/rachelandrew

Slide 6

Slide 6

CSS can be explained in the same structured way we use for other languages. Slides & resources https://noti.st/rachelandrew

Slide 7

Slide 7

• • • • • • • • • Flow Layout Changing the value of display Out of flow elements Formatting Contexts Writing Modes Logical, flow-relative properties and values Alignment Sizing Media & Feature Queries Slides & resources https://noti.st/rachelandrew

Slide 8

Slide 8

Understanding display Slides & resources https://noti.st/rachelandrew

Slide 9

Slide 9

Normal Flow Block and Inline Layout Slides & resources https://noti.st/rachelandrew

Slide 10

Slide 10

Slides & resources https://noti.st/rachelandrew

Slide 11

Slide 11

CSS is doing work for us, before we write any CSS. Slides & resources https://noti.st/rachelandrew

Slide 12

Slide 12

Slides & resources https://noti.st/rachelandrew

Slide 13

Slide 13

.example { display: flex; } Slides & resources https://noti.st/rachelandrew

Slide 14

Slide 14

.example { display: grid; grid-template-columns: 1fr 1fr 1fr; } Slides & resources https://noti.st/rachelandrew

Slide 15

Slide 15

Changing the value of display changes that element and its direct children. Slides & resources https://noti.st/rachelandrew

Slide 16

Slide 16

Slides & resources https://noti.st/rachelandrew

Slide 17

Slide 17

The two values of display Slides & resources https://noti.st/rachelandrew

Slide 18

Slide 18

.example { display: block grid; grid-template-columns: 1fr 1fr 1fr; } Slides & resources https://noti.st/rachelandrew

Slide 19

Slide 19

.example { display: inline grid; grid-template-columns: 1fr 1fr 1fr; } Slides & resources https://noti.st/rachelandrew

Slide 20

Slide 20

The outer display type How the box behaves in the layout - block or inline Slides & resources https://noti.st/rachelandrew

Slide 21

Slide 21

The inner display type The formatting context of the direct children – grid, flex etc. Slides & resources https://noti.st/rachelandrew

Slide 22

Slide 22

Creating a new Block Formatting Context Slides & resources https://noti.st/rachelandrew

Slide 23

Slide 23

.box { background-color: rgb(43,91,128); } Slides & resources https://noti.st/rachelandrew

Slide 24

Slide 24

display: flow-root Slides & resources https://noti.st/rachelandrew

Slide 25

Slide 25

.box { background-color: rgb(43,91,128); display: flow-root; } Slides & resources https://noti.st/rachelandrew

Slide 26

Slide 26

Writing Modes Slides & resources https://noti.st/rachelandrew

Slide 27

Slide 27

writing-mode: horizontal-tb; Inline Dimension Block Dimension Slides & resources https://noti.st/rachelandrew

Slide 28

Slide 28

writing-mode: vertical-rl; Block Dimension Inline Dimension Slides & resources https://noti.st/rachelandrew

Slide 29

Slide 29

Block Start Block Start Block End Block End Slides & resources https://noti.st/rachelandrew

Slide 30

Slide 30

Inline Start Inline End Inline Start Inline End Slides & resources https://noti.st/rachelandrew

Slide 31

Slide 31

Web layout was tied to physical dimensions We think in top, right, bottom, left. Or width & height. Slides & resources https://noti.st/rachelandrew

Slide 32

Slide 32

.example { width: 600px; height: 300px; } Slides & resources https://noti.st/rachelandrew

Slide 33

Slide 33

Logical Properties & Values Slides & resources https://noti.st/rachelandrew

Slide 34

Slide 34

.example { inline-size: 600px; block-size: 300px; } Slides & resources https://noti.st/rachelandrew

Slide 35

Slide 35

inline-size = width block-size = height Slides & resources https://noti.st/rachelandrew

Slide 36

Slide 36

block-size = width inline-size = height Slides & resources https://noti.st/rachelandrew

Slide 37

Slide 37

Physical v. Logical .example { padding-top: 10px; padding-right: 2em; margin-bottom: 2em; } .example { padding-block-start: 10px; padding-inline-end: 2em; margin-block-end: 2em; margin-inline: 1em; } Slides & resources https://noti.st/rachelandrew

Slide 38

Slide 38

.example { border-start-start-radius: 20px; border-start-end-radius: 3em; border-end-start-radius: 2em 4em; border-end-end-radius: 5px; } Slides & resources https://noti.st/rachelandrew

Slide 39

Slide 39

We need to think in terms of this flow-relative, logical world. Slides & resources https://noti.st/rachelandrew

Slide 40

Slide 40

Box Alignment https://drafts.csswg.org/css-align/ Slides & resources https://noti.st/rachelandrew

Slide 41

Slide 41

Aligning things in the block and inline dimensions. Slides & resources https://noti.st/rachelandrew

Slide 42

Slide 42

Distribution of space and alignment of items within their space. Slides & resources https://noti.st/rachelandrew

Slide 43

Slide 43

Block Start Inline Start Slides & resources https://noti.st/rachelandrew

Slide 44

Slide 44

justify-content In Grid, inline dimension space distribution between tracks Slides & resources https://noti.st/rachelandrew

Slide 45

Slide 45

.example { justify-content: space-between; } Slides & resources https://noti.st/rachelandrew

Slide 46

Slide 46

align-content In Grid, block dimension space distribution between tracks Slides & resources https://noti.st/rachelandrew

Slide 47

Slide 47

.example { align-content: end; } Slides & resources https://noti.st/rachelandrew

Slide 48

Slide 48

In flexbox, we justify on the main axis and align on the cross axis Slides & resources https://noti.st/rachelandrew

Slide 49

Slide 49

justify-content In Flex, main axis space distribution between flex items Slides & resources https://noti.st/rachelandrew

Slide 50

Slide 50

.example { justify-content: flex-end; } Slides & resources https://noti.st/rachelandrew

Slide 51

Slide 51

align-content In Flex, cross axis space distribution between flex lines Slides & resources https://noti.st/rachelandrew

Slide 52

Slide 52

.example { align-content: space-around; } Slides & resources https://noti.st/rachelandrew

Slide 53

Slide 53

For –content properties to do anything, you must have spare space to distribute! Slides & resources https://noti.st/rachelandrew

Slide 54

Slide 54

Slides & resources https://noti.st/rachelandrew

Slide 55

Slide 55

.item { justify-self: end; align-self: end; } Slides & resources https://noti.st/rachelandrew

Slide 56

Slide 56

.example { justify-items: end; align-items: end; } Slides & resources https://noti.st/rachelandrew

Slide 57

Slide 57

“[justify-content] does not apply to flex items, because there is more than one item in the main axis.” Slides & resources https://noti.st/rachelandrew

Slide 58

Slide 58

.item { align-self: center; } Slides & resources https://noti.st/rachelandrew

Slide 59

Slide 59

“Prior to alignment via justifycontent and align-self, any positive free space is distributed to auto margins in that dimension.” Slides & resources https://noti.st/rachelandrew

Slide 60

Slide 60

.example div:last-child { margin-left: auto; } Slides & resources https://noti.st/rachelandrew

Slide 61

Slide 61

Let’s stop calling stuff that is in the spec a CSS ‘hack’ Slides & resources https://noti.st/rachelandrew

Slide 62

Slide 62

What about the Box Model? Slides & resources https://noti.st/rachelandrew

Slide 63

Slide 63

When we had to control the size of each item in a layout, the Box Model was key. Slides & resources https://noti.st/rachelandrew

Slide 64

Slide 64

Slides & resources https://noti.st/rachelandrew

Slide 65

Slide 65

500px Slides & resources https://noti.st/rachelandrew

Slide 66

Slide 66

30px + 500px + 30px Slides & resources https://noti.st/rachelandrew

Slide 67

Slide 67

5px + 30px + 500px + 30px + 5px Slides & resources https://noti.st/rachelandrew

Slide 68

Slide 68

40px + 5px + 30px + 500px + 30px + 5px + 40px Slides & resources https://noti.st/rachelandrew

Slide 69

Slide 69

What is the inline-size or width of the box? By default, the content-box Slides & resources https://noti.st/rachelandrew

Slide 70

Slide 70

If you want the specified width to include padding and border Set the box-sizing property to border-box. Slides & resources https://noti.st/rachelandrew

Slide 71

Slide 71

.example { box-sizing: border-box; } Slides & resources https://noti.st/rachelandrew

Slide 72

Slide 72

How big is that box? Slides & resources https://noti.st/rachelandrew

Slide 73

Slide 73

In the past everything was a length or a percentage. Slides & resources https://noti.st/rachelandrew

Slide 74

Slide 74

What is the minimum and maximum size of this thing? Slides & resources https://noti.st/rachelandrew

Slide 75

Slide 75

.example { grid-template-columns: min-content max-content; } Slides & resources https://noti.st/rachelandrew

Slide 76

Slide 76

Any content-based sizing is worked out based on these min and max content sizes. Slides & resources https://noti.st/rachelandrew

Slide 77

Slide 77

.example { display: flex; } Slides & resources https://noti.st/rachelandrew

Slide 78

Slide 78

.example > * { flex: auto; } Slides & resources https://noti.st/rachelandrew

Slide 79

Slide 79

.example > * { flex: auto; } Slides & resources https://noti.st/rachelandrew

Slide 80

Slide 80

.example > * { flex: 1; } Slides & resources https://noti.st/rachelandrew

Slide 81

Slide 81

Old browsers. They exist. Slides & resources https://noti.st/rachelandrew

Slide 82

Slide 82

We have a specification. Some of it isn’t implemented yet. Slides & resources https://noti.st/rachelandrew

Slide 83

Slide 83

Lack of support is very different to the buggy support of the past. Slides & resources https://noti.st/rachelandrew

Slide 84

Slide 84

Media & Feature Queries How big is my viewport? Is this a touchscreen? Does this browser support Grid? Respond based on the answers. Slides & resources https://noti.st/rachelandrew

Slide 85

Slide 85

We need to stop talking about CSS as a weird and quirky thing. Slides & resources https://noti.st/rachelandrew

Slide 86

Slide 86

CSS is different. Not wrong, not impossible to understand as a system. Slides & resources https://noti.st/rachelandrew

Slide 87

Slide 87

Learn and teach CSS as it is today. Slides & resources https://noti.st/rachelandrew

Slide 88

Slide 88

Thank you! @rachelandrew Slides & resources https://noti.st/rachelandrew