TExcelTAD
Home › Tailwind CSS › Custom Utilities & @apply

Custom Utilities & @apply

Sometimes the built-in utilities aren't enough, or you need plain CSS for a third-party widget. Tailwind v4 gives you a set of CSS directives for exactly these cases.

Directive Purpose
@utility define a new utility class that works with all variants
@apply copy existing utilities into your own CSS rule
@layer base / @layer components add base styles or component classes in the right cascade layer
@custom-variant define a new variant like theme-midnight:
@variant use a variant inside your own CSS
@source tell Tailwind to scan extra files for class names

@utility: your own utility class

css
@utility content-auto {
  content-visibility: auto;
}

Because it's registered as a utility, hover:content-auto, md:content-auto and every other variant work automatically. In v3 you would have written this inside @layer utilities { … }; in v4 that no longer creates real utilities — use @utility.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
  <style type="text/tailwindcss">
    @utility text-outline {
      color: transparent;
      -webkit-text-stroke: 2px currentColor;
    }
    @utility scrollbar-none {
      scrollbar-width: none;
      &::-webkit-scrollbar { display: none; }
    }
  </style>
</head>
<body class="p-6">
  <h1 class="text-6xl font-black text-outline text-sky-600 hover:text-rose-500">OUTLINE</h1>
  <div class="scrollbar-none mt-4 flex gap-2 overflow-x-auto">
    <span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">no</span><span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">visible</span>
    <span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">scrollbar</span><span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">but</span>
    <span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">still</span><span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">scrolls</span>
    <span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">sideways</span><span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">→ → →</span>
    <span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">keep</span><span class="shrink-0 rounded-full bg-gray-200 px-3 py-1">going</span>
  </div>
</body>
</html>

Hover the heading: hover:text-rose-500 changes the outline colour, because text-outline uses currentColor.

Functional utilities with values

Add -* to the name and read the value with --value():

css
@utility tab-* {
  tab-size: --value(integer);
}

Now tab-2, tab-4 and tab-8 all work. --value() can also accept theme keys (--value(--tab-size-*)) or arbitrary values (--value([integer])).

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
  <style type="text/tailwindcss">
    @utility stripes-* {
      background-image: repeating-linear-gradient(45deg, currentColor 0 4px, transparent 4px calc(--value(integer) * 1px));
    }
  </style>
</head>
<body class="space-y-3 p-6">
  <div class="stripes-8 h-10 rounded text-amber-400"></div>
  <div class="stripes-16 h-10 rounded text-sky-400"></div>
  <div class="stripes-32 h-10 rounded text-rose-400"></div>
</body>
</html>

@apply: reuse utilities in CSS

@apply inlines the declarations of existing utilities into a rule. It's useful for styling HTML you don't control (CMS output, third-party widgets) or for a small set of truly global component classes.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
  <style type="text/tailwindcss">
    @layer components {
      .btn {
        @apply inline-flex items-center gap-2 rounded-lg px-4 py-2 font-semibold transition;
      }
      .btn-primary {
        @apply bg-violet-600 text-white hover:bg-violet-700;
      }
      .btn-ghost {
        @apply text-violet-700 hover:bg-violet-100;
      }
    }
    @layer base {
      h2 { @apply text-xl font-bold text-gray-900; }
    }
  </style>
</head>
<body class="space-y-3 p-6">
  <h2>Styled by @layer base</h2>
  <button class="btn btn-primary">Primary</button>
  <button class="btn btn-ghost">Ghost</button>
  <button class="btn btn-primary rounded-full">Override with a utility</button>
</body>
</html>

The last button shows why @layer components matters: utilities live in a later layer, so rounded-full still beats the rounded-lg from .btn.

Tip: Don't @apply everything. If you find yourself turning every element into a class, you lose the benefits of utilities. Prefer template components (see the next lessons) and keep @apply for small, shared primitives.

@custom-variant and @variant

Create your own variants with a selector, and use any variant inside custom CSS with @variant:

css
@custom-variant theme-midnight (&:where([data-theme=midnight] *));

.callout {
  background: white;
  @variant dark {
    background: black;
  }
}

@source: scanning extra files

Tailwind finds classes automatically in your project. If classes live somewhere it ignores — for example a UI library inside node_modules — add the path:

css
@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib";

Test yourself

In v4, how should you add a custom utility that works with hover: and md:?

  1. @layer utilities { .x { } }
  2. @utility x { }
  3. @apply x;
  4. @theme { --utility-x }

Why put @apply-based component classes in @layer components?

  1. It is required for @apply to work
  2. So utility classes added in HTML can still override them
  3. So they load faster
  4. So they become variants

What does --value(integer) do inside @utility tab-*?

  1. Reads the number after tab- from the class name
  2. Generates a random integer
  3. Reads a CSS variable named integer
  4. Rounds the result