Utility-First Fundamentals

    Building complex components from a constrained set of primitive utilities.


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

    Using a traditional approach where custom designs require custom CSS

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

    Using utility classes to build custom designs without writing CSS

    In the example above, we’ve used:

    • Tailwind’s and padding utilities (, shrink-0, and p-6) to control the overall card layout
    • The and margin utilities (max-w-sm and mx-auto) to constrain the card width and center it horizontally
    • The and height utilities (w-12 and h-12) to size the logo image
    • The utilities () to handle the spacing between the logo and the text
    • The font size, , 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.

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


    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.

    • 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 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 - 图3


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

    This is easily solved by extracting components and partials, and using like multi-cursor editing and simple loops.

    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: