Sub topic 9
Arbitrary values and theme customization
This chapter assumes you know Tailwind utility classes, state variants such as hover:, and the basic transition-* utilities. It covers what to do when the built-in duration and easing scales don't contain the value you need.
When the defaults are not enough
Tailwind ships with a small set of durations (duration-75 up to duration-1000), delays, and easing curves (ease-linear, ease-in, ease-out, ease-in-out). Most interfaces need only these. When a design asks for 250 ms or a specific cubic-bezier curve, you have two options:
- An arbitrary value, written inline in square brackets. It applies to one place.
- A theme customization, defined once. It becomes a named utility for the whole project.
Arbitrary values
Put the value in square brackets after the utility name.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors duration-[250ms] delay-[40ms] ease-[cubic-bezier(0.22,1,0.36,1)] hover:bg-indigo-700">
Save
</button>Here duration-[250ms] sets transition-duration: 250ms. delay-[40ms] sets the delay. ease-[cubic-bezier(0.22,1,0.36,1)] sets the timing function. Write the unit, because duration-[250] is not valid.
You can also name the property list yourself with transition-[...]. Use underscores in place of spaces, since class names cannot contain spaces.
<div class="transition-[background-color,box-shadow] duration-200 hover:bg-slate-100 hover:shadow-md">
Panel
</div>This transitions only the background color and box shadow. That is cheaper and more predictable than the default property set.
Theme customization in Tailwind v4
Arbitrary values are fine once or twice. If you write the same curve in ten places, define it in the theme. In v4 you do this with CSS variables inside @theme.
@import "tailwindcss";
@theme {
--ease-snappy: cubic-bezier(0.22, 1, 0.36, 1);
--animate-wiggle: wiggle 0.4s ease-in-out 2;
@keyframes wiggle {
0%, 100% { transform: rotate(-3deg); }
50% { transform: rotate(3deg); }
}
}The variable name decides the utility name. --ease-snappy creates ease-snappy. --animate-wiggle creates animate-wiggle. Your markup becomes shorter and the value lives in one file.
<button class="transition-colors duration-200 ease-snappy hover:bg-indigo-700">Save</button>Theme customization in Tailwind v3
In v3 the same work goes in tailwind.config.js, under theme.extend. Using extend adds to the defaults. Putting keys directly under theme replaces the whole default scale.
// tailwind.config.js (Tailwind v3)
module.exports = {
theme: {
extend: {
transitionTimingFunction: {
snappy: 'cubic-bezier(0.22, 1, 0.36, 1)',
},
transitionDuration: {
250: '250ms',
},
},
},
};This gives you ease-snappy and duration-250.
Choosing between the two
| Situation | Use |
|---|---|
| One-off value in a single element | Arbitrary value |
| Value repeated across components | Theme entry |
| Value tied to a design decision, such as brand motion | Theme entry |
| Custom keyframes | Theme entry |
Common errors
- Missing unit.
duration-[250]produces nothing. Writeduration-[250ms]. - Spaces inside brackets. Use underscores, for example
transition-[opacity,transform]. A space ends the class name. - Dynamic class names. Tailwind scans your source for complete strings.
duration-[${ms}ms]built at runtime is never generated. Write out the full class, or use a style attribute for truly dynamic values. - Wrong variable prefix in v4. Only names such as
--ease-*,--animate-*, and--default-transition-durationare mapped to utilities. A variable called--snappycreates nothing. - Mixing up v3 and v4 syntax. A
@themeblock in a v3 project does nothing, and a v3 config file is not read by default in v4.
Exercise
You want a card to transition only its transform and opacity over 350 ms, and you use this exact timing in many components. Which two approaches would you combine, and in v4, what would you write for the duration?
Show answer
Use transition-[transform,opacity] for the property list, and define the duration in the theme so it can be reused. In v4 you would add a variable such as --default-transition-duration for a global default, or use duration-[350ms] for a one-off. For a repeated named value, define a theme entry and apply it to each card.