Sub topic 6
Arbitrary Properties for CSS with No Utility
This chapter assumes you know basic HTML and CSS, including custom properties and selectors. It also assumes you have used Tailwind utilities such as p-4 and variant prefixes such as hover: and md:. You have already seen arbitrary values like w-[317px]. Here you will go one step further and write whole CSS declarations in square brackets.
The gap in the utility set
Tailwind ships utilities for most common CSS properties. It does not ship one for every property. New or niche properties such as mask-type, scroll-snap-stop variants, or text-wrap options may have no class, or may lag behind browser support.
An arbitrary property fills that gap. It is a class whose entire name is a CSS declaration inside square brackets. The property comes first, then a colon, then the value.
<div class="[mask-type:luminance]"></div>Tailwind generates this CSS for that class:
.\[mask-type\:luminance\] {
mask-type: luminance;
}There is no utility name in front of the bracket. The bracket content is the property and the value.
Arbitrary properties with variants
An arbitrary property is a normal class, so variants work in front of it. This includes state variants, breakpoints and dark mode.
<div class="[mask-type:luminance] hover:[mask-type:alpha]"></div>The element uses luminance by default and switches to alpha on hover. The variant comes first, then a colon, then the bracketed declaration.
You can stack variants in the same way you do with any utility:
<div class="[mask-type:luminance] md:hover:[mask-type:alpha]"></div>Defining CSS variables
A common use is to set a CSS custom property that changes under a variant. A custom property is a name that starts with --, and you read it with var().
<section class="[--scroll-offset:56px] lg:[--scroll-offset:44px] scroll-mt-[var(--scroll-offset)]">
...
</section>Here is what happens:
[--scroll-offset:56px]sets the variable on the element.lg:[--scroll-offset:44px]replaces it at thelgbreakpoint.scroll-mt-[var(--scroll-offset)]is an arbitrary value that reads the variable.
The variable keeps the number in one place. You change one declaration per breakpoint, and every class that reads the variable follows it.
Spaces and underscores
The earlier chapters on underscores apply here too. A class name cannot contain a space, so Tailwind turns underscores into spaces in the generated CSS. Use that when a value has several parts:
<div class="[grid-template-areas:'header_header'_'sidebar_main']"></div>This produces grid-template-areas: 'header header' 'sidebar main'. If you need a real underscore in the output, escape it with a backslash: \_.
Choosing between an arbitrary property and a utility
Check for an existing utility first. If scroll-mt-[var(--scroll-offset)] exists, it is clearer than [scroll-margin-top:var(--scroll-offset)]. Both produce the same CSS. The utility is shorter and easier to scan.
| Situation | Better choice |
|---|---|
| A utility exists and only the value is custom | Arbitrary value, such as `w-[317px]` |
| No utility exists for the property | Arbitrary property, such as `[mask-type:luminance]` |
| You need a CSS variable scoped to one element | Arbitrary property, such as `[--scroll-offset:56px]` |
| The same declaration appears in many places | A theme value or a component, covered in a later chapter |
Common errors
Most problems with arbitrary properties come from a small set of mistakes.
- Missing colon.
[mask-type]is not a declaration. Write[mask-type:luminance]. - Spaces in the value.
[grid-template-areas:'a a']breaks the class into two tokens. Use underscores. - Dynamic construction. `
[mask-type:${kind}]` is never found, because Tailwind reads source files as plain text. Write each complete class name in the source. The chapter on class detection explains this in full. - Fighting specificity. Arbitrary properties have the same specificity as other utilities. If a later rule in the stylesheet wins, the order of Tailwind's generated CSS decides, not the order of classes in your HTML.
Example: a component with no matching utility
Suppose a card must fade at its bottom edge using a mask, and the offset must change on large screens. No Tailwind utility is needed if you combine an arbitrary property with a variable.
<div class="[--fade:24px] lg:[--fade:48px] [mask-image:linear-gradient(to_bottom,black_calc(100%-var(--fade)),transparent)] h-48 overflow-hidden">
<p>Long text that fades out at the bottom edge.</p>
</div>The underscores become spaces, so the CSS reads linear-gradient(to bottom, black calc(100% - var(--fade)), transparent). The --fade variable changes from 24px to 48px at lg, and the gradient follows it.
If you use this pattern in several places, move it into a component or a theme value instead of repeating the long class.
Exercise
You want an element to set scroll-snap-stop: always only on screens at the md breakpoint and wider, and you assume no utility covers it. Write the class, and say what happens if you write md:[scroll-snap-stop always] instead.
Show answer
Write md:[scroll-snap-stop:always]. The variant comes first, then the bracketed declaration with a colon between property and value. In md:[scroll-snap-stop always] the colon is missing, so Tailwind has no property and value to read and generates no CSS for that class. (Tailwind also ships a snap-always utility for this property, so check for an existing utility before reaching for brackets.)
Exercise
A teammate writes ` className={[--gap:${gap}px]} ` and the gap never changes. Why, and how do you fix it?
Show answer
Tailwind scans source files as plain text and never runs the template string, so it never sees a complete class such as [--gap:8px] and generates no CSS for it. Fix it by listing each full class name in the source, for example in an object like { small: "[--gap:8px]", large: "[--gap:16px]" }, and looking one up by key.