Sub topic 4
Type hints for ambiguous utilities
This chapter assumes you can read basic HTML and CSS, including units and custom properties. It also assumes you know the Tailwind utilities p-4, text-lg and bg-blue-500, and the hover: and md: variant prefixes. It builds on the previous chapters, where you wrote values like w-[317px] and top-[calc(50%-2rem)].
The problem: one prefix, several properties
Many Tailwind utilities share a prefix but set different CSS properties. In the default theme:
text-lgsetsfont-size.text-blue-500setscolor.border-2setsborder-width.border-red-500setsborder-color.bg-blue-500setsbackground-color.bg-coversetsbackground-size.bg-centersetsbackground-position.
With theme names, Tailwind knows which property you mean, because each name belongs to exactly one theme group. An arbitrary value has no name to look up. Tailwind only has the text between the brackets. So text-[...] could mean font size or color, and border-[...] could mean width or color.
Tailwind first tries to settle this by looking at the value:
<p class="text-[22px] text-[#bada55]">Two classes, two properties</p>22px is a length, so text-[22px] becomes font-size: 22px. #bada55 is a color, so text-[#bada55] becomes color: #bada55. The same applies to rgb(), hsl() and named colors.
When inference fails
Inference breaks down when Tailwind can't see the value's type. The usual case is a CSS variable:
<p class="text-[var(--hero-size)]">Which property?</p>var(--hero-size) could hold 3rem or #334155. Tailwind can't read your CSS at build time, so it can't tell. The same happens with calc() and other functions whose result type depends on their contents.
Don't depend on whichever guess Tailwind makes in this case. A wrong guess doesn't produce an error. You get the wrong property, or no rule at all, and you only notice in the browser.
The type hint syntax
A type hint is a CSS data type name, then a colon, placed at the start of the brackets:
<p class="text-[length:var(--hero-size)] text-[color:var(--hero-tone)]">
Headline
</p>Tailwind removes the length: and color: prefixes and uses them to pick the property. It then writes the rest of the value into the CSS.
| Class | Generated declaration |
|---|---|
| `text-[length:var(--hero-size)]` | `font-size: var(--hero-size)` |
| `text-[color:var(--hero-tone)]` | `color: var(--hero-tone)` |
| `border-[length:var(--rule)]` | `border-width: var(--rule)` |
| `border-[color:var(--rule-tone)]` | `border-color: var(--rule-tone)` |
The hint is only an instruction to Tailwind. It never appears in the CSS output. Tailwind doesn't check that the variable holds a length or a color. If --hero-size holds #334155, you get font-size: #334155, which the browser ignores.
The hint is part of the class name, so it must be in the source as written. Class names that contain a : are fine. The colon inside the brackets doesn't clash with variant colons, because Tailwind only splits variants outside brackets:
<p class="text-[length:var(--hero-size)] md:text-[length:var(--hero-size-lg)] hover:text-[color:var(--hero-hover)]">
Headline
</p>Here md: and hover: are variants. length: and color: are hints.
Common hint names
Different utilities accept different hints. These are the ones you will use most:
| Hint | Meaning | Typical use |
|---|---|---|
| `length:` | A length such as `12px`, `2rem` or `var(...)` holding one | `text-`, `border-`, `bg-` (size), `outline-`, `ring-` |
| `color:` | Any CSS color | `text-`, `bg-`, `border-`, `fill-`, `stroke-`, `shadow-`, `decoration-` |
| `percentage:` | A percentage | `bg-` (position or size) |
| `number:` | A unitless number | `font-[number:...]` for font weight, `stroke-[number:...]` for stroke width |
| `url:` | A `url()` value | `bg-[url:var(--hero-image)]` |
| `image:` | A gradient or other image value | `bg-[image:var(--hero-gradient)]` |
| `position:` | A background position | `bg-[position:var(--focus)]` |
| `family-name:` | A font family | `font-[family-name:var(--font-display)]` |
| `shadow:` | A full shadow value | `shadow-[shadow:var(--card-shadow)]` |
The exact set of hints a utility accepts depends on the Tailwind version. If a hint isn't accepted by that utility, no CSS is generated. If a hinted class does nothing, check the hint name first.
Worked example: a themeable card
This page defines four custom properties on :root and uses hints to attach each one to the right property. It runs in any browser. The script tag loads Tailwind v4's browser build, which is meant for experiments and not for production.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Type hints</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<style>
:root {
--card-size: 1.25rem;
--card-tone: #0f766e;
--card-rule: 4px;
--card-font: Georgia, serif;
}
</style>
</head>
<body class="p-8">
<article
class="border-l-[length:var(--card-rule)] border-l-[color:var(--card-tone)] pl-4"
>
<h2
class="text-[length:var(--card-size)] text-[color:var(--card-tone)] font-[family-name:var(--card-font)]"
>
Release notes
</h2>
<p class="text-[color:var(--card-tone)]">Hints pick the property.</p>
</article>
</body>
</html>The h2 shows three hints on one element:
text-[length:var(--card-size)]setsfont-size.text-[color:var(--card-tone)]setscolor.font-[family-name:var(--card-font)]setsfont-family.
Without the hints, all three classes would have unclear meaning. font-[var(--card-font)] could be a weight or a family.
The article uses the same idea with a directional border. border-l-[length:...] sets border-left-width, and border-l-[color:...] sets border-left-color. Because the border width is set, the border style still needs to be solid. Tailwind's preflight sets border-style: solid for all elements, so the border draws. If you turn off preflight, add border-solid.
How Tailwind decides
The resolution order for an arbitrary value is:
- If the brackets start with a known type name and a colon, use that type.
- Otherwise, inspect the value. Hex codes, color functions and color keywords are colors. Numbers followed by units are lengths.
url(...)is a url. - If the value is
var(...)or similar and has no hint, the type is unknown, and the result is unreliable.
Hints and underscores
Hints and underscores work together. Underscores still turn into spaces, and the hint is stripped first:
<div class="bg-[length:200px_100px] bg-[url:var(--pattern)] bg-no-repeat"></div>bg-[length:200px_100px] produces background-size: 200px 100px. Without the hint, bg-[200px_100px] would be ambiguous, because those values could also be a background position. Here the hint says length:, so it is read as a size.
A related case is backgrounds, which have the most meanings of any prefix. Compare:
<div class="bg-[#0f766e]"></div>
<div class="bg-[length:200px_100px]"></div>
<div class="bg-[position:center_top]"></div>
<div class="bg-[image:linear-gradient(to_right,#0f766e,#1e293b)]"></div>
<div class="bg-[url:var(--hero)]"></div>Each line sets a different property: background-color, background-size, background-position, and two kinds of background-image. Hints make that explicit and make the class readable in code review.
Version note: the parenthesis shorthand
Tailwind v4 added a shorter syntax for CSS variables. In it, (--name) replaces [var(--name)], and hints go inside the parentheses:
<p class="text-(length:--hero-size) text-(color:--hero-tone)">Headline</p>This generates the same CSS as the square-bracket version. In Tailwind v3, only the square-bracket form exists. The rest of this tutorial uses square brackets, which work in both versions.
Skipping the problem with arbitrary properties
You can also avoid ambiguity by writing the CSS property yourself, using an arbitrary property:
<p class="[font-size:var(--hero-size)] [color:var(--hero-tone)]">Headline</p>Here nothing needs to be inferred, because you name the property. The trade-offs:
- Hints keep you inside the utility system. The class still looks like
text-..., and it can pick up features tied to that utility, such as a line-height modifier (text-[length:var(--hero-size)]/6) or an opacity modifier on colors. - Arbitrary properties are clearer when a reader is unsure which property a hint maps to. They also work for properties that have no utility.
- Both work with variants:
hover:[color:var(--hero-hover)]andhover:text-[color:var(--hero-hover)]are equivalent in effect.
Pick one style per project so that code reads consistently.
Common errors
1. No hint on a variable.
<p class="text-[var(--hero-size)]">Headline</p>This may produce color: var(--hero-size), so the text turns invalid or inherits instead of resizing. Add length:.
2. A hint that doesn't match the value.
<p class="text-[color:1.5rem]">Headline</p>Tailwind accepts the hint, but the browser rejects color: 1.5rem. Nothing is reported at build time. Check the output in the browser's inspector.
3. A hint the utility doesn't support. Hints are only meaningful for properties that accept that type. w-[color:red] generates nothing.
4. Building the hint dynamically.
// Not detected: the class name is never written out in full
const broken = `text-[${kind}:var(--x)]`;
// Detected: both complete class names appear in the source
const classes = {
size: "text-[length:var(--x)]",
tone: "text-[color:var(--x)]",
};
const working = classes[kind];Tailwind reads source files as plain text. A hint pieced together at runtime never appears in full, so no CSS is generated for it. A later chapter covers this in detail.
5. Spaces instead of underscores. bg-[length:200px 100px] contains a space, so the class splits in two. Use bg-[length:200px_100px].
Debugging checklist
When an arbitrary value with a variable does nothing:
- Open the browser's developer tools and select the element.
- Look for a rule with your class name, with the backslash-escaped brackets.
- If there is no rule, the class wasn't detected, or the hint isn't valid for that utility.
- If there is a rule but it uses the wrong property, add or fix the hint.
- If the property is right but the value is crossed out as invalid, check that the variable holds the type you hinted, and that it is defined on an ancestor.
Exercise
A teammate writes <h1 class="text-[var(--title)]"> and the heading's color changes instead of its size. The variable --title holds 2.5rem. What is the fix, and what CSS does it generate?
Show answer
Add the length: hint: text-[length:var(--title)]. Tailwind strips the hint and generates font-size: var(--title). Without the hint, Tailwind could not tell from var(--title) whether the value was a size or a color.
Exercise
Write the classes for a div that uses a background image from the variable --hero (holding a url(...) value), a background size of cover kept as a utility, and a border whose width comes from --edge and whose color comes from --edge-tone. Use hints wherever they are needed.
Show answer
<div class="bg-[url:var(--hero)] bg-cover border-[length:var(--edge)] border-[color:var(--edge-tone)]"></div>. The url: hint picks background-image, bg-cover is a normal theme utility, length: picks border-width, and color: picks border-color.
Exercise
Why does ` text-[${kind}:var(--x)] fail even though text-[length:var(--x)]` works when written by hand?
Show answer
Tailwind finds class names by scanning source files as plain text. The template string never contains the full class name, so Tailwind never sees text-[length:var(--x)] and generates no CSS for it. Write each complete class name in the source, for example in an object, and choose between them at runtime.