Refactoring (the way we talk about) CSS. · Slides & resources Refactoring (the way we talk about)...

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

Transcript of Refactoring (the way we talk about) CSS. · Slides & resources Refactoring (the way we talk about)...

Page 1: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Refactoring (the way we talk about) CSS.

Rachel Andrew @ CSS Day

Page 2: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

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.

Page 3: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

I thought that I had teaching CSS layout all

figured out

Page 4: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Talking about CSS as a layout system

Page 5: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Teaching CSS in the same structured way we would any

other language.

Page 6: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

• Flow Layout• Changing the value of display• Out of flow elements• Block Formatting Contexts• Writing Modes• Logical, flow-relative properties and values• Alignment• Sizing• Media & Feature Queries

Page 7: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Understanding display

Page 8: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Normal FlowBlock and Inline Layout

Page 9: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Page 10: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

CSS is doing work for us, before we write any CSS.

Page 11: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Page 12: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {display: flex;

}

Page 13: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {display: grid;grid-template-columns: 1fr 1fr 1fr;

}

Page 14: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Changing the value of display changes that element and its

direct children.

Page 15: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Page 16: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

The two values of display

Page 17: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {display: block grid;grid-template-columns: 1fr 1fr 1fr;

}

Page 18: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {display: inline grid;grid-template-columns: 1fr 1fr 1fr;

}

Page 19: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

The outer display typeHow to box behaves in the layout - block or inline

Page 20: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

The inner display typeThe formatting context of the direct children – grid, flex etc.

Page 21: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Busting out of flow

Page 22: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

position

Page 23: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.position {position: absolute;

}

Page 24: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

float

Page 25: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.box {background-color: rgb(43,91,128);

}

Page 26: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

display: flow-root

Page 27: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Creating a new Block Formatting Context

Page 28: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.box {background-color: rgb(43,91,128);display: flow-root;

}

Page 29: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Writing Modes

Page 30: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Block Dimension

writing-mode: horizontal-tb;

Inline Dimension

Page 31: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

InlineDimension

writing-mode: vertical-rl;

Block Dimension

Page 32: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Block Start

BlockStart

BlockE

ndBlock End

BlockE

nd

Page 33: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Inline Start

InlineE

nd

InlineStart

Inline End

Page 34: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Web layout was tied tophysical dimensions

We think in top, right, bottom, left. Or width & height.

Page 35: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {width: 600px;height: 300px;

}

Page 36: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Logical Properties & Values

Page 37: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {inline-size: 600px;block-size: 300px;

}

Page 38: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

block-size =

height

inline-size = width

Page 39: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

inline-size = heig

ht

block-size = width

Page 40: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

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

}

Physical v. Logical

Page 41: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {border-start-start-radius: 20px;border-start-end-radius: 3em;border-end-start-radius: 2em 4em;border-end-end-radius: 5px;

}

Page 42: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

We need to teach this flow-relative, logical world.

Page 43: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Box Alignmenthttps://drafts.csswg.org/css-align/

Page 44: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Aligning things in the blockand inline dimensions.

Page 45: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Distribution of space and alignment of items within

their space.

Page 46: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Block Start

InlineStart

Page 47: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

justify-contentIn Grid, inline dimension space distribution between tracks

Page 48: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {justify-content: space-between;

}

Page 49: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

align-contentIn Grid, block dimension space distribution between tracks

Page 50: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {align-content: end;

}

Page 51: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

In flexbox, we justify on the main axis and

align on the cross axis

Page 52: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

justify-contentIn Flex, main axis space distribution between flex items

Page 53: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {justify-content: flex-end;

}

Page 54: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

align-contentIn Flex, cross axis space distribution between flex lines

Page 55: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {align-content: space-around;

}

Page 56: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

For –content properties to do anything, you must have spare

space to distribute!

Page 57: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Page 58: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.item {justify-self: end;align-self: end;

}

Page 59: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {justify-items: end;align-items: end;

}

Page 60: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

“[justify-content] does not apply to flex items, because there is more than one item in the main axis.”

Page 61: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.item {align-self: center;

}

Page 62: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

“Prior to alignment via justify-content and align-self, any positive free space is distributed to auto margins in that dimension.”

Page 63: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example div:last-child {margin-left: auto;

}

Page 64: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Let’s stop calling stuff that isin the spec a CSS ‘hack’

Page 65: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Safe and Unsafe alignment

Avoiding CSS data loss

Page 66: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {display: flex;flex-direction: column;align-items: center;

}

Page 67: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {display: flex;flex-direction: column;align-items: safe center;

}

Page 68: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Box Sizinghttps://drafts.csswg.org/css-sizing-3/

Page 69: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

What about the Box Model?

Page 70: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

When we had to control the size of each item in a layout,

the Box Model was key.

Page 71: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Page 72: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

500px

Page 73: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

30px + 500px + 30px

Page 74: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

5px + 30px + 500px + 30px + 5px

Page 75: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

40px + 5px + 30px + 500px + 30px + 5px + 40px

Page 76: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

What is the inline-size or width of the box?

By default, the content-box

Page 77: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

If you want the specified width to include padding

and borderSet the box-sizing property to border-box.

Page 78: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {box-sizing: border-box;

}

Page 79: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

How big is that box?

Page 80: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

In the past everything was a length or a percentage.

Page 81: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

What is the minimum and maximum size of this thing?

Page 82: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {grid-template-columns: min-content max-content;

}

Page 83: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Any content-based sizing is worked out based on these min and max content sizes.

Page 84: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example {display: flex;

}

Page 85: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example > * {flex: auto;

}

Page 86: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example > * {flex: auto;

}

Page 87: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

.example > * {flex: 1;

}

Page 88: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Old browsers. They exist.

Page 89: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

We have a specification. Some of it isn’t implemented yet.

Page 90: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Lack of support is very different to the buggy

support of the past.

Page 91: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Media & Feature QueriesHow big is my viewport? Is this a touchscreen? Does this browser support Grid? Respond based on the answers.

Page 92: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

We need to stop talking about and teaching CSS as a weird

and quirky thing.

Page 93: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

CSS is unlike other languages because it serves

environments like no other.

Page 94: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Teach CSS as it is today.

Page 95: Refactoring (the way we talk about) CSS. · Slides & resources  Refactoring (the way we talk about) CSS. Rachel Andrew @ CSS Day

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

Thank you!@rachelandrew