Intrinsic CSS with Container Queries & Units

A presentation at Jamstack Conf in November 2022 in San Francisco, CA, USA by Miriam Suzanne

Slide 1

Slide 1

Intrinsic CSS With Container Queries & Units Miriam Suzanne Jamstack Conf 2022 @mia@front-end.social

Slide 2

Slide 2

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

»»1989

Slide 6

Slide 6

Slide 7

Slide 7

Slide 8

Slide 8

Slide 9

Slide 9

Line Mode Browser, developed by Nicola Pellow

Slide 10

Slide 10

“Web for all. Web on everything. – W3C Mission Statement

Slide 11

Slide 11

Slide 12

Slide 12

Slide 13

Slide 13

Slide 14

Slide 14

Slide 15

Slide 15

Slide 16

Slide 16

Slide 17

Slide 17

Slide 18

Slide 18

Slide 19

Slide 19

“We’re designing unknown content with unknown collaborators on an infinite and unknowable canvas, across operating systems, interfaces, writing-modes, & languages… —Me 👋

Slide 20

Slide 20

Slide 21

Slide 21

Slide 22

Slide 22

@media Queries 2008/2009

Slide 23

Slide 23

Responsive Web Design™ Ethan Marcotte An evolution of the already-responsive web…

Slide 24

Slide 24

• Fluid Grids (%-based) • Flexible Images (%-based) • Media Queries

Slide 25

Slide 25

Slide 26

Slide 26

Slide 27

Slide 27

Container Queries???!?1?! Please? If we promise to be good?

Slide 28

Slide 28

“No. Never gonna happen. —Browsers

Slide 29

Slide 29

Slide 30

Slide 30

Slide 31

Slide 31

Back To The [Right Now] 2010s » 2020s

Slide 32

Slide 32

• • • • • • Flexbox & Grid Box Alignment Viewport Units Intrinsic Sizing Aspect Ratios Min / Max / Clamp / &c.

Slide 33

Slide 33

Intrinsic Web Design™ Jen Simmons The responsive web keeps evolving…

Slide 34

Slide 34

• • • • • • Truly Two-Dimensional Layouts Combine Fluid & Fixed Stages of Squishiness Nested Contexts Expand & Contract Content Media Queries, As Needed

Slide 35

Slide 35

• • • • • • Truly Two-Dimensional Layouts Combine Fluid & Fixed Stages of Squishiness Nested Contexts Expand & Contract Content Media Queries, As Needed

Slide 36

Slide 36

Slide 37

Slide 37

🚧 Laying Foundations 🚧 2010s » 2020s

Slide 38

Slide 38

2020 Proposals • David Baron: @container • Brian Kardell: switch()

Slide 39

Slide 39

@container

Slide 40

Slide 40

CSS Containment Size, Layout, Style, Paint Avoid internal impacts on external elements…

Slide 41

Slide 41

⚠ Size Containment Removes Intrinsic Sizing

Slide 42

Slide 42

Slide 43

Slide 43

Slide 44

Slide 44

Slide 45

Slide 45

Single-Axis Containment? We need it

Slide 46

Slide 46

Slide 47

Slide 47

contain: inline-size block-size Thanks to Ian Kilpatrick

Slide 48

Slide 48

Containment Gotchas • Inline-size is never intrinsic (content-sized) • Subgrids can’t contribute track sizing • Counters are contained

Slide 49

Slide 49

“Yeah, ok. —Browsers

Slide 50

Slide 50

😭 It’s Impossible

Slide 51

Slide 51

🥳 It’s Shipping

Slide 52

Slide 52

August-September 2022 » Chrome/Edge 105 & Safari 16

Slide 53

Slide 53

Define Containers Elements we want to query

Slide 54

Slide 54

contain: layout size style We can do better (more declarative)

Slide 55

Slide 55

container-type: inline-size normal | size* | inline-size* *Make sure the sizing is extrinsic (block elements stretch by default)

Slide 56

Slide 56

No Default Container We need something to query

Slide 57

Slide 57

html { container-type: inline-size; } Similar to @media, but em/rem queries use actual root font-size

Slide 58

Slide 58

html, main, aside { container-type: inline-size; }

Slide 59

Slide 59

Container Names Optional and unlimited

Slide 60

Slide 60

html { container-type: inline-size; container-name: layout root; } Use names like unique IDs or reusable classes

Slide 61

Slide 61

html { container-name: layout root; } main { container-name: layout content; } .card { container-name: component card; } Use names like unique IDs or reusable classes

Slide 62

Slide 62

html { container: layout root / inline-size; } Name is required in the shorthand, but type is optional

Slide 63

Slide 63

Query Containers Depending on context

Slide 64

Slide 64

@media (min-width: 40em) { .card { /* … / } h2 { / … */ } }

Slide 65

Slide 65

@container (min-width: 40em) { .card { /* … / } h2 { / … */ } }

Slide 66

Slide 66

@container (width > 40em) { .card { /* … / } h2 { / … */ } }

Slide 67

Slide 67

Each Matched Element Queries the Nearest Ancestor With the Necessary Container-Type

Slide 68

Slide 68

Slide 69

Slide 69

Nest Containers Each will query the next one up

Slide 70

Slide 70

Eric Portis (CodePen)

Slide 71

Slide 71

Query Named Containers When it matters

Slide 72

Slide 72

@container card (width > 40em) { h2 { /* … */ } }

Slide 73

Slide 73

Each Matched Element Queries the Nearest Ancestor With the Necessary Container-Type And Container-Name

Slide 74

Slide 74

David Herron (CodePen)

Slide 75

Slide 75

Una Kravets (CodePen)

Slide 76

Slide 76

Jhey Tompkins (CodePen)

Slide 77

Slide 77

⚠ Grid & Flexbox Tracks Don’t Match the Container Size

Slide 78

Slide 78

<section class=grid> <article class=card>…</article> <article class=card>…</article> <article class=card>…</article> </section>

Slide 79

Slide 79

Slide 80

Slide 80

<section class=grid> <div class=card-container> <article class=card>…</article> </div> … </section>

Slide 81

Slide 81

Slide 82

Slide 82

Web Component :host Containers built in

Slide 83

Slide 83

Max Böck (CodePen)

Slide 84

Slide 84

Container Query Units cqw | cqh | cqi | cqb | cqmin | cqmax

Slide 85

Slide 85

Fallback to Small Viewport When no container is available

Slide 86

Slide 86

h2{ font-size: max(1.25rem, 12cqi - 1rem); } cqi » container query inline

Slide 87

Slide 87

Scott Kellum (CodePen)

Slide 88

Slide 88

Val Head (CodePen)

Slide 89

Slide 89

Slide 90

Slide 90

Firefox is Coming Soon* ffi *my uno cial read of the issue status & recent commits

Slide 91

Slide 91

@supports not (container: name) { /* fallback */ } Test for support

Slide 92

Slide 92

There’s a Polyfill fi GoogleChromeLabs/container-query-poly ll

Slide 93

Slide 93

Container Style Queries?? Work is underway…

Slide 94

Slide 94

@container style(font-style: italic) { em { background: var(—highlight); } }

Slide 95

Slide 95

@container style(—arrow: bottom-right) { .tooltip::after { inset-block-start: 100%; inset-inline-end: 1em; } }

Slide 96

Slide 96

Slide 97

Slide 97

Prototype in Chromium Canary Currently only works with custom properties fl about:// ags » Experimental Web Platform features

Slide 98

Slide 98

More Query Features?? What would you ask a container?

Slide 99

Slide 99

@container state(is-stuck) { /* when position:sticky applies? */ }

Slide 100

Slide 100

@container state(is-snapped) { /* when scroll-snap applies? */ }

Slide 101

Slide 101

“Our medium is not done. Our medium is still going through radical changes. —Jen Simmons

Slide 102

Slide 102

Miriam Suzanne Jamstack Conf 2022 @mia@front-end.social