0

Is it possible to display color boxes before any TailwindCSS class name related to colors, regardless of where I write the TailwindCSS class name?

I've noticed that this works perfectly in CSS files or within the HTML class or className attributes (e.g. border-error-400 in second photo).

I would assume this should work across the entire code, but I was disappointed to find that, as shown in the attached screenshot, the color indicator box doesn't appear when a TailwindCSS color class is written as the value of a JS object.

It doesn't work anywhere Working in className="..." attribute
not working here working here
3
  • 1
    Currently, you don't even have a hint yet. The solution for the hint will definitely be in the regex, see here: tailwindCSS.experimental.classRegex --- I'm not sure if the Tailwind Intellisense automatically places the color box in the correct spot with the regex, so it's just a guess. Commented Apr 7, 2025 at 15:04
  • 1
    Note: I slightly rephrased your question. At first, I didn't fully understand what you meant, since you provided a screenshot and a brief description. The code snippet in the screenshot was a bit too much for me to see both the working and non-working examples at once. If you disagree with the changes, feel free to revert them here: stackoverflow.com/posts/79560191/revisions Commented Apr 7, 2025 at 15:15
  • But it's worth going through the recommended troubleshooting steps, as certain special VSCode settings can cause these issues: stackoverflow.com/a/79808734/15167500 Commented Nov 4, 2025 at 9:31

1 Answer 1

1

TLDR: Although the first solutions specifically respond to the example in the question, I suggest you consider a much more consistent approach: creating a function to which you always pass only TailwindCSS classes. Then, inform IntelliSense that the function named xy will accept only TailwindCSS classes, so it should handle it accordingly. See more: Solution #3.

Well, I checked my suggestion, and yes; TailwindCSS IntelliSense fully applies all of its features based on the correct regex patterns.

Note: A prerequisite for the proper validation of my answer is the installation of TailwindCSS IntelliSense.



Solution #1 for plain JavaScript objects

This gives you the correct answer, although I don't think it's the best way. With regex, we can declare the syntax of JS objects so that it attempts to search for TailwindCSS class names in all strings placed as values, and suggest TailwindCSS class names as hints.

TailwindCSS IntelliSense with this setting:

{
  "tailwindCSS.experimental.classRegex": [
    ":\\s*?[\"'`]([^\"'`]*).*?,",
  ],
}

Note: For open settings press CtrlP and type Preferences: Open User Settings (JSON).

Important: The downside, is that it will be valid for every object. I think this might be a waste of energy and an unnecessary overcomplication of the process.

example



Solution #2 for plain JavaScript variables

Similarly to objects, regex can also be written for variables.

TailwindCSS IntelliSense with this setting:

{
  "tailwindCSS.experimental.classRegex": [
    "(?:const|let|var)\\s+[\\w$_][_\\w\\d]*\\s*=\\s*['\\\"](.*?)['\\\"]",
  ],
}

Note: For open settings press CtrlP and type Preferences: Open User Settings (JSON).

Important: The repeated downside, once again, is that it will be valid for every string. I think this might be a waste of energy and an unnecessary overcomplication of the process.

example



Solution #3 with a special function (recommended)

The advantage of the function is that wherever you want to use a TailwindCSS class, you just need to call it, and it will work immediately, whereas the previous solution will only be available within JS objects.

Instead of relying on regex, I recommend creating a "fake" function that accepts a string as a parameter and simply returns that parameter as the result. This way, you can explicitly tell TailwindCSS IntelliSense that the parameters passed to this function definitely contain TailwindCSS class names, so it will work correctly and consistently anywhere later on.

function tw (input: TemplateStringsArray | string, ...interpolations: any[]): string {
  if (typeof input === "string") {
    return input;
  }

  let result = input[0];
  for (let i = 0; i < interpolations.length; i++) {
    result += interpolations[i] + input[i + 1];
  }
  return result;
}

And the parameters of the tw function will be correctly handled by TailwindCSS IntelliSense with this setting:

{
  "tailwindCSS.classFunctions": [
    "tw",
  ],
}

Note: For open settings press CtrlP and type Preferences: Open User Settings (JSON).

Note: The tw function doesn't need to be overcomplicated. I simply allowed it to accept the three basic types of string inputs. All three invocation styles return a single concatenated string. For example, in example_4, the result would be "bg-red-500 text-green-400".

example

Sign up to request clarification or add additional context in comments.

You can encounter many useful pre-written regex patterns here: github.com/paolotiu/tailwind-intellisense-regex-list

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.