By this point, it’s not a secret to most people that I like Tailwind.
But, unknown to many people (who often jump to conclusions when you mention Tailwind), I don’t like vanilla Tailwind. In fact, I find most of it horrible and I shall refrain from saying further unkind words about it.
But I recognize and see that Tailwind’s methodology has merits — lots of them, in fact — and they go a long way to making your styles more maintainable and performant.
Today, I want to explore one of these merit-producing features that has been severely undersold — Tailwind’s @apply
feature.
What @apply
does
Tailwind’s @apply
features lets you “apply” (or simply put, copy-and-paste) a Tailwind utility into your CSS.
Most of the time, people showcase Tailwind’s @apply
feature with one of Tailwind’s single-property utilities (which changes a single CSS declaration). When showcased this way, @apply
doesn’t sound promising at all. It sounds downright stupid. So obviously, nobody wants to use it.
/* Input */
.selector {
@apply p-4;
}
/* Output */
.selector {
padding: 1rem;
}
To make it worse, Adam Wathan recommends against using @apply
, so the uptake couldn’t be worse.
Confession: The `apply` feature in Tailwind basically only exists to trick people who are put off by long lists of classes into trying the framework.
You should almost never use it 😬
Reuse your utility-littered HTML instead.https://t.co/x6y4ksDwrt
— Adam Wathan (@adamwathan) February 9, 2020
Personally, I think Tailwind’s @apply
feature is better than described.
Tailwind’s @apply
is like Sass’s @includes
If you have been around during the time where Sass is the dominant CSS processing tool, you’ve probably heard of Sass mixins. They are blocks of code that you can make — in advance — to copy-paste into the rest of your code.
- To create a mixin, you use
@mixin
- To use a mixin, you use
@includes
// Defining the mixin
@mixin some-mixin() {
color: red;
background: blue;
}
// Using the mixin
.selector {
@include some-mixin();
}
/* Output */
.selector {
color: red;
background: blue;
}
Tailwind’s @apply
feature works the same way. You can define Tailwind utilities in advance and use them later in your code.
/* Defining the utility */
@utility some-utility {
color: red;
background: blue;
}
/* Applying the utility */
.selector {
@apply some-utility;
}
/* Output */
.selector {
color: red;
background: blue;
}
Tailwind utilities are much better than Sass mixins
Tailwind’s utilities can be used directly in the HTML, so you don’t have to write a CSS rule for it to work.
@utility some-utility {
color: red;
background: blue;
}
<div class="some-utility">...</div>
On the contrary, for Sass mixins, you need to create an extra selector to house your @includes
before using them in the HTML. That’s one extra step. Many of these extra steps add up to a lot.
@mixin some-mixin() {
color: red;
background: blue;
}
.selector {
@include some-mixin();
}
/* Output */
.selector {
color: red;
background: blue;
}
<div class="selector">...</div>
Tailwind’s utilities can also be used with their responsive variants. This unlocks media queries straight in the HTML and can be a superpower for creating responsive layouts.
<div class="utility1 md:utility2">…</div>
A simple and practical example
One of my favorite — and most easily understood — examples of all time is a combination of two utilities that I’ve built for Splendid Layouts (a part of Splendid Labz):
vertical
: makes a vertical layouthorizontal
: makes a horizontal layout
Defining these two utilities is easy.
- For
vertical
, we can use flexbox withflex-direction
set tocolumn
. - For
horizontal
, we use flexbox withflex-direction
set torow
.
@utility horizontal {
display: flex;
flex-direction: row;
gap: 1rem;
}
@utility vertical {
display: flex;
flex-direction: column;
gap: 1rem;
}
After defining these utilities, we can use them directly inside the HTML. So, if we want to create a vertical layout on mobile and a horizontal one on tablet or desktop, we can use the following classes:
<div class="vertical sm:horizontal">...</div>
For those who are new to Tailwind, sm:
here is a breakpoint variant that tells Tailwind to activate a class when it goes beyond a certain breakpoint. By default, sm
is set to 640px
, so the above HTML produces a vertical layout on mobile, then switches to a horizontal layout at 640px
.
If you prefer traditional CSS over composing classes like the example above, you can treat @apply
like Sass @includes
and use them directly in your CSS.
<div class="your-layout">...</div>
.your-layout {
@apply vertical;
@media (width >= 640px) {
@apply horizontal;
}
}
The beautiful part about both of these approaches is you can immediately see what’s happening with your layout — in plain English — without parsing code through a CSS lens. This means faster recognition and more maintainable code in the long run.
Tailwind’s utilities are a little less powerful compared to Sass mixins
Sass mixins are more powerful than Tailwind utilities because:
- They let you use multiple variables.
- They let you use other Sass features like
@if
and@for
loops.
@mixin avatar($size, $circle: false) {
width: $size;
height: $size;
@if $circle {
border-radius: math.div($size, 2);
}
}
On the other hand, Tailwind utilities don’t have these powers. At the very maximum, Tailwind can let you take in one variable through their functional utilities.
/* Tailwind Functional Utility */
@utility tab-* {
tab-size: --value(--tab-size-*);
}
Fortunately, we’re not affected by this “lack of power” much because we can take advantage of all modern CSS improvements — including CSS variables. This gives you a ton of room to create very useful utilities.
Let’s go through another example
A second example I often like to showcase is the grid-simple
utility that lets you create grids with CSS Grid easily.
We can declare a simple example here:
@utility grid-simple {
display: grid;
grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
gap: var(--gap, 1rem);
}
By doing this, we have effectively created a reusable CSS grid (and we no longer have to manually declare minmax
everywhere).
After we have defined this utility, we can use Tailwind’s arbitrary properties to adjust the number of columns on the fly.
<div class="grid-simple [--cols:3]">
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
</div>
To make the grid responsive, we can add Tailwind’s responsive variants with arbitrary properties so we only set --cols:3
on a larger breakpoint.
<div class="grid-simple sm:[--cols:3]">
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
</div>
This makes your layouts very declarative. You can immediately tell what’s going on when you read the HTML.
Now, on the other hand, if you’re uncomfortable with too much Tailwind magic, you can always use @apply
to copy-paste the utility into your CSS. This way, you don’t have to bother writing repeat
and minmax
declarations every time you need a grid that grid-simple
can create.
.your-layout {
@apply grid-simple;
@media (width >= 640px) {
--cols: 3;
}
}
<div class="your-layout"> ... </div>
By the way, using @apply
this way is surprisingly useful for creating complex layouts! But that seems out of scope for this article so I’ll be happy to show you an example another day.
Wrapping up
Tailwind’s utilities are very powerful by themselves, but they’re even more powerful if you allow yourself to use @apply
(and allow yourself to detach from traditional Tailwind advice). By doing this, you gain access to Tailwind as a tool instead of it being a dogmatic approach.
To make Tailwind’s utilities even more powerful, you might want to consider building utilities that can help you create layouts and nice visual effects quickly and easily.
I’ve built a handful of these utilities for Splendid Labz and I’m happy to share them with you if you’re interested! Just check out Splendid Layouts to see a subset of the utilities I’ve prepared.
By the way, the utilities I showed you above are watered-down versions of the actual ones I’m using in Splendid Labz.
One more note: When writing this, Splendid Layouts work with Tailwind 3, not Tailwind 4. I’m working on a release soon, so sign up for updates if you’re interested!