Utility-first CSS and Tailwind
What utility-first CSS is, why teams pick it over named classes, how Tailwind v4 generates CSS from your files, how @theme works and where it hurts.
Utility-first CSS and Tailwind
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Utility-first CSS styles an element by stacking small single-purpose classes in the HTML (`px-4`, `rounded-lg`, `text-white`) instead of writing a named class like `.button` and its rules in a stylesheet.
- Tailwind scans your source files for class names and generates only the CSS those classes need, so the stylesheet stays proportional to what you use, not to the size of the framework.
- In Tailwind v4 you load it with `@import "tailwindcss"` and define design tokens in `@theme`, which become both CSS custom properties and matching utilities (`--color-brand` gives `bg-brand`, `text-brand`).
- Variants prefix a utility with a condition: `hover:`, `md:`, `dark:`, `focus-visible:`. Each compiles to an ordinary rule inside the matching selector or media query.
- The trade is real: you gain no naming, no dead CSS and a constrained design scale; you pay with long class lists, repeated markup that must be extracted into components, and a reliance on tooling.
The idea, and the objection
A traditional approach names a thing and then styles the name:
<button class="btn btn--primary">Save</button>
.btn { padding: 0.5rem 1rem; border-radius: 0.5rem; }
.btn--primary { background: royalblue; color: white; }
Utility-first flips it. The CSS is a large, fixed list of one-job classes, and you compose them in the markup:
<button class="rounded-lg bg-blue-600 px-4 py-2 text-white hover:bg-blue-700">Save</button>
The standard objection is that this looks like inline styles with extra steps. It is not, for three reasons. Utilities draw from a constrained scale (px-4 is always the same value, not “somewhere around 16px”), they support states and breakpoints (hover:, md:), which inline styles cannot, and they compile to real classes with low, even specificity. The honest answer is that it trades one set of problems for another, as the last section describes.
How Tailwind produces its CSS
Tailwind is a build step. It reads your source files (HTML, JSX, templates), finds anything that looks like a class name it knows, and writes only the CSS for those. The production file is small because unused utilities are never generated.
To check how this behaves, I ran Tailwind v4.3.5’s CLI over a file containing one button:
<button class="rounded-lg bg-brand px-4 py-2 text-white hover:bg-brand/80 md:px-6">Save</button>
with this stylesheet:
@import "tailwindcss";
@theme {
--color-brand: oklch(55% 0.2 260);
--font-display: "Fraunces", serif;
}
The output contained, among other rules, .bg-brand{background-color:var(--color-brand)}, .px-4{padding-inline:calc(var(--spacing) * 4)}, a @media (min-width:48rem) block holding .md\:px-6, and the hover rule inside @media (hover:hover). It was wrapped in cascade layers named theme, base, components and utilities. The --color-brand token from @theme appeared as a custom property.
That shows several things at once:
- Spacing is a multiplier.
px-4iscalc(var(--spacing) * 4)so one--spacingvariable scales every spacing utility. hover:is wrapped in@media (hover:hover)so touch devices do not get sticky hover states.- Cascade layers organise the output, which is the mechanism from cascade layers and scope. Utilities sit in the last layer, so they beat component styles without
!important. - Breakpoints are plain media queries,
mdbeing 48rem.
Because Tailwind finds classes by scanning text, the class name has to appear whole in your source. bg-${color}-500 assembled at runtime will not be found. Use full names in a lookup table.
Tailwind v4 setup
The important v4 changes:
- CSS-first configuration. There is no
tailwind.config.jsby default. You load Tailwind with@import "tailwindcss"and customise with@themein CSS, as above. Tokens become custom properties, which means they are available to your own CSS and to JavaScript. - Automatic content detection. It finds your template files without a list of paths.
- Integration via a Vite plugin (
@tailwindcss/vite), a PostCSS plugin, or a CLI. I used the CLI above. - Modern browser baseline. Tailwind’s documentation states v4 targets Safari 16.4, Chrome 111 and Firefox 128 or newer, because it relies on features like cascade layers and
@property. Older audiences need v3.
Many tutorials online still show v3 setup: a tailwind.config.js, @tailwind base; @tailwind components; @tailwind utilities; directives and content arrays. When you follow one, check the Tailwind version it uses.
Variants, arbitrary values and dark mode
- Variants prefix any utility:
hover:,focus-visible:,md:,dark:,group-hover:,has-[:checked]:. They stack:md:hover:bg-blue-700. - Arbitrary values escape the scale when needed:
w-[137px],grid-cols-[1fr_2fr],bg-[#ff6b00]. - Dark mode is the
dark:variant, driven byprefers-color-schemeby default, as in user preference queries, and can be switched to a class.
Where it hurts
- Long class lists make markup noisy. Editors and formatters that sort classes help.
- Repeated patterns need to be extracted into components in your framework, not into
@applyrules, or you rebuild the BEM layer you were avoiding. - Fighting the system. Utilities are good at the common case and awkward for complex selectors, animations and states. It is normal to write some plain CSS alongside, and the cascade layers make that safe.
- Tooling dependency. The CSS exists only after the build, and the HTML is hard to read without knowing the class names.
- Not a substitute for CSS knowledge.
flex,gridanditems-centerare the layout properties from earlier lessons with shorter names. If you do not understand Flexbox, Tailwind will not explain it.
A fair summary is that utility-first suits teams building many UIs with a shared design scale, especially in component frameworks, and suits less well a handful of hand-crafted pages or stylesheets that must be readable without a build.
What the video covers
This embeds the opening of Coder Coder’s “Tailwind CSS v4 for Beginners | Full Course 2026” (March 2026, 2 hours 16 minutes in full), from 0:00 to 10:43. Its chapter list starts with “What is Tailwind?” at 0:00 and “Installation & Setup” at 2:39, and the next chapter, “Text & Colors”, begins at 10:43, so the clip is the “what and setup” portion. The later chapters, which cover hover states, custom values, layouts, a custom theme, pseudo-elements, layers and components, are not in the clip but are in the full course on the same page. I could not read a transcript, so how the first ten minutes explain the idea is not described here. Because the course is from 2026 it teaches v4. Better Stack’s video on what changed in v4 is in further viewing.
Where this fits
Utility-first is one answer to the problem in CSS architecture and BEM, and relies on the build tooling from PostCSS and Lightning CSS. The next lesson covers the other answer, scoping styles through the build: CSS Modules and CSS-in-JS.
Check yourself
What problem does utility-first CSS remove that BEM and component classes have?
Naming and growth. With named classes, every new component needs a name and its own rules, and the stylesheet only grows because nobody can be sure a rule is unused. With utilities, the styles are written where they are used from a fixed set, so there is nothing to name, nothing to orphan when you delete the markup, and styles from different places cannot interfere through specificity.
How does Tailwind decide which CSS to put in the output?
It scans your source files for strings that match utility class names and generates rules only for those. Class names must therefore appear as complete strings in the source: `bg-red-500` works, but building `bg-${color}-500` from pieces will not be found.
In a Tailwind v4 stylesheet you write `@theme { --color-brand: oklch(55% 0.2 260); }`. What do you get?
A CSS custom property `--color-brand` on the root, and utilities such as `bg-brand`, `text-brand` and `border-brand` generated from it. In my test, `bg-brand` compiled to `background-color: var(--color-brand)`. The token is therefore both a variable you can use in plain CSS and a utility.
Your team has fifteen identical buttons, each with the same 12 utility classes. What do you do?
Extract a component in your framework (a React, Vue or template partial) so the class list lives in one place. Reaching for `@apply` in a stylesheet recreates the named-class approach and loses most of the benefits, though it is an option for third-party markup you cannot edit.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.