Custom utilities

@utility and @variant: extending Tailwind in CSS, plus typography for long text.

What it is

Tailwind v4 is configured in CSS, and extended there too. @utility adds a class that behaves like a built-in one: it works with every variant (hover:, md:, dark:) and only ships if it's used. Add -* to the name and --value() reads the value from the class: a number, a theme key or anything in brackets. @custom-variant adds a variant, and @variant applies one inside your own CSS. Plugins load with @plugin, like the typography plugin at the bottom.

When to use it

  • A pattern you repeat that no built-in utility covers
  • A value scale of your own: give it a theme namespace and a functional utility
  • A state you check often (hocus:) as one word
  • prose for Markdown, CMS and docs content
Official docs ↗

1. Plain and functional utilities

overflow-x-auto

  • @theme
  • @utility
  • @variant
  • @custom-variant
  • @plugin
  • @source
  • @apply
  • @config
  • --value()
  • --modifier()
  • @reference
  • @import

overflow-x-auto scrollbar-hidden fade-x-8

  • @theme
  • @utility
  • @variant
  • @custom-variant
  • @plugin
  • @source
  • @apply
  • @config
  • --value()
  • --modifier()
  • @reference
  • @import

scrollbar-hidden is a plain utility. fade-x-8 is functional: --value(integer) turns the 8 into calc(var(--spacing) * 8), so it follows the spacing scale like p-8. Scroll the rows sideways.

tab-2

function size() {
	if (wide) {
		return 2;
	}
}

tab-4

function size() {
	if (wide) {
		return 2;
	}
}

tab-github

function size() {
	if (wide) {
		return 2;
	}
}

tab-* accepts a number or a named size from @theme (--tab-size-github: 8).

2. Variants of your own

Links with link-grow: an underline that grows on hover or keyboard focus, like this one.

@custom-variant hocus (&:hover, &:focus-visible); defines it. The buttons use hocus:bg-accent; link-grow uses @variant hocus { … } inside its own definition.

3. Typography for long text

prose prose-site prose-base prose-code:before:content-none prose-code:after:content-none

Writing about CSS

The same HTML, styled by one class. Good for anything that arrives as Markdown or from a CMS.

Tailwind styles markup you write, one utility at a time. Long-form text is different: you don't control each <p>, so the plugin styles the elements for you, with sensible rhythm, links and lists.

What it covers

  • Headings, lists and quotes
  • Inline code and code blocks
  • Tables, keyboard keys like Ctrl + K, and rules

Use utilities for the layout, and prose for the words.

@plugin "@tailwindcss/typography";
ClassDoes
proseTurns it on
prose-lgScales the whole set
prose-a:…Targets one element type

Untick prose to see the bare HTML. It follows the theme switch with no dark:prose-invert, because prose-site points the plugin's colours at the site's tokens.

Show code · src/app/globals.css
@import "tailwindcss";
@import "@djmiskey/lab-tokens/tailwind.css";
@plugin "@tailwindcss/typography";

/* The shared look of every djmiskey.com lab (neutral tokens, light/dark,
   fluid type, element defaults) comes from @djmiskey/lab-tokens. Each lab
   sets only its own accent: this one is sky. */
:root {
  --lab-accent: #0369a1; /* white text on it 5.9:1, and ≥ 3:1 against the dark backgrounds */
  --lab-accent-ink: #075985; /* clears 4.5:1 on the surface and the code background */
  --lab-accent-ink-dark: #38bdf8;
}

/* Extending Tailwind in CSS (see /tailwind/utilities). */

/* A variant: hover for a mouse, focus-visible for a keyboard, in one word. */
@custom-variant hocus (&:hover, &:focus-visible);

/* A plain utility: hide the scrollbar but keep the scrolling. */
@utility scrollbar-hidden {
  scrollbar-width: none;
  &::-webkit-scrollbar {
    display: none;
  }
}

/* Functional utilities: the * is the value. fade-x-8 fades both edges over
   calc(var(--spacing) * 8), like any spacing utility; fade-x-[3rem] takes any length. */
@utility fade-x-* {
  --fade: calc(var(--spacing) * --value(integer));
  --fade: --value([length]);
  mask-image: linear-gradient(to right, transparent, #000 var(--fade), #000 calc(100% - var(--fade)), transparent);
}
/* tab-4 takes a number; tab-github a named size from @theme below. */
@utility tab-* {
  tab-size: --value(integer, --tab-size-*);
}

/* A utility that uses a variant inside: @variant applies hocus to its own CSS.
   The underline grows from the left, and simply appears with reduced motion. */
@utility link-grow {
  text-decoration: none;
  background: linear-gradient(currentColor 0 0) 0 100% / 0 2px no-repeat;
  transition: background-size 0.2s ease-out;
  @variant hocus {
    background-size: 100% 2px;
  }
  @variant motion-reduce {
    transition: none;
  }
}

/* Typography (prose), coloured by this site's tokens, so light and dark mode
   come free: no prose-invert needed. */
@utility prose-site {
  --tw-prose-body: var(--color-fg);
  --tw-prose-headings: var(--color-fg);
  --tw-prose-lead: var(--color-muted);
  --tw-prose-links: var(--color-accent-ink);
  --tw-prose-bold: var(--color-fg);
  --tw-prose-counters: var(--color-muted);
  --tw-prose-bullets: var(--color-muted);
  --tw-prose-hr: var(--color-line);
  --tw-prose-quotes: var(--color-fg);
  --tw-prose-quote-borders: var(--color-accent);
  --tw-prose-captions: var(--color-muted);
  --tw-prose-kbd: var(--color-fg);
  --tw-prose-kbd-shadows: color-mix(in oklab, var(--color-fg) 15%, transparent);
  --tw-prose-code: var(--color-fg);
  --tw-prose-pre-code: var(--color-fg);
  --tw-prose-pre-bg: var(--color-code);
  --tw-prose-th-borders: var(--color-line);
  --tw-prose-td-borders: var(--color-line);
}

@theme {
  --tab-size-github: 8;
}
Show code · src/app/tailwind/utilities/Custom.tsx
// Utilities defined in globals.css with @utility, used like built-in ones.
const tags = ["@theme", "@utility", "@variant", "@custom-variant", "@plugin", "@source", "@apply", "@config", "--value()", "--modifier()", "@reference", "@import"];

function Chips({ className, label }: { className: string; label: string }) {
  return (
    <div className="grid gap-1">
      <p className="m-0 text-xs text-muted"><code>{label}</code></p>
      {/* Focusable so keyboard users can scroll it too. */}
      <ul tabIndex={0} aria-label={`Directives, ${label}`} className={`m-0 flex list-none gap-2 overflow-x-auto p-0 py-1 ${className}`}>
        {tags.map((t) => <li key={t} className="shrink-0 rounded-full border border-line bg-bg px-3 py-1 font-mono text-xs">{t}</li>)}
      </ul>
    </div>
  );
}

export function ScrollRows() {
  return (
    <div className="grid gap-3" data-testid="scroll-rows">
      <Chips className="" label="overflow-x-auto" />
      <Chips className="scrollbar-hidden fade-x-8" label="overflow-x-auto scrollbar-hidden fade-x-8" />
    </div>
  );
}

const source = "function size() {\n\tif (wide) {\n\t\treturn 2;\n\t}\n}";
export function TabSizes() {
  return (
    <div className="grid gap-3 sm:grid-cols-3">
      {["tab-2", "tab-4", "tab-github"].map((cls) => (
        <div key={cls} className="grid min-w-0 gap-1">
          <p className="m-0 text-xs text-muted"><code>{cls}</code></p>
          <pre tabIndex={0} data-testid={cls}
            className={`m-0 overflow-x-auto rounded-lg bg-code px-3 py-2 font-mono text-xs ${cls}`}>
            {source}
          </pre>
        </div>
      ))}
    </div>
  );
}

// hocus: is a home-made variant; link-grow uses it from inside its own CSS.
export function Hocus() {
  return (
    <div className="grid gap-3">
      <div className="flex flex-wrap gap-2">
        {["Hover me", "or Tab to me"].map((t) => (
          <button key={t} type="button" className="cursor-pointer rounded-md border border-line px-3 py-1.5 text-sm transition-colors hocus:border-accent hocus:bg-accent hocus:text-white hocus:outline-none">
            {t}
          </button>
        ))}
      </div>
      <p className="m-0 text-sm">
        Links with <code>link-grow</code>: <a className="link-grow" href="#hocus">an underline that grows</a> on hover or
        keyboard focus, <a className="link-grow" href="#hocus">like this one</a>.
      </p>
    </div>
  );
}
Show code · src/app/tailwind/utilities/Prose.tsx
"use client";

import { useState } from "react";

// The typography plugin styles HTML you don't write class by class: Markdown,
// CMS content. prose turns it on; prose-site (a utility in globals.css) points
// its colours at this site's tokens. (The size names below are full class
// names, so Tailwind finds them when it scans this file.)
const sizes = ["prose-sm", "prose-base", "prose-lg"] as const;

export function Prose() {
  const [on, setOn] = useState(true);
  const [size, setSize] = useState<(typeof sizes)[number]>("prose-base");
  const classes = on
    ? `prose prose-site ${size} prose-code:before:content-none prose-code:after:content-none`
    : "";
  return (
    <div className="grid grid-cols-[minmax(0,1fr)] gap-3">
      <div className="flex flex-wrap items-center gap-4 text-sm">
        <label className="flex items-center gap-2">
          <input type="checkbox" checked={on} onChange={(e) => setOn(e.target.checked)} /> prose
        </label>
        <label className="flex items-center gap-2">
          Size
          <select value={size} onChange={(e) => setSize(e.target.value as (typeof sizes)[number])} disabled={!on}>
            {sizes.map((s) => <option key={s}>{s}</option>)}
          </select>
        </label>
        <code className="text-xs break-all" data-testid="prose-classes">{classes || "(no classes)"}</code>
      </div>
      <article className={`rounded-xl border border-line bg-bg p-4 ${classes}`} data-testid="prose">
        <h3>Writing about CSS</h3>
        <p className="lead">The same HTML, styled by one class. Good for anything that arrives as Markdown or from a CMS.</p>
        <p>
          Tailwind styles markup you write, one utility at a time. Long-form text is different: you don&apos;t control each{" "}
          <code>&lt;p&gt;</code>, so the plugin styles the elements for you, with <strong>sensible rhythm</strong>,{" "}
          <a href="https://github.com/tailwindlabs/tailwindcss-typography" target="_blank" rel="noreferrer">links</a> and
          lists.
        </p>
        <h4>What it covers</h4>
        <ul>
          <li>Headings, lists and quotes</li>
          <li>Inline <code>code</code> and code blocks</li>
          <li>Tables, keyboard keys like <kbd>Ctrl</kbd> + <kbd>K</kbd>, and rules</li>
        </ul>
        <blockquote><p>Use utilities for the layout, and prose for the words.</p></blockquote>
        <pre tabIndex={0}><code>{`@plugin "@tailwindcss/typography";`}</code></pre>
        <table>
          <thead><tr><th>Class</th><th>Does</th></tr></thead>
          <tbody>
            <tr><td><code>prose</code></td><td>Turns it on</td></tr>
            <tr><td><code>prose-lg</code></td><td>Scales the whole set</td></tr>
            <tr><td><code>prose-a:…</code></td><td>Targets one element type</td></tr>
          </tbody>
        </table>
      </article>
    </div>
  );
}