Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
@kpdcdg kpdcdg Fix typo 909d1c8 Jun 3, 2019
2 contributors

Users who have contributed to this file

@adamwathan @kpdcdg
201 lines (163 sloc) 11.1 KB
extends title description titleBorder
_layouts.documentation
Utility-First
Building complex components from a constrained set of primitive utilities.
true

Overview

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

@component('_partials.tip-bad') Using a traditional approach where custom designs require custom CSS @endcomponent

@component('_partials.code-sample', ['class' => 'bg-gray-200 px-8 py-12'])

ChitChat

You have a new message!

@slot('code')

ChitChat Logo

ChitChat

You have a new message!

<style> .chat-notification { display: flex; max-width: 24rem; margin: 0 auto; padding: 1.5rem; border-radius: 0.5rem; background-color: #fff; box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04); } .chat-notification-logo-wrapper { flex-shrink: 0; } .chat-notification-logo { height: 3rem; width: 3rem; } .chat-notification-content { margin-left: 1.5rem; padding-top: 0.25rem; } .chat-notification-title { color: #1a202c; font-size: 1.25rem; line-height: 1.25; } .chat-notification-message { color: #718096; font-size: 1rem; line-height: 1.5; } </style>

@endslot @endcomponent

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

@component('_partials.tip-good') Using utility classes to build custom designs without writing CSS @endcomponent

@component('_partials.code-sample', ['class' => 'bg-gray-200 px-8 py-12'])

ChitChat

You have a new message!

@slot('code')

ChitChat Logo

ChitChat

You have a new message!

@endslot @endcomponent

In the example above, we've used:

  • Tailwind's flexbox and padding utilities (flex, 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-lg, and shadow-xl) to style the card's appearance
  • The width and height utilities (w-12 and h-12) to size the logo image
  • The margin and padding utilities (ml-6 and pt-1) to position the card text
  • The font size, text color, and line-height utilities (text-xl, text-gray-900, leading-tight, etc.) to style the card text

...allowing 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.
  • Pseudo-classes. Inline styles can't target states like hover or focus, but Tailwind's pseudo-class variants make it easy to style those states with utility classes.

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

@component('_partials.code-sample', ['class' => 'p-8 bg-gray-200'])

Woman's Face

Erin Lindford

Customer Support Specialst

Message
@endcomponent

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, either as template partials/JavaScript components, or using Tailwind's @@apply feature to create abstractions around common utility patterns.

@component('_partials.code-sample', ['class' => 'text-center']) Button

@slot('code')

Button Button <style> .btn { @@apply font-bold py-2 px-4 rounded; } .btn-blue { @@apply bg-blue-500 text-white; } .btn-blue:hover { @@apply bg-blue-700; } </style>

@endslot @endcomponent

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

You can’t perform that action at this time.