Using Javascript Variables in Tailwind

Tailwind is a CSS utility framework that provides shorthand CSS classes for rapidly composing designs. Instead of writing CSS rules, you simply add utility classes to your element. This blog post will be the first in a series about using Tailwind at enterprise scale and the tips and tricks I've developed while building a component library at Kizen and migrating to Tailwind from a css-in-js approach.

Before we get started, I want to acknowledge that Tailwind can be a bit polarizing within the web development community. I've built many sites using Tailwind, and many without, but this post isn't about whether or not you should use Tailwind - that's for you to decide!

With that out of the way, let's get into it! I'll start with a quick refresher on Tailwind. Take this simple element and some styling:

  .box {
    width: 3.5rem;
    height: 3.5rem;
    background-color: rgb(239, 68, 68);

<div class="box"></div>

In Tailwind, using the shorthand utility classes, the same element can be written as:

<div class="w-14 h-14 bg-red-500"></div>

For reference, here's how that element looks using either approach:

My examples from this point on will use React, but the same principles apply to any framework.

A Quick Look at How Tailwind Works

Tailwind provides many utility classes that can be used to style elements, mirroring almost all the existing CSS properties. You wouldn't want to include all of these classes in your app if they aren't used, though, so the Tailwind CLI parses your code and generates a CSS bundle of only the utility classes you're using.

In my first example, I used the token red-500 for the color. This yields some generated CSS:

.bg-red-500 {
  --tw-bg-opacity: 1;
  background-color: rgb(239 68 68 / var(--tw-bg-opacity));

This particular class was only included in the bundle because it was used in the code. For example, bg-red-400 will not be generated or bundled unless it's added somewhere in the application.

That's great if we're using a predefined color pallete, but that's not always the case. Tailwind also allows for arbitrary values, using a special notation (note the square brackets, and lack of spaces inside the rgb color value):

<div className="bg-[rgb(239,68,68)]" />

Like before, the Tailwind CLI will parse this out and generate a bundle with the correct class. In this case, it looks like this:

.bg-\[rgb\(239\2c 68\2c 68\)\] {
  --tw-bg-opacity: 1;
  background-color: rgb(239 68 68 / var(--tw-bg-opacity));

This looks great, and works because the className is static and can be easily parsed. However, this approach starts to break down if you're using content from another system or storage medium, like user-defined colors.

Let's look another contrived example where this exact issue comes to light. In this one, we'll have a user-defined color stored in a variable, and we want to use that as the background:

const color = "rgb(239, 68, 68)";

Can we simply build the className dynamically, like this?

<div className={`bg-${color}`} />

❌ This approach won't work, because the Tailwind CLI can't statically determine what that class will be, so it doesn't get included in the bundle. This will have no effect on our UI, and the color won't be applied.

Common Solution

One of the most common solutions I've seen is to use the style attribute instead of the className attribute:

  className="w-14 h-14"
    backgroundColor: color,

⚠️ This does work, but I don't particularly like this approach. Not only are we breaking out of Tailwind's utility classes, reducing maintainability and increasing surface area for bugs, but we're also introducing inline styles, which can be very difficult to override because they take the highest precendence.

There has to be a better way to pass arbitrary values to Tailwind, that will allow us to keep all our styling limited to a single approach.

Using The Style Prop the Right Way

We're on the right track here, because the solution does involve inline styles, but in a different way that avoids all the problems I listed above. The solution is to use the style attribute to set a CSS variable, that we can then access from Tailwind.

  className="w-14 h-14 bg-[var(--user-color)]"
    "--user-color": color,

Here, I'm using the same square-brace notation as earlier, but instead of an rgb color, I have the CSS variable referenced.

Let's look at the generated CSS to see why this works:

.bg-\[var\(--user-color\)\] {
  background-color: var(--user-color);

✅ We can statically analyze and parse the className, so no issues there! And, we're using a CSS variable, rather than passing rules as inline styles, so we don't end up with split strategies for our styling, or precendence issues.

The inline style is only used to set the CSS variable, which is then used in the Tailwind class! Since the generated CSS doesn't need to know the value of the variable, it doesn't matter that it's dynamic.


This pattern isn't limited to just colors! This can be used for any CSS property that has a Tailwind utility class. I've used this approach in conjunction with calculations for padding, margin, and even font sizes! If you want to learn more about arbitrary styles in Tailwind, check out the Tailwind docs.

This blog post is the first in a series I'll be writing about using Tailwind in enterprise-scale applications, and some of the problems we've had to solve at Kizen as we migrate from a CSS-in-js approach to Tailwind.

Stay tuned for more under the css and Tailwind tags on my blog, and if you have any topics about Tailwind at scale you'd like covered, shoot me an email at tailwind@k10y.com!