What’s new in CSS?

A presentation at SydCSS in November 2022 in Sydney NSW, Australia by Rachel Andrew

Slide 1

Slide 1

What’s new in CSS SydCSS 2022

Slide 2

Slide 2

Rachel Andrew Technical writer for Chrome. Content lead web.dev and developer.chrome.com. CSS Working Group member.

Slide 3

Slide 3

CSS is amazing right now!

Slide 4

Slide 4

Slide 5

Slide 5

Use your brand color in form inputs. accent-color

Slide 6

Slide 6

input { accent-color: var(—blue-sapphire); }

Slide 7

Slide 7

Use accent-color now!

Slide 8

Slide 8

New color functions color-mix()

Slide 9

Slide 9

li { background-color: color-mix(in lch, white 10%, darkblue); }

Slide 10

Slide 10

Try out color-mix() In Safari Technology Preview enable CSS color-mix() in the develop menu In Firefox, in about:config enable layout.css.color-mix.enabled

Slide 11

Slide 11

Solving font layout shifts size-adjust, ascent-override, descent-override, line-gap-override

Slide 12

Slide 12

@font-face { font-family: “fallback”; src: local(Arial); size-adjust: 90%; ascent-override: 110%; descent-override: 110%; line-gap-override: 99%; } @font-face { font-family: “Webfont”; src: url(/path/to/webfont.woff2) format(‘woff2’); } html { font-family: “Webfont”, “fallback”; }

Slide 13

Slide 13

Use size-adjust, ascent-override, descent-override, and line-gap-override now!

Slide 14

Slide 14

Controlling specificity @layer

Slide 15

Slide 15

@layer base special @layer special { .item { color: rebeccapurple; } } @layer base { .item { color: green; } }

Slide 16

Slide 16

Use Cascade Layers now!

Slide 17

Slide 17

Small, large, and dynamic viewport units Units that account for mobile viewports with and without UI elements.

Slide 18

Slide 18

Viewport concepts • Large viewport: the viewport with no dynamic UI elements visible. • Small viewport: the viewport with the dynamic UI elements visible.

Slide 19

Slide 19

.wrapper { block-size: 100lvh; /* large / } .wrapper { block-size: 100svh; / small / } .wrapper { block-size: 100dvh; / dynamic */ }

Slide 20

Slide 20

Slide 21

Slide 21

Use the new viewport units now!

Slide 22

Slide 22

Aspect Ratios aspect-ratio

Slide 23

Slide 23

The “padding hack” https://css-tricks.com/aspect-ratio-boxes/

Slide 24

Slide 24

aspect-ratio: 16/9;

Slide 25

Slide 25

Slide 26

Slide 26

.wrapper { display: grid; grid-template-columns: 200px 350px; } .box16x9 { aspect-ratio: 16/9; } .box4x3 { aspect-ratio: 4/3; }

Slide 27

Slide 27

Slide 28

Slide 28

1fr 1fr 1fr

Slide 29

Slide 29

Use aspect-ratio now!

Slide 30

Slide 30

Subgrid Allow nested grids to inherit the track sizing from the parent.

Slide 31

Slide 31

.container { display: grid; grid-template-columns: 1fr 1fr 1fr; } .container .item { grid-column: 1 / 3; display: grid; grid-template-columns: subgrid; }

Slide 32

Slide 32

.container figure { display: grid; grid-template-rows: subgrid; } .caption { grid-row: 2; }

Slide 33

Slide 33

Subgrid

Slide 34

Slide 34

wpt.fyi/interop-2022 - November 2022

Slide 35

Slide 35

CSS Containment This box and children are independent from the rest of the layout.

Slide 36

Slide 36

.sidebar { contain: layout; }

Slide 37

Slide 37

Use the contain property now!

Slide 38

Slide 38

Container Queries Design components that can react to the available space in their container.

Slide 39

Slide 39

Slide 40

Slide 40

.sidebar { container-type: inline-size; }

Slide 41

Slide 41

Using container-type creates a containment context. This also turns on layout, style, and inline-size containment, in the same way as using the contain property does.

Slide 42

Slide 42

.sidebar { container-type: inline-size; container-name: sidebar; }

Slide 43

Slide 43

@container (min-width: 400px) { .card { display: grid; grid-template-columns: 1fr 2fr; } }

Slide 44

Slide 44

@container sidebar (min-width: 400px) { .card { display: grid; grid-template-columns: 1fr 2fr; } }

Slide 45

Slide 45

Slide 46

Slide 46

.wrapper > div { container-type: inline-size; } @container (min-width: 220px) { .box { background-color: var(—blue-sapphire); } }

Slide 47

Slide 47

.fig-panel { display: grid; grid-template-columns: 1fr 1fr; }

Slide 48

Slide 48

@container (min-width: 700px) { grid-template-columns: repeat(3,1fr); }

Slide 49

Slide 49

Container Queries

Slide 50

Slide 50

This seems so simple! Why did it take so long?

Slide 51

Slide 51

Slide 52

Slide 52

Slide 53

Slide 53

Putting it together Container Queries and subgrid

Slide 54

Slide 54

@container grid (min-width: 600px) { .item { display: grid; grid-template-columns: subgrid; } }

Slide 55

Slide 55

Thank you! Find me @rachelandrew Slides at: https://noti.st/rachelandrew