Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Components and Reuse

Arbitrary Values and Resolving Conflicts

Use Tailwind CSS square-bracket arbitrary values and properties, CSS variable shorthands, the important modifier and prefix, and avoid dynamic class name mistakes.

The default scale covers most designs, but real designs contain odd numbers: a hero that is exactly 37rem wide, a brand color from a style guide, a grid with a strange column template. Tailwind lets you step outside the scale for a single class using square brackets, without leaving the utility workflow. This lesson shows the syntax, the related tools (!, prefixes, source detection), and how to stay out of trouble with conflicting classes and dynamic class names.

Arbitrary values

Put any valid CSS value in square brackets after the utility name:

HTML
<div class="top-[117px] w-[37rem] bg-[#0ea5e9] text-[1.15rem] grid-cols-[1fr_2fr_1fr]"></div>

Rules to remember:

  • Spaces become underscores. grid-cols-[1fr_2fr_1fr] means 1fr 2fr 1fr. To write a real underscore, escape it: content-['snake\_case'].
  • Math works: h-[calc(100vh-4rem)]. Inside calc() Tailwind adds the spaces around operators for you.
  • When a utility accepts several types (text- is both font size and color), give a type hint: text-[length:var(--size)] or text-[color:var(--c)]. Often Tailwind infers it correctly.
  • Opacity modifiers also work: bg-[#0ea5e9]/50.
Arbitrary values in action

Arbitrary properties and variants

If Tailwind has no utility for a CSS property, use an arbitrary property: the whole declaration in brackets.

HTML
<div class="[mask-type:luminance] [--my-gap:2rem] [scroll-snap-type:x_mandatory]"></div>

Arbitrary properties accept variants like any other class: hover:[text-wrap:balance], md:[--my-gap:4rem].

Arbitrary variants write a selector or at-rule in brackets, with & as the element:

HTML
<li class="[&:nth-child(3)]:underline [&_p]:mt-2 [&>svg]:size-4 [@media(min-aspect-ratio:16/9)]:hidden"></li>

[&_p]:mt-2 styles descendant paragraphs, [&>svg]:size-4 styles direct SVG children. These are the escape hatches for when you do not own the inner markup.

Arbitrary properties and selector variants

CSS variables in utilities

When the value comes from a CSS variable, use parentheses instead of brackets. Tailwind v4 wraps it in var() for you:

HTML
<div class="bg-(--panel-bg) w-(--panel-width) text-(length:--panel-text)"></div>

bg-(--panel-bg) is the same as bg-[var(--panel-bg)], with less noise. This is the usual way to pass dynamic values from JavaScript or inline style into a utility:

A variable set by style, read by a utility

This is the right answer to "how do I use a value from my database in a class?" Do not try to generate a class name from the value. Put the value into a variable and let one fixed class read it.

Dynamic class names do not work

Tailwind builds its CSS by scanning your source files for complete class names as text. It never runs your code. So this fails in a build:

JavaScript
// Wrong: the full names never appear in the source
const cls = `bg-${color}-500`;   // 'bg-red-500' is never written out

Fixes, in order of preference:

  1. Map to complete names ({ red: 'bg-red-500', blue: 'bg-blue-500' }), as in Reusing Styles with Components.
  2. Pass a value through a CSS variable and an arbitrary utility, as above.
  3. Safelist classes that really are generated at runtime: @source inline("bg-red-500 bg-blue-500 md:bg-{red,blue}-500");

The browser script used in the live examples on this site watches the DOM and generates CSS for classes at runtime, so concatenated names would appear to work there. A production build does not do that. Always write full class names, as if you were using a build.

Conflicting utilities

If one element gets two utilities that set the same property, such as p-2 p-4, the result depends on the order in the generated stylesheet, not on the order in your HTML. For different property values this is usually the larger or later utility, but you should not depend on it. Conflicts happen mostly when a component takes an extra class from outside and adds it to its own. Solutions:

  • Do not put conflicting classes on the same element; remove the base one.
  • In JS frameworks, use tailwind-merge: twMerge('px-2 py-1 p-4') returns 'p-4', removing the classes the last one overrides.
  • Use a variant or the important modifier when you must override from the outside.

The important modifier

Add ! at the end of a utility to mark it !important:

HTML
<p class="text-sm! text-red-500">Forced small</p>

The generated rule has !important, so it wins against anything, including inline-style-free stylesheets from third-party code. It works with variants too: md:p-8!. In Tailwind v3 the ! went at the start (!text-sm); v4 accepts the old position for compatibility, but the end position is preferred.

Reach for ! only when you fight CSS you cannot change, because it makes later overrides harder.

The prefix option

If Tailwind has to live on a page next to other CSS with colliding names (a legacy Bootstrap site, say), you can give every class a prefix when importing:

CSS
@import "tailwindcss" prefix(tw);

Now every class is written with the prefix first: tw:flex tw:md:bg-white tw:hover:underline. Theme variables get the prefix too (--tw-color-red-500).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you write grid-template-columns: 1fr 2fr 1fr as an arbitrary value?

    Spaces cannot appear in a class name, so underscores stand for spaces inside brackets.
  2. 2. What does bg-(--panel-bg) do?

    Parentheses are shorthand for var(...) in utilities.
  3. 3. Why does bg-${color}-500 fail in a production build?

    The full class name never appears in the source, so no CSS is generated for it.
  4. 4. Where does the important modifier go in Tailwind v4?

    v4 puts the exclamation mark after the utility; the leading form still works for compatibility.
  5. 5. What is the safest way to apply a width that comes from runtime data?

    One fixed class reading a variable gives the build a complete name to find.

Summary

  • Square brackets give you any value: w-[37rem], bg-[#0ea5e9], grid-cols-[1fr_2fr]; underscores are spaces; parentheses read CSS variables.
  • Arbitrary properties ([mask-type:luminance]) and variants ([&_p]:mt-2) cover whatever has no utility.
  • Never assemble class names from fragments; use complete names, variables, or @source inline().
  • Resolve conflicts by removing one of the classes, tailwind-merge, or the ! modifier as a last resort; prefix(tw) avoids name collisions.

Next: Keyframe Animations in Tailwind CSS