Core Concepts

Utility-First Fundamentals

Building complex components from a constrained set of primitive utilities.


Overview

Traditionally, whenever you need to style something on the web, you write CSS.

Utility-First Fundamentals - 图1

Using a traditional approach where custom designs require custom CSS

Utility-First Fundamentals - 图2

  1. <div class="chat-notification">
  2. <div class="chat-notification-logo-wrapper">
  3. <img class="chat-notification-logo" src="/img/logo.svg" alt="ChitChat Logo">
  4. </div>
  5. <div class="chat-notification-content">
  6. <h4 class="chat-notification-title">ChitChat</h4>
  7. <p class="chat-notification-message">You have a new message!</p>
  8. </div>
  9. </div>
  10. <style>
  11. .chat-notification {
  12. display: flex;
  13. max-width: 24rem;
  14. margin: 0 auto;
  15. padding: 1.5rem;
  16. border-radius: 0.5rem;
  17. background-color: #fff;
  18. box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  19. }
  20. .chat-notification-logo-wrapper {
  21. flex-shrink: 0;
  22. }
  23. .chat-notification-logo {
  24. height: 3rem;
  25. width: 3rem;
  26. }
  27. .chat-notification-content {
  28. margin-left: 1.5rem;
  29. padding-top: 0.25rem;
  30. }
  31. .chat-notification-title {
  32. color: #1a202c;
  33. font-size: 1.25rem;
  34. line-height: 1.25;
  35. }
  36. .chat-notification-message {
  37. color: #718096;
  38. font-size: 1rem;
  39. line-height: 1.5;
  40. }
  41. </style>

With Tailwind, you style elements by applying pre-existing classes directly in your HTML.

Utility-First Fundamentals - 图3

Using utility classes to build custom designs without writing CSS

Utility-First Fundamentals - 图4

  1. <div class="p-6 max-w-sm mx-auto bg-white rounded-xl shadow-lg flex items-center space-x-4">
  2. <div class="shrink-0">
  3. <img class="h-12 w-12" src="/img/logo.svg" alt="ChitChat Logo">
  4. </div>
  5. <div>
  6. <div class="text-xl font-medium text-black">ChitChat</div>
  7. <p class="text-gray-500">You have a new message!</p>
  8. </div>
  9. </div>

In the example above, we’ve used:

  • Tailwind’s flexbox and padding utilities (flex, shrink-0, and p-6) to control the overall card layout
  • The max-width and margin utilities (max-w-sm and mx-auto) to constrain the card width and center it horizontally
  • The background color, border radius, and box-shadow utilities (bg-white, rounded-xl, and shadow-lg) to style the card’s appearance
  • The width and height utilities (w-12 and h-12) to size the logo image
  • The space-between utilities (space-x-4) to handle the spacing between the logo and the text
  • The font size, text color, and font-weight utilities (text-xl, text-black, font-medium, etc.) to style the card text

This approach allows us to implement a completely custom component design without writing a single line of custom CSS.

Now I know what you’re thinking, “this is an atrocity, what a horrible mess!” and you’re right, it’s kind of ugly. In fact it’s just about impossible to think this is a good idea the first time you see it — you have to actually try it.

But once you’ve actually built something this way, you’ll quickly notice some really important benefits:

  • You aren’t wasting energy inventing class names. No more adding silly class names like sidebar-inner-wrapper just to be able to style something, and no more agonizing over the perfect abstract name for something that’s really just a flex container.
  • Your CSS stops growing. Using a traditional approach, your CSS files get bigger every time you add a new feature. With utilities, everything is reusable so you rarely need to write new CSS.
  • Making changes feels safer. CSS is global and you never know what you’re breaking when you make a change. Classes in your HTML are local, so you can change them without worrying about something else breaking.

When you realize how productive you can be working exclusively in HTML with predefined utility classes, working any other way will feel like torture.


Why not just use inline styles?

A common reaction to this approach is wondering, “isn’t this just inline styles?” and in some ways it is — you’re applying styles directly to elements instead of assigning them a class name and then styling that class.

But using utility classes has a few important advantages over inline styles:

  • Designing with constraints. Using inline styles, every value is a magic number. With utilities, you’re choosing styles from a predefined design system, which makes it much easier to build visually consistent UIs.
  • Responsive design. You can’t use media queries in inline styles, but you can use Tailwind’s responsive utilities to build fully responsive interfaces easily.
  • Hover, focus, and other states. Inline styles can’t target states like hover or focus, but Tailwind’s state variants make it easy to style those states with utility classes.

This component is fully responsive and includes a button with hover and focus styles, and is built entirely with utility classes:

Utility-First Fundamentals - 图5

  1. <div class="py-8 px-8 max-w-sm mx-auto bg-white rounded-xl shadow-lg space-y-2 sm:py-4 sm:flex sm:items-center sm:space-y-0 sm:space-x-6">
  2. <img class="block mx-auto h-24 rounded-full sm:mx-0 sm:shrink-0" src="/img/erin-lindford.jpg" alt="Woman's Face">
  3. <div class="text-center space-y-2 sm:text-left">
  4. <div class="space-y-0.5">
  5. <p class="text-lg text-black font-semibold">
  6. Erin Lindford
  7. </p>
  8. <p class="text-gray-500 font-medium">
  9. Product Engineer
  10. </p>
  11. </div>
  12. <button class="px-4 py-1 text-sm text-purple-600 font-semibold rounded-full border border-purple-200 hover:text-white hover:bg-purple-600 hover:border-transparent focus:outline-none focus:ring-2 focus:ring-purple-600 focus:ring-offset-2">Message</button>
  13. </div>
  14. </div>

Maintainability concerns

The biggest maintainability concern when using a utility-first approach is managing commonly repeated utility combinations.

This is easily solved by extracting components, usually as template partials or components.

  1. <!-- PrimaryButton.vue -->
  2. <template>
  3. <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
  4. <slot/>
  5. </button>
  6. </template>

You can also use Tailwind’s @apply feature to create CSS abstractions around common utility patterns.

Utility-First Fundamentals - 图6

  1. <!-- Using utilities -->
  2. <button class="py-2 px-4 font-semibold rounded-lg shadow-md text-white bg-sky-500 hover:bg-sky-700">
  3. Save changes
  4. </button>
  5. <!-- Extracting classes using @apply -->
  6. <button class="btn btn-sky">
  7. Button
  8. </button>
  9. <style>
  10. .btn {
  11. @apply py-2 px-4 font-semibold rounded-lg shadow-lg;
  12. }
  13. .btn-sky {
  14. @apply text-white bg-sky-500 hover:bg-sky-700;
  15. }
  16. </style>

Aside from that, maintaining a utility-first CSS project turns out to be a lot easier than maintaining a large CSS codebase, simply because HTML is so much easier to maintain than CSS. Large companies like GitHub, Netflix, Heroku, Kickstarter, Twitch, Segment, and more are using this approach with great success.

If you’d like to hear about others’ experiences with this approach, check out the following resources:

For even more, check out The Case for Atomic/Utility-First CSS, curated by John Polacek.