Capítulo 6 de 29

Chapter 6: Functions and Directives

Core Idea

This is the reference index for every custom at-rule ("directive") and build-time function Tailwind adds to CSS — @import/@theme/@source/@utility/@variant/@custom-variant/@apply/@reference plus the legacy-compat @config/@plugin, and the --alpha()/--spacing()/theme() functions.

Key Concepts

  • @import "tailwindcss": inlines Tailwind (and any other CSS file) at build time — no separate preprocessor import step needed.
  • @theme: defines custom design tokens (colors, fonts, breakpoints) as CSS custom properties (full mechanics in the Theme chapter).
  • @source "<path>": registers extra files/directories for class detection beyond Tailwind's automatic scan (e.g. a node_modules UI package).
  • @utility: registers a custom utility class that gets variant support (hover:, focus:, lg:) automatically.
  • @variant: applies a built-in Tailwind variant inside hand-written CSS, e.g. @variant dark { background: black; } inside a rule.
  • @custom-variant: defines a brand-new variant, e.g. @custom-variant theme-midnight (&:where([data-theme="midnight"] *));, enabling theme-midnight:bg-black.
  • @apply: inlines existing utility classes into hand-written CSS (@apply rounded-b-lg shadow-md;) — useful for overriding third-party library styles while still using Tailwind's design tokens/syntax.
  • @reference: imports a stylesheet for reference only (no output duplication) so @apply/@variant can resolve theme values inside a Vue/Svelte <style> block or a CSS Module, which are otherwise processed in isolation. If the project uses only the unmodified default theme, @reference "tailwindcss"; alone is enough — otherwise reference the actual project stylesheet (e.g. @reference "../../app.css";).
  • Subpath imports: @import, @reference, @plugin, and @config all support Node.js-style subpath imports ("imports": {"#app.css": "./src/css/app.css"} in package.json) when using the CLI, Vite, or PostCSS — works like a bundler/TS path alias.
  • --alpha(): adjusts a color's opacity at build time, e.g. --alpha(var(--color-lime-300) / 50%) compiles to color-mix(in oklab, var(--color-lime-300) 50%, transparent).
  • --spacing(): generates a value from the spacing scale, e.g. --spacing(4) compiles to calc(var(--spacing) * 4); combines with calc() inside arbitrary values (py-[calc(--spacing(4)-1px)]).
  • @config (v3 compat): loads a legacy JS-based tailwind.config.js; can be combined with @theme/@utility to migrate incrementally (CSS-defined values are merged where possible and take precedence over the config file). corePlugins, safelist, and separator config options are not supported in v4 — use @source inline() for safelisting instead.
  • @plugin (v3 compat): loads a legacy JS plugin by package name or local path, e.g. @plugin "@tailwindcss/typography";.
  • theme() function (deprecated): dot-notation access to theme values, e.g. theme(spacing.12) — superseded by referencing the generated CSS variables directly (var(--spacing-12)).

Reference Tables

Directive/FunctionPurposeStatus
@importInline CSS files, including Tailwind itselfCurrent
@themeDefine design tokensCurrent
@sourceRegister extra scan pathsCurrent
@utilityRegister a custom utilityCurrent
@variantApply a variant inside CSSCurrent
@custom-variantDefine a new variantCurrent
@applyInline utility classes into custom CSSCurrent
@referenceImport a stylesheet for theme access only, no outputCurrent
--alpha()Adjust a color's opacityCurrent
--spacing()Compute a value from the spacing scaleCurrent
@configLoad a legacy tailwind.config.jsv3 compatibility only
@pluginLoad a legacy JS pluginv3 compatibility only
theme()Dot-notation theme value accessDeprecated — use CSS variables

Key Takeaways

  1. This chapter is the index — for full mechanics of @theme/@utility/@custom-variant/@variant, see Adding Custom Styles; for @source, see Detecting Classes in Source Files.
  2. @reference is specifically for scoped-CSS contexts (Vue/Svelte <style>, CSS Modules) that don't automatically see the project's @theme — without it, @apply/@variant in those files can't resolve custom tokens.
  3. @config/@plugin/theme() exist only to ease migration from Tailwind v3 — new projects should use @theme/@utility/CSS variables instead, and corePlugins/safelist/separator config options have no v4 equivalent via @config.

Connects To

  • Adding Custom Styles: full walkthrough of @theme, @utility (including --value()/--modifier()), and @custom-variant.
  • Theme: what @theme tokens generate and how to reference them as CSS variables (the replacement for the deprecated theme() function).
  • Compatibility: @reference is the same fix recommended there for CSS Modules and framework <style> blocks.