Sub topic 1
Where Utilities End: The Theme and Its Limits
Prerequisites
Assume you know basic HTML and CSS, including units, custom properties, and selectors. You should also know Tailwind utilities such as p-4, text-lg, and bg-blue-500, plus variants such as hover: and md:.
The Theme and Its Limits
Tailwind's theme is the named design system behind utilities. For example, p-4 uses a spacing value from the theme, and text-lg uses a configured font size. Theme values keep repeated choices consistent.
Sometimes a design needs a value that appears once. A theme may not contain a 317px width, a special grid definition, or a particular color. Adding every one-off value to the theme makes the design system harder to understand. Tailwind provides arbitrary values for these cases.
Arbitrary Values with Square Brackets
An arbitrary value is a CSS value written inside square brackets after a utility's prefix:
<!doctype html>
<html lang="en">
<body>
<div class="absolute top-[117px] left-[calc(50%-2rem)] w-[317px] bg-[#bada55]">
Positioned panel
</div>
</body>
</html>The utility still supplies the CSS property. The bracket supplies its value. Thus, w-[317px] means a width of 317px, while bg-[#bada55] means that background color.
Use this syntax for a local exception. If the same value appears across several components, add a meaningful theme value instead.
Spaces, Underscores, and Escaping
Class names cannot contain literal spaces, so use underscores inside arbitrary values. Tailwind converts them to spaces:
<div class="grid grid-cols-[1fr_500px_2fr] gap-[10px]">
<div>Sidebar</div>
<div>Content</div>
<div>Aside</div>
</div>This produces grid-template-columns: 1fr 500px 2fr. If the CSS value needs a literal underscore, escape it with a backslash, such as content-['hello\_world'].
Type Hints for Ambiguous Utilities
Some utilities accept several kinds of values. When a CSS variable makes the intended type unclear, add a type hint:
<p class="text-[length:var(--hero-size)] text-[color:var(--hero-tone)]">
Headline
</p>Here, length: tells Tailwind to generate a font-size value, while color: tells it to generate a text color. Type hints help Tailwind select the correct CSS property when the value alone is ambiguous.
Exercise
Which class sets a three-column grid with columns of 1fr, 500px, and 2fr?
Show answer
grid-cols-[1fr_500px_2fr].