--- name: creating-custom-utilities description: Create custom utilities with @utility directive supporting static utilities, functional utilities with values, theme-based utilities, and multi-value utilities. Use when extending Tailwind with custom CSS properties or patterns. allowed-tools: Read, Write, Edit, Grep, Glob --- # Creating Custom Utilities ## Purpose The `@utility` directive creates custom utility classes with full variant support (hover, focus, responsive, etc.). This is the proper way to extend Tailwind v4 with custom utilities. ## Static Utilities Define utilities with fixed values: ```css @utility content-auto { content-visibility: auto; } @utility content-hidden { content-visibility: hidden; } @utility text-balance { text-wrap: balance; } @utility text-pretty { text-wrap: pretty; } @utility bg-glass { background: rgba(255, 255, 255, 0.1); backdrop-filter: blur(10px); border: 1px solid rgba(255, 255, 255, 0.2); } ``` **Usage:** ```html

``` Static utilities work with all variants automatically. ## Functional Utilities with Integer Values Create utilities that accept numeric values: ```css @utility mt-* { margin-top: calc(0.25rem * --value(integer)); } @utility truncate-* { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: --value(integer); overflow: hidden; } @utility text-stroke-* { -webkit-text-stroke-width: --value(integer) px; } ``` **Usage:** ```html

``` The `--value(integer)` function extracts the numeric value from the class name. ## Theme-Based Utilities Reference theme variables using `--value(--namespace- *)`: ```css @theme { --tab-size-2: 2; --tab-size-4: 4; --tab-size-8: 8; --tab-size-github: 8; } @utility tab-* { tab-size: --value(--tab-size- *); } ``` **Usage:** ```html
...
...
``` **Another example:** ```css @theme { --aspect-square: 1 / 1; --aspect-video: 16 / 9; --aspect-portrait: 3 / 4; } @utility aspect-* { aspect-ratio: --value(--aspect- *); } ``` **Usage:** ```html