Utility First CSS Isn’t Inline Styles

A presentation at CSS Day in June 2024 in Amsterdam, Netherlands by Sarah Dayan

Slide 1

Slide 1

Utility First CSS Isn’t Inline Styles Sarah Dayan | Principal Engineer at Algolia | CSS Day 2024

Slide 2

Slide 2

CSS Hi, I’m Sarah! 👋 Principal Engineer at Algolia JavaScript & CSS Lover Massive Cheese Enthusiast

Slide 3

Slide 3

“ Is CSS even a programming language?

Slide 4

Slide 4

“ Are you sure you don’t want to be a manager?

Slide 5

Slide 5

Slide 6

Slide 6

<ul class=”divide-y divide-gray-100”> <li class=”flex gap-x-4 py-5”> <img class=”size-12 flex-none rounded” src=”avatar.jpg” /> <div class=”min-w-0”> <p class=”text-sm font-semibold leading-6 text-gray-900”> Sarah Dayan </p> <p class=”mt-1 truncate text-xs leading-5 text-gray-500”> Principal Engineer at Algolia </p> </div> </li> </ul>

Slide 7

Slide 7

<ul class=”divide-y divide-gray-100”> <li class=”flex gap-x-4 py-5”> <img class=”size-12 flex-none rounded” src=”avatar.jpg” /> <div class=”min-w-0”> <p class=”text-sm font-semibold leading-6 text-gray-900”> Sarah Dayan </p> <p class=”mt-1 truncate text-xs leading-5 text-gray-500”> Principal Engineer at Algolia </p> </div> </li> Atomic (or utility-first) CSS </ul> Visual APIs

Slide 8

Slide 8

<ul class=”list”> <li class=”list-item”> <img class=”list-avatar” src=”avatar.jpg” /> <div class=”list-content”> <p class=”list-heading”> Sarah Dayan </p> <p class=”list-subheading”> Principal Engineer at Algolia </p> </div> </li> </ul>

Slide 9

Slide 9

<ul class=”list”> <li class=”list-item”> <img class=”list-avatar” src=”avatar.jpg” /> <div class=”list-content”> <p class=”list-heading”> Sarah Dayan </p> <p class=”list-subheading”> Principal Engineer at Algolia </p> </div> </li> </ul> Semantic CSS Identity-based contract

Slide 10

Slide 10

Slide 11

Slide 11

Atomic CSS Semantic CSS

Slide 12

Slide 12

Atomic CSS Semantic CSS

Slide 13

Slide 13

“ It violates separation of concerns!

Slide 14

Slide 14

“ It bloats the HTML!

Slide 15

Slide 15

“ It’s ugly and hard to read!

Slide 16

Slide 16

youtu.be/R50q4NES6Iw

Slide 17

Slide 17

“ It’s just the same as inline styles!

Slide 18

Slide 18

“ It’s just the same as inline styles!

Slide 19

Slide 19

Inline styles only have a local impact.

Slide 20

Slide 20

Well, almost 😅

Slide 21

Slide 21

<article style=”color: darkblue”> <header> <h2>Blade Runner 2049</h2> </header> <section> <p> Officer K (Ryan Gosling), a new blade runner for the Los Angeles Police Department, unearths a long-buried secret that has the potential to plunge what’s left of society into chaos. </p> </section> </article>

Slide 22

Slide 22

No pseudo-classes No pseudo-elements No combinators No media queries No animations

Slide 23

Slide 23

Atomic classes live in style sheets.

Slide 24

Slide 24

.on-hover:text-red:hover { color: red; }

Slide 25

Slide 25

<article> <header> <h2>Blade Runner 2049</h2> </header> <section> <p> Officer K (<a class=”on-hover:text-red” href=”#”>Ryan Gosling</a>), a new blade runner for the Los Angeles Police Department, unearths a long-secret that has the potential to plunge what’s left of society into chaos. </p> </section> </article>

Slide 26

Slide 26

.group:hover .group-hover:text-red { color: red; }

Slide 27

Slide 27

<article class=”group”> <header class=”group-hover:text-red”> <h2>Blade Runner 2049</h2> </header> <section> <p> Officer K (Ryan Gosling), a new blade runner for the Los Angeles Police Department, unearths a long-secret that has the potential to plunge what’s left of society into chaos. </p> </section> </article>

Slide 28

Slide 28

Slide 29

Slide 29

Wait, it gets quirkier.

Slide 30

Slide 30

.peer:in-range ~ .peer-invalid { visibility: hidden; } .peer:out-of-range ~ .peer-invalid { visibility: visible; }

Slide 31

Slide 31

<form> <label> <span>Date</span> <input type=”date” class=”peer” min=”2024-01-01” max=”2024-12-31” /> <p class=”peer-invalid”> Please provide a valid date. </p> </label> </form>

Slide 32

Slide 32

Slide 33

Slide 33

Slide 34

Slide 34

@media (min-width: 768px) { .col-md-1 { flex: 0 0 auto; width: 8.33333333%; } .col-md-2 { flex: 0 0 auto; width: 16.66666667%; } .col-md-3 { flex: 0 0 auto; width: 25%; }

Slide 35

Slide 35

@media (hover: none) and (pointer: coarse) { .on-touch:block { display: block; } }

Slide 36

Slide 36

@media (orientation: landscape) { .on-landscape:hidden { display: none; } }

Slide 37

Slide 37

@media (prefers-color-scheme: dark) { .dark:bg-darkblue { color: darkblue; } }

Slide 38

Slide 38

Atomic classes have context.

Slide 39

Slide 39

Slide 40

Slide 40

Inline styles are unbounded.

Slide 41

Slide 41

Slide 42

Slide 42

Slide 43

Slide 43

Inline styles don’t have a strategy.

Slide 44

Slide 44

Atomic classes are a system.

Slide 45

Slide 45

Inline styles convey no intent.

Slide 46

Slide 46

.col fl fl fl { oat: left } .col { oat: left } .col { oat: left }

Slide 47

Slide 47

.col { oat: left } .col { oat: left } fi fl fl fl .clear x .col { oat: left }

Slide 48

Slide 48

.clearfix:after { content: ”; display: table; clear: both; }

Slide 49

Slide 49

<header> <h2 style=” overflow: hidden; text-overflow: ellipsis; white-space: nowrap ” > Blade Runner 2049 </h2> </header>

Slide 50

Slide 50

<header> <h2 class=”truncate”> Blade Runner 2049 </h2> </header>

Slide 51

Slide 51

<header> <h2 style=” overflow: hidden; text-overflow: ellipsis; white-space: nowrap ” > Blade Runner 2049 </h2> </header> <header> <h2 class=”truncate”> Blade Runner 2049 </h2> </header>

Slide 52

Slide 52

All abstractions convey intent.

Slide 53

Slide 53

Atomic CSS abstracts visuals, not identity.

Slide 54

Slide 54

Atomic CSS abstracts visuals, not identity.

Slide 55

Slide 55

Inline styles aren’t cacheable.

Slide 56

Slide 56

HTML File updates Time

Slide 57

Slide 57

Slide 58

Slide 58

Slide 59

Slide 59

Slide 60

Slide 60

Slide 61

Slide 61

Slide 62

Slide 62

Slide 63

Slide 63

You can cache inline styles. But that’s pointless.

Slide 64

Slide 64

CSS assets last longer.

Slide 65

Slide 65

HTML CSS File updates Time

Slide 66

Slide 66

Slide 67

Slide 67

Assets freshness speaks volumes on where to spend focus.

Slide 68

Slide 68

A healthy codebase grants you focus.

Slide 69

Slide 69

Slide 70

Slide 70

Slide 71

Slide 71

Slide 72

Slide 72

CSS compositions always have to be mentally compiled.

Slide 73

Slide 73

<div class=”flex mb-16 md:mr-0 mr-24”> <a class=”text-color-inherit hover:underline” href=”/doc/guides/security/api-keys/” > API keys </a> </div> <div class=”flex mb-16 md:mr-0 mr-24”> <a class=”text-color-inherit hover:underline” href=”/doc/guides/security/security-best-practices/” > Security best practices </a> </div>

Slide 74

Slide 74

Slide 75

Slide 75

Well-crafted systems liberate you.

Slide 76

Slide 76

Well-crafted systems liberate you.

Slide 77

Slide 77

That was intense.

Slide 78

Slide 78

Constructive criticism is necessary.

Slide 79

Slide 79

“ Your brain rewards you for classifying new information into existing buckets. Looking for similarities is lower energy than understanding new differences. Matthias Verraes | Software consultant, curator of DDD Europe

Slide 80

Slide 80

“ Proclaiming that the new idea is the same as the old idea gives you permission not to investigate it deeply, stick to the existing reality where you are an expert in the old idea, and the new ones are merely repackaging of your insight. Matthias Verraes | Software consultant, curator of DDD Europe

Slide 81

Slide 81

“ Proclaiming that the new idea is the same as the old idea gives you permission not to investigate it deeply, stick to the existing reality where you are an expert in the old idea, and the new ones are merely repackaging of your insight. Matthias Verraes | Software consultant, curator of DDD Europe

Slide 82

Slide 82

“ There’s such a firehose of information that keeping up with even a single topic is impossible. We should be forgiven for not knowing and understanding and keeping up with everything, even if our job is knowing and understanding and keeping up with everything. Matthias Verraes | Software consultant, curator of DDD Europe

Slide 83

Slide 83

“ But let’s try to make a habit of this. When you feel “oh I get it, it’s just like…”, then follow up with “so what makes the new thing different from the old thing?” Matthias Verraes | Software consultant, curator of DDD Europe

Slide 84

Slide 84

“ But let’s try to make a habit of this. When you feel “oh I get it, it’s just like…”, then follow up with “so what makes the new thing different from the old thing?” Matthias Verraes | Software consultant, curator of DDD Europe

Slide 85

Slide 85

Atomic CSS isn’t a one-size-fits-all.

Slide 86

Slide 86

And sometimes, problems don’t exist.

Slide 87

Slide 87

This sucks.

Slide 88

Slide 88

This is not for me.

Slide 89

Slide 89

This is not for me. But it might, eventually.

Slide 90

Slide 90

Think different critically.

Slide 91

Slide 91

CSS Thank you! sarahdayan.dev algolia.com