Dynamic color interpolation via JIT color-mix()
(e.g. bg-red-250
)
#17673
Replies: 3 comments 7 replies
-
Much needed! |
Beta Was this translation helpful? Give feedback.
-
I think this could be a neat addition but I'm not sure how to add it without suddenly being very prescriptive about color names, or assuming that just because someone has I'm also extremely allergic to configuration so I don't want to make this some sort of option if we did add it. How would this work if for example someone loaded in a Radix color scheme where the naming is like |
Beta Was this translation helpful? Give feedback.
-
Here's a fun demo just showing how this would look, the interpolated colors turn out pretty nice as long as you've got enough of your own predefined color steps: |
Beta Was this translation helpful? Give feedback.
-
Summary
Introduce support for JIT generation of intermediate color shades, shorthand syntax to be able to
color-mix
shades of the same color (e.g..bg-red-250
), by leveraging the native CSScolor-mix()
function.For example, if someone wanted to use shades in-between
red-200
andred-300
that would be equivalent tored-225
,red-250
,red-275
, they would need to manually add that shade to their theme (and manually include the entire color's shades to not overwrite the entire color), add custom classes to their CSS for each, or use a complex arbitrary value inline usingcolor-mix()
to achieve those values (Tailwind Play):Tailwind CSS's compiler could introduce this same convention as a JIT-only shorthand for ease of use, so the same result could be achieved like this:
Implementation note⚠️
This would be purely a JIT feature. It would not pre-generate CSS variables for every possible numeric value but rather create the specific
color-mix()
rule only when a corresponding class is used in the HTML/template files. As a result, these values would also not be suggested using Intellisense, which is a similar. trade-off for other similar features in Tailwind CSS v4 that support arbitrary values.These intermediate colors would be supported, but not as official values, just as a
z-67
z-index value would not be an "official value". Users—if they really wanted to—could use classnames likebg-red-462
though that would not generally be considered helpful. In most cases, users might reach for this for-25
,-50
, and-75
values.Motivation
Tailwind's default color palette with stops from 50 to 950 provides a great baseline. However, in some cases, designers and developers need slightly adjusted shades that fall between these predefined stops. Currently, achieving this requires either:
bg-[#...]
, which can be verbose for simple shade adjustments.With Tailwind v4 making square brackets optional for simple values (e.g.
z-67
→z-index: 67
(Tailwind Play)), there's an opportunity for a similarly more intuitive way to access in-between shades of colors.Proposal
Color stops between built-in shades
Introduce the ability for Tailwind's JIT engine to recognize patterns like
bg-{colorName}-{numericValue}
(e.g.,bg-red-250
,text-blue-515
,border-emerald-880
) where the numeric value is not one of the standard stops (50, 100-900, 950).When such a class is detected, Tailwind would generate the necessary CSS on-the-fly using the native
color-mix()
function to interpolate between the two nearest standard color stops via the built-in CSS variables (e.g., --color-red-200) in the user's theme.For example,
bg-red-250
could generate CSS similar to this:Color stops between built-in start/end color-stops and white/black, respectively
Shades between 0-50 and 950-1000 could also be achieved using this same method, calculating the distance between white/black and the respective nearest shade.
For example:
(Note: The exact
color-mix()
parameters, like color space (OKLCH recommended for perceptual uniformity) and hue interpolation method, are open for discussion, butoklch shorter hue
seems like a strong default).Benefits
-{number}
pattern, consistent with opacity and arbitrary value scales.color-mix()
for efficient browser rendering; avoids adding hundreds of predefined CSS variables.Discussion Points
bg-red-250
)?color-mix()
parameters (color space, hue interpolation)?Beta Was this translation helpful? Give feedback.
All reactions