DEV Community

Md Hehedi Hasan
Md Hehedi Hasan

Posted on

How to add ANY prefix in React + Vite + Tailwind CSS

Here is a clean Markdown file you can save as
ADD_PREFIX_TAILWIND_V4.md
or anything you want.


📘 How to Add Any Prefix in React + Vite + Tailwind CSS v4

In Tailwind CSS v4, prefixes are no longer added using tailwind.config.js.
Instead, you add the prefix directly in your CSS @import rule.


✅ 1. Open Your Main CSS File

Examples:

src/index.css
Enter fullscreen mode Exit fullscreen mode

or:

src/App.css
Enter fullscreen mode Exit fullscreen mode

✅ 2. Import Tailwind With a Custom Prefix

You can use any prefix such as:

  • tw
  • my
  • ui
  • x
  • custom
  • anything you want

Example:

@import "tailwindcss" prefix(tw);
Enter fullscreen mode Exit fullscreen mode

Or:

@import "tailwindcss" prefix(my);
Enter fullscreen mode Exit fullscreen mode

Or:

@import "tailwindcss" prefix(custom);
Enter fullscreen mode Exit fullscreen mode

✅ 3. Use Prefixed Classes in React

Example (prefix: tw):

<h1 className="tw-text-3xl tw-font-bold tw-text-red-500">
  Hello Tailwind
</h1>
Enter fullscreen mode Exit fullscreen mode

Example (prefix: my):

<h1 className="my-text-xl my-font-semibold">
  Custom Prefix Working
</h1>
Enter fullscreen mode Exit fullscreen mode

🎯 Variants (hover, responsive, dark mode)

Tailwind v4 uses this format:

prefix + variant + ":" + utility
Enter fullscreen mode Exit fullscreen mode

✔ Correct:

tw-hover:text-red-500
tw-md:text-xl
tw-dark:bg-black
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect:

tw:hover:text-red-500
tw:text-red-500
Enter fullscreen mode Exit fullscreen mode

✅ Example Button (prefix: my)

<button className="
  my-bg-blue-500
  my-text-white
  my-rounded
  my-hover:bg-blue-600
">
  Click Me
</button>
Enter fullscreen mode Exit fullscreen mode

📌 Summary

Feature Tailwind v4 Behavior
Add prefix @import "tailwindcss" prefix(tw);
Any prefix allowed Yes
Utility format tw-text-red-500
Variants tw-hover:bg-black
Wrong format tw:bg-black

Top comments (0)