Scaling CSS Layout Beyond Pixels

A presentation at beyond tellerrand in May 2022 in Düsseldorf, Germany by Stephanie Eckles

Slide 1

Slide 1

Scaling CSS Layout Beyond Pixels Stephanie Eckles @5t3ph • ModernCSS.dev

Slide 2

Slide 2

fl quite literally make or break your layout. In this new world, strict pixel values are so Web 2.0. Let’s review modern CSS techniques for building

Slide 3

Slide 3

the users’ browsing environment is not predictable

Slide 4

Slide 4

Started from the farm, now I’m here

Slide 5

Slide 5

.stage { width: 800px; height: 600px; display: grid; place-items: center; grid-template-areas: “stage”; overflow: hidden; & > * { grid-area: stage; text-align: center; } }

Slide 6

Slide 6

Evolution of Web Design fl fi fi Small, nite, absolute canvas In nite, exible, adaptive canvas

Slide 7

Slide 7

Jen Simmons “Getting Out Of Our Ruts” - An Event Apart, 2016

Slide 8

Slide 8

Jen Simmons “Getting Out Of Our Ruts” - An Event Apart, 2016

Slide 9

Slide 9

Jen Simmons “Everything You Know About Web Design Just Changed” - View Source, 2018

Slide 10

Slide 10

Jen Simmons “Everything You Know About Web Design Just Changed” - View Source, 2018

Slide 11

Slide 11

Jen Simmons “Everything You Know About Web Design Just Changed” - View Source, 2018

Slide 12

Slide 12

  1. Contracting & Expanding how designs adapt to available space

Slide 13

Slide 13

  1. Flexibility allowing variable rate adaptations

Slide 14

Slide 14

  1. Viewport purposely using viewport edges & units

Slide 15

Slide 15

fi Rachel Andrew “Making Things Better: Rede ning the Technical Possibilities of CSS” - An Event Apart, 2019

Slide 16

Slide 16

these are ok, actually? (but we can build them better)

Slide 17

Slide 17

the following techniques are guaranteed* to surprise and delight * not a legally-binding guarantee, YMMV, etc, etc your designers you

Slide 18

Slide 18

using adaptive layout techniques is a trust exercise between devs, designers, and the browser

Slide 19

Slide 19

flexibility within constraints

Slide 20

Slide 20

find comfort with variability adapt to the environment

Slide 21

Slide 21

clamp() the slinky of web design Global 90% Chromium v79 Firefox v75 Safari v13.1

Slide 22

Slide 22

clamp(min, ideal, max max) min ideal

Slide 23

Slide 23

font-size: 1rem ideal, 4vw max) 3rem clamp(min,

Slide 24

Slide 24

padding: 1rem ideal, 3% 1.5rem clamp(min, max) width: clamp(min, 30ch ideal, 80% max) 80ch

Slide 25

Slide 25

using clamp() for intrinsic design , y ya gn i s de ns! e k to ✓ designers provide min and max ✓ devs determine dynamic dimensions

Slide 26

Slide 26

min() and max() context-dependent options Global 90% Chromium v79 Firefox v75 Safari v13.1

Slide 27

Slide 27

min | max(2+ values) uses smallest value uses largest value

Slide 28

Slide 28

min( 100vw - 3rem, 80ch ) selected when viewport < 80ch selected when viewport > 80ch

Slide 29

Slide 29

.container { } width: min( 100vw - 3rem, 80ch ) margin-inline: auto;

Slide 30

Slide 30

.container { width: min( 100vw - 3rem, var(- -container-max, 80ch) margin-inline: auto; )

Slide 31

Slide 31

Slide 32

Slide 32

Slide 33

Slide 33

allows intrinsic sizing Global 94% Chromium v22 Firefox v3 fi

  • width property + pre xes Safari v6.1

Slide 34

Slide 34

fi sizing keywords available inline space fit-content min-content max-content t-content mincontent max-content

Slide 35

Slide 35

t-content t-content will grow but not over ow mincontent fl max-content has over ow potential max-content fl fi fi available inline space

Slide 36

Slide 36

width: fit-content contentrelative width without changing the display value! fl Macaroon tiramisu souf é tart chocolate cake

Slide 37

Slide 37

grid units & functions slinky potential everywhere Global 93% Chromium v57 Firefox v52 Safari v10.1

Slide 38

Slide 38

CSS grid is the perfect toolset for achieving flexibility within constraints

Slide 39

Slide 39

grid-template-columns: repeat( auto-fit, minmax(30ch, 1fr ) ) fi fi fi de ne a recurring pattern create as many tracks that t de nes a range minimum value maximum value

Slide 40

Slide 40

grid-template-columns: repeat( auto-fit, minmax(30ch, 1fr ) )

Slide 41

Slide 41

repeat( auto-fit, minmax( min( 100%, var(- -grid-min, 30ch) ), 1fr ) )

Slide 42

Slide 42

grid-template-columns: fit-content( 20ch ) minmax(50%, 1fr)

Slide 43

Slide 43

grid-template-columns: fit-content( var(- -sidebar-max, 20ch) ) minmax(50%, 1fr)

Slide 44

Slide 44

but what about spacing?

Slide 45

Slide 45

24px 32px 32px

Slide 46

Slide 46

gap, padding, & margin

Slide 47

Slide 47

gap, padding, & margin have different purposes

Slide 48

Slide 48

gap, padding, & margin use appropriate units & adaptive methods

Slide 49

Slide 49

goal: make spacing contextual so it flexes beyond the happy path

Slide 50

Slide 50

padding - box spacing clamp() with % & rem relative to the inline size

Slide 51

Slide 51

24px 32px 32px clamp()

Slide 52

Slide 52

24px 32px 32px 🙅 🎉 clamp() min()

Slide 53

Slide 53

main, .card { padding: clamp(1.5rem, 6%, 3rem) } main { width: min(100%, 60ch) }

Slide 54

Slide 54

main, .card { padding: clamp(1.5rem, 6%, 3rem) } main { width: min(100%, 60ch) }

Slide 55

Slide 55

main, .card { padding: clamp(1.5rem, 6%, 3rem) } main { width: min(100%, 60ch) } behavior up to 400% zoom

Slide 56

Slide 56

behavior comparison at 400% zoom clamp() px

Slide 57

Slide 57

:root { - -padding-sm: clamp(1rem, } 3%, 1.5rem);

  • -padding-md: clamp(1.5rem, 6%, 3rem);
  • -padding-lg: clamp(3rem, 12%, 6rem); , y ya gn i s de ns! e k to

Slide 58

Slide 58

margin - block layout spacing min() with viewport units, rem create contextual spacing

Slide 59

Slide 59

margin-block-start: min(4rem, 8vh); } static fl fl .block- ow { ex

Slide 60

Slide 60

.block- ow { margin-block-start: min(4rem, 8vh); fl }

Slide 61

Slide 61

.block- ow { margin-block-start: min(4rem, 8vh); } fl 8vh at 400% zoom

Slide 62

Slide 62

:root { - -block- ow-sm: min(2rem, 4vh); - -block- ow-md: min(4rem, 8vh); - -block- ow-lg: fl fl fl } min(8rem, 16vh); , y ya gn i s de ns! e k to

Slide 63

Slide 63

:is(body, .block- ow) > * + * { margin-block-start: var(- -block- ow, var(- -block- ow-md) ); fl fl fl }

Slide 64

Slide 64

gap - layout component spacing clamp() with vmax & rem

Slide 65

Slide 65

gap ! x o b x e l f & d i r g n i e l b a l i a v a

Slide 66

Slide 66

gap: clamp(1.5rem, 6vmax, 3rem); , y ya gn i s de ns! e k to

Slide 67

Slide 67

gap: clamp(1.5rem, 6vmax, 3rem); = gap: 48px = gap: 40px

Slide 68

Slide 68

gap: clamp(1.5rem, 6vmax, 3rem); zoom 400% = gap: 24px

Slide 69

Slide 69

:root { - -layout-gap-sm: clamp(1rem, 3vmax, 1.5rem) }

  • -layout-gap-md: clamp(1.5rem, 6vmax, 3rem);
  • -layout-gap-lg: 4rem); clamp(3rem, 8vmax,

Slide 70

Slide 70

Slide 71

Slide 71

  • { box-sizing: border-box; margin: 0; } body { font-family: system-ui; background-color: #28173c; color: #cc92ff; } body > * + * { margin-block-start: min(4rem, 8vh); } header { color: #28173c; background-color: #cc92ff; text-align: center; display: grid; gap: max(1rem, 2vmax); padding-block: clamp(3rem, 12%, 6rem); } main { width: min(100% - 8vmin, 120ch); margin-inline: auto; } ul { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 30ch), 1fr)); gap: max(2rem, 4vmax); } ul li > * + * { margin-block-start: 1em; }

Slide 72

Slide 72

Steph, why are you so 🤔 anti-media queries?

Slide 73

Slide 73

I’m pro-selective use of media queries Viewport-relative media queries aren’t scalable and are no longer the best tool for the job

Slide 74

Slide 74

things I couldn’t fit in this talk… container queries container units :has()

Slide 75

Slide 75

☙ Why haven’t we already embraced Intrinsic Web Design? ❧

Slide 76

Slide 76

hard to relinquish control feel trapped by processes not empowered to create or request change

Slide 77

Slide 77

spicy take incoming… necessary the rigidity of design systems and frameworks is at odds with intrinsic web design

Slide 78

Slide 78

intrinsic web design cheatsheet clamp() min() / max() fit-content / () grid auto-fit grid minmax start using contextual spacing gap, margin, and padding

Slide 79

Slide 79

ModernCSS.dev/spacing 5t3ph.dev/workshop

Slide 80

Slide 80

The web’s greatest strength, I believe, is often seen as a limitation, as a defect. It is the nature of the web to be flexible, and it should be our role as designers and developers to embrace this flexibility, and produce pages which, by being flexible, are accessible to all. John Allsop A Dao of Web Design, 2000

Slide 81

Slide 81

Scaling CSS Layout Beyond Pixels Stephanie Eckles @5t3ph • ModernCSS.dev