Sub topic 6
Arbitrary properties for CSS with no utility
This chapter assumes you know basic HTML and CSS, including custom properties (--name) and selectors. It also assumes you know Tailwind utilities such as p-4 and bg-blue-500, and variant prefixes such as hover: and md:. Earlier chapters covered arbitrary values like w-[317px]. Here the brackets hold a whole declaration instead of a single value.
Where utilities run out
Tailwind ships a utility for most common CSS properties. It cannot ship one for all of them. CSS adds new properties faster than any framework can wrap them, and some are too specialized to deserve a name. Examples are mask-type, scrollbar-gutter, view-transition-name and contain.
When you need one of these, you have three options:
- Write a separate CSS rule in your stylesheet.
- Add a custom utility or plugin.
- Write an arbitrary property: a complete CSS declaration inside square brackets, used as a class name.
The third option keeps the style on the element, supports variants, and needs no configuration.
The syntax
An arbitrary property is [property:value].
<div class="[mask-type:luminance]"></div>Tailwind sees the class [mask-type:luminance], recognizes the property:value shape, and generates a rule like this one:
.\[mask-type\:luminance\] {
mask-type: luminance;
}The class name is escaped in the stylesheet, so the brackets and colon are valid in the selector. You do not write that CSS yourself.
Three rules define the syntax:
- The brackets contain exactly one declaration, with a colon between the property and the value.
- The property name is written as in CSS:
scrollbar-gutter,-webkit-text-stroke,--my-variable. - The value is written as in CSS, except that spaces become underscores (covered below).
How this differs from an arbitrary value
An arbitrary value is attached to a utility: w-[317px]. The utility name (w-) decides which property is set, and the brackets supply the value. An arbitrary property has no utility name. The property is in the brackets, so the class always begins with [.
| Form | Example | Who picks the property? | Who supplies the value? |
|---|---|---|---|
| Utility | `w-4` | The utility | The theme |
| Arbitrary value | `w-[317px]` | The utility | You |
| Arbitrary property | `[scrollbar-gutter:stable]` | You | You |
Because you pick the property, Tailwind does not check it. If you write [scrollbar-gutterr:stable], Tailwind generates a rule with the misspelled property, and the browser ignores it. Nothing reports an error. When an arbitrary property seems to do nothing, check the spelling in DevTools first.
Spaces and underscores in values
Class names cannot contain spaces, because a space separates one class from the next. For values that need spaces, Tailwind converts each underscore to a space in the generated CSS.
<h1 class="[-webkit-text-stroke:1px_black]">Outlined</h1>This produces -webkit-text-stroke: 1px black;.
The same rule applies to values with several parts, such as font variation settings:
<p class="[font-variation-settings:'wght'_650,'wdth'_90]">Variable font</p>The generated value is 'wght' 650, 'wdth' 90. Commas need no escaping, and neither do quotes. A single quote inside a class attribute is fine as long as the attribute itself uses double quotes.
If you need a literal underscore, escape it with a backslash. In practice you rarely need this. Tailwind already leaves underscores alone in some contexts where a space would be wrong, such as inside url().
<div class="[--label:'snake\_case']"></div>This sets --label: 'snake_case', with the underscore kept.
Custom properties as arbitrary properties
A property name that starts with -- is a CSS custom property, and it works in this syntax. This is one of the most useful forms.
<div class="[--pad:0.5rem] md:[--pad:1.5rem] p-[var(--pad)] bg-gray-100">
Padding grows at the md breakpoint.
</div>Here is what each class does:
[--pad:0.5rem]defines--padon the element.md:[--pad:1.5rem]redefines it from themdbreakpoint up.p-[var(--pad)]is an arbitrary value that reads the variable.
The padding utility never changes. Only the variable does. That is useful when several utilities, or several descendants, share one value. Custom properties inherit, so children can read var(--pad) too.
The scroll offset pattern from the plan works the same way:
<section class="[--scroll-offset:56px] lg:[--scroll-offset:44px] scroll-mt-[var(--scroll-offset)]">
...
</section>Type hints still apply to values
Setting a variable with an arbitrary property does not tell Tailwind what type the value is. When you later read it with a utility such as text-[...], Tailwind may not know whether var(--x) is a size or a color. Use a type hint in that case:
<p class="[--hero-size:3rem] [--hero-tone:#0f766e] text-[length:var(--hero-size)] text-[color:var(--hero-tone)]">
Headline
</p>Without length: and color:, text-[var(--hero-size)] would be ambiguous. The hints remove the guesswork.
Theme values as variables
In Tailwind v4, theme values are exposed as CSS variables, such as --color-blue-500. An arbitrary property can read them directly:
<div class="[border-color:var(--color-blue-500)]"></div>In v3, the equivalent is the theme() function inside the brackets, for example [border-color:theme(colors.blue.500)]. Either way, the value stays tied to your theme instead of being a hard-coded hex string.
A runnable example
This complete page uses Tailwind v4 from its browser build, so you can open it as a local file. It combines three arbitrary properties and one variable.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Arbitrary properties</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-6 space-y-6">
<h1 class="text-5xl font-black text-white [-webkit-text-stroke:1px_black]">
Outlined heading
</h1>
<div class="h-24 w-72 overflow-y-auto bg-gray-100 [scrollbar-gutter:stable]">
<p>
This box reserves space for the scrollbar even when the content is
short, because scrollbar-gutter is set to stable. Add more text to see
the scrollbar appear without the layout shifting sideways.
</p>
</div>
<div class="[--pad:0.5rem] md:[--pad:2rem] p-[var(--pad)] bg-teal-100">
Resize the window past the md breakpoint and the padding changes.
</div>
</body>
</html>To check the result, open DevTools, select the heading, and look at the computed styles. You should see -webkit-text-stroke: 1px black.
Combining with variants
An arbitrary property is a normal class, so any variant can prefix it. Variants go before the opening bracket.
<div class="[mask-type:luminance] hover:[mask-type:alpha]"></div>Variants stack in the usual way:
<div class="[contain:content] md:hover:[contain:layout]"></div>Several variant types work with arbitrary properties:
- State variants:
hover:,focus:,disabled: - Breakpoints:
sm:,md:,lg: - Dark mode:
dark: - Group and peer variants:
group-hover:,peer-checked: - Arbitrary variants, covered in a later chapter:
[&>li]:
The order matters only for readability. The variant chain is applied to the selector, and the declaration goes inside.
Conflicts with other utilities
If one element has two classes that set the same property, the winner depends on the order of rules in the generated stylesheet. It does not depend on the order of the class names in your HTML. This applies to an arbitrary property and a regular utility alike:
<!-- Avoid: two classes set color -->
<p class="text-blue-500 [color:red]">Which color?</p>Do not rely on a particular outcome here. Pick one way to set each property on an element. If a variant must override a base value, give both the same form ([color:red] hover:[color:blue]), or use variants consistently.
How Tailwind finds them
The diagram shows what happens to an arbitrary property class between your source file and the browser.
The scanning step is the important one. Tailwind reads your files as plain text and looks for strings that could be class names. It does not run your JavaScript, so it cannot know what a template literal will produce.
Why dynamic construction fails
The same scanner explains the most common mistake with arbitrary properties. A class name must appear in your source as a complete, literal string.
// Not detected: Tailwind never sees the complete class name
const broken = `[scrollbar-gutter:${mode}]`;
// Detected: every class name appears in full in the source
const gutters = {
stable: "[scrollbar-gutter:stable]",
both: "[scrollbar-gutter:stable_both-edges]",
};
const working = gutters[mode];In the first line, the scanner sees the fragment [scrollbar-gutter: followed by ${mode}]. Neither piece is a valid class, so no CSS is generated. The page works in your head and breaks in the browser, and nothing reports an error.
If the value really comes from data at runtime, such as a color picked by a user, use an inline CSS variable and a static class that reads it:
const el = document.querySelector("#panel");
el.style.setProperty("--panel-tone", userColor);<div id="panel" class="[background-color:var(--panel-tone)]"></div>The class is complete and static, so Tailwind generates it. The value changes at runtime through the variable.
Edge cases and gotchas
- No validation. Tailwind emits whatever you write. A wrong property name or invalid value produces a rule the browser drops silently.
- Colons in values. Only the first colon splits property from value, so values that contain colons, such as
url(https://...), are fine. Underscores in URLs are preserved, so you do not need to escape them. - Vendor prefixes. Tailwind adds prefixes for properties it knows about in its own utilities. For an arbitrary property, write the prefixed name yourself when you need it, as in
[-webkit-text-stroke:1px_black]. - Class length. Long declarations make long class lists. If one value needs many parts, a variable or a component extraction is easier to read.
- Important modifier. To force priority, add the important modifier. The position of
!differs between versions (v3 puts it before the class, v4 after), so check your version's docs before relying on it. - Multiple declarations. One arbitrary property sets one declaration. To set two, write two classes.
When to use an arbitrary property
Use one when all of these are true:
- No utility sets the property.
- The style is specific to one element or a small number of elements.
- You do not want to add anything to your theme or stylesheet.
Choose another approach when you see these signs:
| Situation | Better choice |
|---|---|
| The same declaration appears on many elements | Extract a component, or add a custom utility |
| The value is a brand color or spacing step used across the site | Add it to the theme |
| The style is long and has many parts | A separate CSS rule or component class |
| A built-in utility already exists | Use the utility |
A good test: if you copy the same bracketed class a third time, it probably belongs in the theme or a component.
Summary of the pattern
[property:value]writes one CSS declaration as a class.- Underscores become spaces. Escape a literal underscore with a backslash.
[--name:value]defines a CSS variable, which other classes can read withvar(--name).- Prefix with variants (
hover:,md:,dark:) as with any class. - Tailwind does not validate the property. Typos fail silently.
- Write the complete class name in your source so the scanner can find it.
Exercise
The class [font-variation-settings:'wght'_700] is added to a heading, but the heading does not change weight. DevTools shows the rule font-variation-settings: 'wght' 700 applied. Name two likely causes that are not related to Tailwind's class detection.
Show answer
The CSS was generated correctly, so the problem is outside Tailwind. First, the font in use may not be a variable font, or may lack a wght axis, so the browser has nothing to vary. Second, another rule may override the property, for example a later rule or an inline style that sets font-variation-settings, or a higher-specificity selector. Check the font file's axes and the Styles panel for a struck-through declaration.
Exercise
Rewrite this so Tailwind generates the CSS, and explain why the original fails:
const cls = md:[scroll-margin-top:${px}px];
Show answer
The original builds part of the class name at runtime, so Tailwind's text scanner never sees a complete class. Use full class names in a lookup, for example const offsets = { small: "md:[scroll-margin-top:40px]", large: "md:[scroll-margin-top:80px]" }; const cls = offsets[size];. If the number truly comes from runtime data, set --offset with style.setProperty and use the static class md:[scroll-margin-top:var(--offset)].
Exercise
Write classes for a card that has --card-pad set to 1rem by default and 2rem from the lg breakpoint, and uses that variable as its padding.
Show answer
[--card-pad:1rem] lg:[--card-pad:2rem] p-[var(--card-pad)]. The first two classes are arbitrary properties that define the variable. The last is an arbitrary value that reads it.
Exercise
What CSS does the class hover:[mask-type:alpha] produce, in outline, and what happens if you misspell the property as mask-typ?
Show answer
It produces a rule whose selector is the escaped class name with the :hover state, and whose declaration is mask-type: alpha. If you misspell the property, Tailwind still emits mask-typ: alpha because it does not validate property names. The browser ignores the unknown property, so nothing changes and no error appears.