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

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

“ Is CSS even a programming language?

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

<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>

<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

<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>

<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

Atomic CSS Semantic CSS

Atomic CSS Semantic CSS

“ It violates separation of concerns!

“ It bloats the HTML!

“ It’s ugly and hard to read!

youtu.be/R50q4NES6Iw

“ It’s just the same as inline styles!

“ It’s just the same as inline styles!

Inline styles only have a local impact.

Well, almost 😅

<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>

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

Atomic classes live in style sheets.

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

<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>

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

<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>

Wait, it gets quirkier.

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

<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>

@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%; }

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

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

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

Atomic classes have context.

Inline styles are unbounded.

Inline styles don’t have a strategy.

Atomic classes are a system.

Inline styles convey no intent.

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

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

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

<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>

<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>

All abstractions convey intent.

Atomic CSS abstracts visuals, not identity.

Atomic CSS abstracts visuals, not identity.

Inline styles aren’t cacheable.

HTML File updates Time

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

CSS assets last longer.

HTML CSS File updates Time

Assets freshness speaks volumes on where to spend focus.

A healthy codebase grants you focus.

CSS compositions always have to be mentally compiled.

<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>

Well-crafted systems liberate you.

Well-crafted systems liberate you.

That was intense.

Constructive criticism is necessary.

“ 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

“ 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

“ 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

“ 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

“ 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

“ 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

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

And sometimes, problems don’t exist.

This sucks.

This is not for me.

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

Think different critically.

CSS Thank you! sarahdayan.dev algolia.com