Tailwind CSS Academy · 课时

任意值及其代价

使用 w-[347px] 这样的方括号表示法处理一次性值,并了解灵活性与样式表可能变得臃肿之间的权衡。

第 4 / 4 课13 个步骤

任意值及其代价 是 CoddyKit 上的免费 Tailwind CSS Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Tailwind CSS Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Tailwind CSS Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Are Arbitrary Values

Tailwind's arbitrary value syntax lets you use any CSS value directly inside a class name using square bracket notation. Instead of being limited to Tailwind's preset scale, you can write w-[347px], text-[#1a2b3c], or grid-cols-[1fr_2fr_1fr].

This feature, enabled by the JIT engine, is one of Tailwind's most powerful capabilities. It bridges the gap between utility classes and the rare cases where a precise, off-scale value is genuinely needed.

<div class="w-[347px] h-[calc(100vh-64px)] bg-[#1a1a2e] text-[#e2e8f0]">
  Arbitrary values for precise control
</div>

<div class="grid grid-cols-[1fr_2fr_1fr] gap-[22px]">
  Custom grid layout with arbitrary columns
</div>

Syntax Reference for Arbitrary Values

The bracket syntax works with any Tailwind utility prefix. The value inside the brackets is passed directly as the CSS value. Spaces within the value are represented by underscores (Tailwind replaces them before parsing).

For utilities that generate multiple CSS properties, only the relevant property receives the arbitrary value. For example, p-[12px] sets all four padding values to 12px, just like p-4 but with a custom value.

<!-- Width and height -->
<div class="w-[240px] h-[180px]"></div>

<!-- Custom color -->
<div class="bg-[#f0a500] text-[#2d3748]"></div>

<!-- Calc values -->
<div class="top-[calc(50%_-_24px)]"></div>

<!-- CSS variables -->
<div class="bg-[var(--primary-color)]"></div>

<!-- Custom grid -->
<div class="grid-cols-[200px_1fr_minmax(200px,_1fr)]"></div>

<!-- Spacing with custom value -->
<div class="mt-[7px] px-[18px]"></div>

When to Use Arbitrary Values

Arbitrary values are appropriate in specific situations:

  • Precise positioning: A tooltip that must align exactly with a reference point (top-[3px])
  • External constraints: Matching a third-party component's height or border radius
  • Unique brand colors: A brand color that does not fit the Tailwind palette well (bg-[#e63946])
  • Dynamic CSS expressions: calc, min, max, or clamp values

They are NOT appropriate for values that belong in the design system — use theme extension for those.

<!-- Appropriate: precise positioning calculation -->
<div class="absolute top-[calc(50%_-_24px)] left-[calc(50%_-_24px)]"
     aria-label="Loading">
  Loading spinner centered
</div>

<!-- Appropriate: brand color not in default palette -->
<header class="bg-[#e63946] text-white">
  Brand header
</header>

<!-- Inappropriate: use theme extension instead -->
<!-- <div class="w-[96px]"> Use w-24 (96px) from default scale instead! -->
<div class="w-24">Use the scale utility</div>

Arbitrary Values vs Theme Extension

When a value is used in multiple places across your project, it belongs in the theme extension, not as an arbitrary value. Adding it to the theme gives it a semantic name, makes it consistent, and allows you to change it in one place.

A good rule: if you use an arbitrary value twice in different files, promote it to the theme. If it appears only once in a highly specific context, the arbitrary value is fine.

// Instead of repeating w-[347px] in 5 components:
// ❌ Scattered arbitrary values
// <div class="w-[347px]">...</div>  (component A)
// <div class="w-[347px]">...</div>  (component B)
// <div class="w-[347px]">...</div>  (component C)

// ✅ Theme extension — one source of truth
// tailwind.config.js:
module.exports = {
  theme: {
    extend: {
      width: {
        'sidebar': '347px',
      },
    },
  },
};
// Usage:
// <div class="w-sidebar">...</div>  -- consistent and named

The Cost of Each Arbitrary Value

Each unique arbitrary value generates a unique CSS rule. Five uses of w-[347px] in the same file only generate one CSS rule (JIT deduplicates). But w-[347px], w-[348px], and w-[349px] in different components each generate separate rules — three separate rules for values that differ by a single pixel.

This is the cost of arbitrary values: they can defeat the CSS reuse that makes utility classes efficient. Ten slightly different arbitrary widths generate ten separate rules rather than one shared utility.

/* Each generates a separate CSS rule */
.w-\[347px\] { width: 347px; }
.w-\[348px\] { width: 348px; }
.w-\[349px\] { width: 349px; }
.top-\[calc\(50\%-24px\)\] { top: calc(50% - 24px); }
.top-\[calc\(50\%-32px\)\] { top: calc(50% - 32px); }

/* Compare: the scale utilities share values across all usages */
/* .w-24 is generated once and reused wherever w-24 appears */

Arbitrary Property Syntax

Beyond utility prefixes, Tailwind's JIT also supports arbitrary CSS properties using the syntax [property:value] — a square bracket containing both a CSS property name and a value separated by a colon.

This is the escape hatch for CSS properties that Tailwind does not have a utility for, like [mask-image:linear-gradient(...)], [scroll-snap-type:x_mandatory], or vendor-prefixed properties. Use it sparingly — if you find yourself using many arbitrary properties, consider adding a Tailwind plugin instead.

<!-- Arbitrary CSS property -->
<div class="[mask-image:linear-gradient(to_bottom,black,transparent)]
            [scroll-snap-type:y_mandatory]
            [-webkit-line-clamp:3]">
  Content with non-utility CSS properties
</div>

<!-- Also works with variants -->
<div class="hover:[text-decoration:underline_wavy_red]
            focus:[outline:2px_solid_blue]">
  Custom hover/focus styles via arbitrary property
</div>

Arbitrary Values With Variants

Arbitrary values compose fully with all variant prefixes. You can combine responsive, hover, focus, dark, and other variants with any arbitrary value — the same way standard utilities work.

This gives you the full power of Tailwind's variant system for custom values. A custom off-scale hover color, a responsive custom width, or a dark-mode arbitrary color value all work seamlessly.

<!-- Responsive arbitrary width -->
<div class="w-[240px] md:w-[320px] lg:w-[400px]">
  Width changes at each breakpoint
</div>

<!-- Dark mode arbitrary color -->
<div class="bg-[#f8f9fa] dark:bg-[#1a1a2e] text-[#212529] dark:text-[#f8f9fa]">
  Light/dark mode with arbitrary hex colors
</div>

<!-- Hover with arbitrary value -->
<button class="bg-[#e63946] hover:bg-[#c1121f] text-white px-4 py-2 rounded-lg">
  Custom brand button
</button>

Color Arbitrary Values and Opacity

When using arbitrary hex colors, you can also apply the opacity modifier with a slash: bg-[#e63946]/50 sets the background to 50% opacity. This works because the slash opacity syntax is a JIT feature that applies regardless of whether the base color is from the palette or an arbitrary value.

You can also use rgba() or hsl() functions directly inside the brackets for arbitrary opacity or color model control.

<!-- Arbitrary color with opacity modifier -->
<div class="bg-[#e63946]/50">50% opacity red</div>
<div class="bg-[#e63946]/75">75% opacity red</div>

<!-- RGB function -->
<div class="bg-[rgb(230,57,70)]">RGB red</div>

<!-- HSL function -->
<div class="bg-[hsl(353,76%,55%)]">HSL red</div>

<!-- RGBA for direct alpha control -->
<div class="bg-[rgba(230,57,70,0.5)]">RGBA with alpha</div>

Escape Characters in Arbitrary Values

Some characters inside arbitrary values need to be escaped because they have meaning in CSS or HTML. In Tailwind arbitrary values, use underscores for spaces (Tailwind automatically converts them) and escape parentheses and other special characters within calc() by using underscores for spaces inside the function.

For URL values (like background images), wrap the value in single quotes: bg-[url('/img/hero.png')]. Tailwind preserves the single quotes in the generated CSS.

<!-- Underscore = space in arbitrary values -->
<div class="grid grid-cols-[200px_1fr] gap-[10px_20px]">
  Column gap 10px, row gap 20px
</div>

<!-- calc with underscore-spaces -->
<div class="w-[calc(100%_-_64px)]">
  Full width minus 64px
</div>

<!-- URL as background image -->
<div class="bg-[url('/img/hero.jpg')] bg-cover bg-center h-64">
  Hero image
</div>

Audit Your Arbitrary Values

Periodically search your codebase for all arbitrary value usages to identify candidates for theme extraction. A quick grep identifies every bracket class in your project:

Any value appearing more than twice in different files is a strong candidate for a named theme token. Any value appearing once is fine as an arbitrary value. Values that look like they belong to a consistent scale (like w-[350px], w-[360px], w-[370px]) signal that an off-scale usage pattern needs to be standardized.

# Find all arbitrary value usages in your project
grep -roh 'class="[^"]*\[[^\]]*\][^"]*"' src/ | sort | uniq -c | sort -rn

# Or more targeted: find classes with bracket notation
grep -roh '[a-z:-]*\[[^\]]*\]' src/ --include='*.{html,js,jsx,ts,tsx}' | sort | uniq -c | sort -rn

# Review high-count arbitrary values — add to theme if used 3+ times

Stacking Arbitrary Values With Utilities

Arbitrary values compose naturally with standard utility classes in the same class string. You can mix scale utilities and arbitrary values freely — this is intentional, not a workaround. The design system handles 95% of cases with scale utilities; arbitrary values fill the remaining 5%.

The key discipline is using scale utilities first and reaching for arbitrary values only when the scale genuinely does not have what you need. Enforce this discipline in code review to keep your markup from becoming a collection of off-scale arbitrary values.

<!-- Mixing utilities and arbitrary values naturally -->
<div class="flex items-center gap-4 px-6 py-3    <!-- scale utilities -->
            w-[480px]                              <!-- arbitrary: specific width -->
            bg-[#1a1a2e]                          <!-- arbitrary: brand color -->
            rounded-xl shadow-lg                  <!-- scale utilities again -->
            text-sm font-medium text-white">
  A card using both
</div>

Quick Check

Test your understanding of Tailwind CSS Mastery concepts from this lesson.

Lesson Recap

In this lesson you learned: arbitrary values use [value] bracket syntax to set any CSS value on any utility, powered by JIT's on-demand generation; each unique arbitrary value generates a unique CSS rule, so repeated off-scale values across files should be promoted to the theme; and underscores represent spaces inside arbitrary values (e.g., calc(100%_-_64px)). This concludes the JIT and Production Optimization course — next we explore design tokens and systematic theming.

免费开始

用 AI 导师学习 HTML — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
30
课程
120

常见问题解答

「任意值及其代价」课时是免费的吗?

是的 — 「任意值及其代价」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Tailwind CSS Academy 课程的其余内容,请升级到 CoddyKit PRO。 Tailwind CSS Academy 课程共包含 4 节课。

「任意值及其代价」这节课中我会学到什么?

使用 w-[347px] 这样的方括号表示法处理一次性值,并了解灵活性与样式表可能变得臃肿之间的权衡。 你通过在浏览器中直接运行的动手代码来练习 Tailwind CSS Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Tailwind CSS Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Tailwind CSS Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「任意值及其代价」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Tailwind CSS Academy 课中编写并运行代码吗?

能。每节 Tailwind CSS Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. JIT 引擎的工作原理
  2. 动态类名安全列表
  3. 分析并缩减捆绑包大小
  4. 任意值及其代价
← 返回 Tailwind CSS Academy