Sub topic 4
Type hints for ambiguous utilities
This chapter assumes you can write simple arbitrary values such as w-[317px] and that you know what a CSS custom property (variable) is. You should also be comfortable with Tailwind utilities like text-lg and border-2.
Why some utilities are ambiguous
Several Tailwind utilities share one prefix but set different CSS properties. text-lg sets font size. text-red-500 sets color. Both start with text-.
With theme values, Tailwind looks the name up in the theme, so there is no confusion. With an arbitrary value, Tailwind has only the text inside the brackets. It inspects that text and decides which property you meant.
| Class | What Tailwind sees | Generated property |
|---|---|---|
| `text-[14px]` | A length | `font-size` |
| `text-[#bada55]` | A hex color | `color` |
| `border-[3px]` | A length | `border-width` |
| `border-[#bada55]` | A hex color | `border-color` |
Tailwind recognizes 14px as a length and #bada55 as a color from the text alone. That inference works for literal values.
When inference fails
A CSS variable hides its type. Tailwind cannot know whether var(--hero-size) holds a length or a color, because the value is only resolved in the browser.
<p class="text-[var(--hero-size)]">Headline</p>The utility is ambiguous. Tailwind may choose the wrong property or generate nothing, so the text silently keeps its old size. You will not see an error. Your style is simply missing.
Adding a type hint
A type hint is a type name followed by a colon, placed at the start of the bracketed value. It tells Tailwind which kind of value follows.
<p class="text-[length:var(--hero-size)] text-[color:var(--hero-tone)]">Headline</p>The two classes now resolve as follows:
text-[length:var(--hero-size)]generatesfont-size: var(--hero-size).text-[color:var(--hero-tone)]generatescolor: var(--hero-tone).
The hint is removed from the output. Only the value after the colon reaches the CSS.
Here is a complete page you can open in a browser. It assumes a Tailwind setup that scans this file.
<!doctype html>
<html lang="en">
<body>
<div style="--hero-size: 2.5rem; --hero-tone: #0f766e;">
<p class="text-[length:var(--hero-size)] text-[color:var(--hero-tone)]">
Headline
</p>
</div>
</body>
</html>The variables are set on the parent. The paragraph inherits them, so the headline renders large and teal.
Common hints and where they matter
The hint names a CSS value type. These are the ones you will use most.
| Utility | Hint | Property it selects |
|---|---|---|
| `text-[...]` | `length:` | `font-size` |
| `text-[...]` | `color:` | `color` |
| `border-[...]` | `length:` | `border-width` |
| `border-[...]` | `color:` | `border-color` |
| `bg-[...]` | `color:` | `background-color` |
| `bg-[...]` | `url:` or `image:` | `background-image` |
| `bg-[...]` | `position:` | `background-position` |
| `bg-[...]` | `size:` | `background-size` |
| `font-[...]` | `family-name:` | `font-family` |
| `font-[...]` | `number:` | `font-weight` |
The pattern is the same for each. If the property you want shares its prefix with another property, and the value is a variable, add the hint.
<div class="border-[length:var(--rule)] border-[color:var(--rule-tone)] bg-[url:var(--pattern)]">
Card
</div>> Note: the hint list above covers the types Tailwind documents for these utilities. If a hint is not accepted, check the Tailwind version you use, because the supported names can differ between versions.
Common mistakes
- Leaving out the hint when the value is a variable. This is the most frequent cause of a style that does nothing.
- Adding a hint when the value is a literal.
text-[length:14px]works, buttext-[14px]is shorter and means the same thing. - Using the wrong hint.
text-[color:var(--hero-size)]sets the text color to a length, which the browser discards as invalid. - Putting spaces around the colon. Write
length:var(--x), with no space. Spaces in class names must be written as underscores, and a stray space splits the class in two.
Exercise
A component sets --accent: #e11d48; on its parent. You write border-[var(--accent)] on a child and the border color does not change. Why, and how do you fix it?
Show answer
var(--accent) hides its type, so border-[...] could mean border width or border color, and Tailwind cannot decide. Add a type hint: border-[color:var(--accent)]. This generates border-color: var(--accent). The element also needs a border width, such as border-2, to show a border at all.
Exercise
Write the classes that set a paragraph's font size from --size and its text color from --tone.
Show answer
text-[length:var(--size)] text-[color:var(--tone)]. The length: hint selects font-size and the color: hint selects color.