Modern CSS Layout is Awesome! (2024 edition)

A presentation at Pixel Pioneers in June 2024 in Bristol, UK by Michelle Barker

Slide 1

Slide 1

Modern CSS Layout is Awesome!

Slide 2

Slide 2

Senior Front End Developer at

Slide 3

Slide 3

💖 The web’s most beautiful language

Slide 4

Slide 4

css-irl.info

Slide 5

Slide 5

Slide 6

Slide 6

🤕 fi Tables Floats Clear xes

Slide 7

Slide 7

🤕 fi Tables Floats Clear xes 🙂 Flexbox Grid Multi-column Viewport units Custom properties

Slide 8

Slide 8

“Material honesty” Jeremy Keith (Resilient Web Design)

Slide 9

Slide 9

🤕 fi Tables Floats Clear xes 🙂 Flexbox Grid Multi-column Viewport units 🤯 min(), max(), clamp() Logical properties and values Container queries Subgrid :has() Intrinsic sizing…

Slide 10

Slide 10

It’s time to re-learn CSS layout!

Slide 11

Slide 11

“Be the browser’s mentor, not its micromanager” Andy Bell

Slide 12

Slide 12

You might have missed…

Slide 13

Slide 13

Aspect-ratio

Slide 14

Slide 14

Before ☹ .aspect-box { position: relative; } .aspect-box::before { display: block; content: ”; width: 100%; padding-bottom: calc(100% / (var(—-aspect-ratio, 3 / 2))); } .aspect-box > :first-child { position: absolute; top: 0; right: 0; bottom: 0; left: 0; } After ☺ .aspect-box { aspect-ratio: 3 / 2; }

Slide 15

Slide 15

Slide 16

Slide 16

Slide 17

Slide 17

codepen.io/michellebarker/pen/bGLpzjd

Slide 18

Slide 18

codepen.io/michellebarker/pen/bGLpzjd

Slide 19

Slide 19

Flex gap

Slide 20

Slide 20

The problem

Slide 21

Slide 21

The problem

Slide 22

Slide 22

The problem

Slide 23

Slide 23

The solution ul { gap: 1em 0.5em; }

Slide 24

Slide 24

New and improved viewport units

Slide 25

Slide 25

The problem

Slide 26

Slide 26

The problem

Slide 27

Slide 27

The solution 100svh 100svh

Slide 28

Slide 28

The solution 100lvh 100lvh

Slide 29

Slide 29

The solution 100dvh 100dvh

Slide 30

Slide 30

Overlaying elements (Putting things on top of each other)

Slide 31

Slide 31

figcaption { position: absolute; top: 0; right: 0; bottom: 0; left: 0; }

Slide 32

Slide 32

figcaption { position: absolute; inset: 0; }

Slide 33

Slide 33

figcaption { position: absolute; inset: 50% 50% 0 0; }

Slide 34

Slide 34

figcaption { position: absolute; inset-block: 50px; }

Slide 35

Slide 35

figcaption { position: absolute; inset-inline: 50px; }

Slide 36

Slide 36

figcaption { position: absolute; inset: auto -2rem -2rem auto; }

Slide 37

Slide 37

figure { display: grid;

  • { grid area: 1 / 1; } }

Slide 38

Slide 38

fi Intrinsic sizing min-content max-content t-content

Slide 39

Slide 39

div { width: 300px; }

Slide 40

Slide 40

div { width: min content; }

Slide 41

Slide 41

div { width: min content; }

Slide 42

Slide 42

div { width: max content; }

Slide 43

Slide 43

i div { width: f t content; }

Slide 44

Slide 44

i div { width: f t content; }

Slide 45

Slide 45

Slide 46

Slide 46

Subgrid

Slide 47

Slide 47

Slide 48

Slide 48

.card { grid-row: span 3; display: grid; gap: 0; grid-template-rows: subgrid; }

Slide 49

Slide 49

.card { grid-row: span 3; display: grid; gap: 0; grid-template-rows: subgrid; }

Slide 50

Slide 50

Responsive layouts

Slide 51

Slide 51

Slide 52

Slide 52

Slide 53

Slide 53

i

.card grid { display: grid; gap: 1rem; grid template columns: repeat(auto f ll, minmax(18rem, 1fr)); }

Slide 54

Slide 54

i

.card grid { display: grid; gap: 1rem; grid template columns: repeat(auto f ll, minmax(min(18rem, 100%), 1fr)); }

Slide 55

Slide 55

Container queries

Slide 56

Slide 56

main, aside { container: layout / inline size; }

Slide 57

Slide 57

@container layout (inline size > 700px) { .card grid { grid template columns: repeat(2, 1fr); } }

Slide 58

Slide 58

@container layout (inline size > 1200px) { .card grid { grid template columns: repeat(3, 1fr); } }

Slide 59

Slide 59

@container layout (inline size > 1200px) { .card grid { grid template columns: repeat(3, 1fr); } }

Slide 60

Slide 60

li { container: card / inline size; }

Slide 61

Slide 61

@container card (inline size > 450px) { .card { display: grid; grid template columns: 1fr 1fr; } }

Slide 62

Slide 62

@container card (inline size > 450px) { .card { display: grid; grid template columns: 1fr 1fr; } }

Slide 63

Slide 63

@container card (inline size > 450px) { .card { display: grid; grid template columns: 1fr 1fr; } }

Slide 64

Slide 64

@container layout (inline size > 1200px) { .card grid:has(> :last child:nth child(3n - 2)) > :f rst child { grid column: span 3; }

i

}

Slide 65

Slide 65

.card content { padding: clamp(1rem, 5cqi, 3rem); font size: clamp(1rem, 2.5cqi, 1.6rem);

_ _ }

Slide 66

Slide 66

Container-relative units cqw query container width cqh query container height cqi query container inline size cqb query container block size cqmin smallest of ‘cqi’ or ‘cqb’ cqmax largest of ‘cqi’ or ‘cqb’

Slide 67

Slide 67

Browser support

Slide 68

Slide 68

container-query-poly ll fi fi github.com/GoogleChromeLabs/container-query-poly ll

Slide 69

Slide 69

codepen.io/michellebarker/pen/yLxKPYW

Slide 70

Slide 70

utopia.fyi

Slide 71

Slide 71

:has()

Slide 72

Slide 72

.grid blockquote { grid-column: 2 / span 4; grid-row: 2 / span 2; text-align: center; }

Slide 73

Slide 73

.grid:has(img) img { grid-row: 1 / span 2; grid-column: 1 / span 3; } .grid:has(img) blockquote { grid-row: 2 / span 2; grid-column: 3 / span 4; text-align: left; }

Slide 74

Slide 74

Anchor positioning

Slide 75

Slide 75

Slide 76

Slide 76

/ < . . . / < / <

<div class=”wrapper”> <button> button> <div class=”tooltip”> … div> div>

Slide 77

Slide 77

/ < . . . / < .wrapper { position: relative; } / <

<div class=”wrapper”> <button> button> <div class=”tooltip”> … div> div>

Slide 78

Slide 78

/ < . . . / < .tooltip { position: absolute; } / <

<div class=”wrapper”> <button> button> <div class=”tooltip”> … div> div>

Slide 79

Slide 79

Anchor Any other element

Slide 80

Slide 80

Anchor

button { anchor name: } anchor_1;

Slide 81

Slide 81

button { anchor name: } anchor_1;

.tooltip { position: absolute; position anchor: anchor_1; }

Slide 82

Slide 82

button { anchor name: } anchor_1;

.tooltip cite { position: absolute; position anchor: anchor_1; inset }

Slide 83

Slide 83

button { anchor name: } anchor_1;

.tooltip { position: absolute; top: anchor( anchor_1 bottom); right: anchor( anchor_1 right); }

Slide 84

Slide 84

button { anchor name: } anchor_1;

.tooltip { position: absolute; top: anchor( anchor_1 150%); right: anchor( anchor_1 -150%); }

Slide 85

Slide 85

Slide 86

Slide 86

Slide 87

Slide 87

@position try pos left { right: anchor(var( anchor name) right); left: auto; }

@position try pos right { left: anchor(var( anchor name) left); }

Slide 88

Slide 88

@position try pos left { right: anchor(var( anchor name) right); left: auto; }

@position try pos right { left: anchor(var( anchor name) left); }

Slide 89

Slide 89

@position try pos left { right: anchor(var( anchor name) right); left: auto; }

@position try pos right { left: anchor(var( anchor name) left); }

Slide 90

Slide 90

.tooltip { position try options: pos right, pos left; position try order: most width; }

Slide 91

Slide 91

.tooltip { position try options: pos right, pos left; position try order: most width; }

Slide 92

Slide 92

codepen.io/michellebarker/pen/jOomKRE

Slide 93

Slide 93

youtube.com/watch?v=XaO2mZnIOzs

Slide 94

Slide 94

codepen.io/michellebarker/pen/pomWryN

Slide 95

Slide 95

codepen.io/michellebarker/pen/pomWryN

Slide 96

Slide 96

codepen.io/michellebarker/pen/pomWryN

Slide 97

Slide 97

Masonry

Slide 98

Slide 98

Slide 99

Slide 99

Slide 100

Slide 100

Slide 101

Slide 101

shorturl.at/LpCUW

Slide 102

Slide 102

Let’s start building creative layouts again.

Slide 103

Slide 103

💖 Thank you css-irl.info