Back in December 2024, I was working on an AI writing assistant for the editor in our dashboard. The idea was pretty simple. You write a short brief, select a tone, and the AI writes a draft for you.
But there was one problem.
Our existing editor was built using JSP, rendered on the server, with years of hand-written legacy CSS behind it. And obviously, we weren’t going to rewrite the entire editor just to introduce one new feature. So I built the AI writing assistant using React, Tailwind CSS, and shadcn/ui, and mounted it directly inside the existing JSP page. This was a few months after we’d started shipping new features in React this way. If you’re wondering how React gets onto a JSP page at all, that’s the first post.
Sounds straightforward, right?
Well, mounting it was the easy part. We’d already solved that. Getting the CSS to behave was a completely different story.
The Problem: Two Stylesheets Fighting Each Other
Since we were mounting React directly inside the JSP page, both were sharing the same document. We weren’t using an iframe or Shadow DOM to isolate anything. Which meant the CSS from our React components could affect the existing JSP page, and the legacy CSS could affect our React components.
Styles
From Tailwind Preflight → legacy page
- 1
input { padding: 0 }inputs collapse - 2
a { color: inherit }link turns grey - 3
ul { list-style: none }bullets vanish
From the legacy CSS → React
- 4
.editor label { color: #a8a29e }React’s labels fade and shrink
And that’s exactly what happened. For example, our legacy stylesheet had rules like this:
/* legacy-base.css */
button,
input[type="submit"] {
text-transform: uppercase;
}
Now, this rule was written long before we introduced React. But CSS doesn’t care about that.
If there’s a <button> anywhere on the page, including inside our React components, this rule can affect it.
We’d also had this in our Tailwind config from early on:
important: true, // every Tailwind utility becomes !important
This adds !important to every Tailwind utility, so our utilities beat any legacy rule fighting over the same property. For a while, it seemed like the only thing keeping our React components from breaking: whenever I tried removing it, the legacy styles started affecting them again.
What I didn’t realize yet was that it was also hiding the real problem.
Not ideal.
First Attempt: Prefixing Tailwind Classes
The first issue was pretty obvious. Tailwind generates global utility classes. For example, when you use hidden, Tailwind generates something like:
.hidden {
display: none;
}
But our legacy JSP pages were already using class="hidden" for their own elements. So now we had two different stylesheets using the same class names.
The fix for this was simple enough. Tailwind supports adding a prefix to all its utility classes. So I added this to our Tailwind config:
// tailwind.config.js
prefix: "tw-",
Now, instead of writing:
<div className="hidden md:flex p-4 border">
We’d write:
<div className="tw-hidden md:tw-flex tw-p-4 tw-border">
This way, every Tailwind utility carries the tw- prefix, while the existing JSP classes would remain unchanged. Variants still go in front, so it’s md:tw-flex, not tw-md:flex. And since none of our legacy classes used that prefix, we no longer had to worry about those class-name collisions.
So, that fixed the class-name collisions.
But our inputs were still broken.
The Actual Problem: Tailwind Preflight
This took me a little longer to figure out. Tailwind includes something called Preflight, which is essentially a set of base CSS styles that reset the browser’s default styling. You can read more about it in the Tailwind Preflight documentation.
For example, Preflight includes rules like these:
/* Tailwind Preflight (excerpt) */
button,
input,
select,
textarea {
font-size: 100%;
font-weight: inherit;
padding: 0;
}
a {
color: inherit;
}
ol,
ul {
list-style: none;
margin: 0;
padding: 0;
}
Notice something?
These aren’t Tailwind utility classes. They’re targeting HTML elements directly. Which means adding tw- to our utility classes doesn’t help here.
Now, our legacy CSS also had rules targeting the same HTML elements. For example:
/* legacy-forms.css */
input {
padding: 8px 0;
font-size: 18px;
font-weight: 500;
}
So we had Tailwind Preflight setting the input padding to 0, while our legacy CSS was setting it to 8px 0. Both input selectors have the same CSS specificity.
In our case, the React stylesheet was loading after the legacy stylesheet. So Tailwind Preflight was overriding the existing styles for inputs, links, lists, and other HTML elements across the editor page. That’s why things were breaking even after we added the prefix.
You can see the tie for yourself. Here’s the editor with Preflight loaded globally, with DevTools open on the Title input. Click around the page: the link and the list lose the same way.
Why Our React Wrapper Didn’t Help
We already had a wrapper around every React mount point. Something like this:
<div id="ai-assistant" class="react-root"></div>
Initially, I thought having all the React components inside .react-root would be enough to isolate the styling. But that’s not how CSS works.
Just because your React components are rendered inside a particular div doesn’t mean the styles imported by React are restricted to that div. Preflight’s selectors are still plain input, a, and ul, and those match elements anywhere in the document. So the wrapper alone wasn’t doing anything to prevent the CSS from leaking into the JSP page.
At this point, I tried another approach. What if we simply disabled Tailwind Preflight? Tailwind lets you do that:
// tailwind.config.js
corePlugins: {
preflight: false,
},
And it worked!
Well, sort of.
The legacy JSP page went back to normal because Tailwind was no longer resetting its HTML elements. But now our React components started breaking.
The problem was that shadcn/ui expects the base styles provided by Tailwind Preflight. Without those styles, the components weren’t behaving as expected. For example, our legacy uppercase rule was now affecting the select buttons inside React. And some of our textareas were overflowing their containers because they no longer had the expected box-sizing: border-box reset.
Here’s the same page with Preflight off. The legacy side is fine again, but click the Tone select or the Brief textarea inside React:
So we had two options, and both were bad.
- Enable Preflight, and the legacy JSP page breaks.
- Disable Preflight, and our React components break.
What we actually needed was a way to keep Preflight enabled, but make sure it only applied inside .react-root.
The Fix: Scoped Tailwind Preflight
After looking into ways to isolate Tailwind’s reset styles, I came across this package:
And this was exactly what we needed. The package lets you scope Tailwind Preflight to a specific container instead of applying it globally. So I added it to our Tailwind configuration:
Roman86/tailwindcss-scoped-preflight// tailwind.config.js
import {
scopedPreflightStyles,
isolateInsideOfContainer,
} from "tailwindcss-scoped-preflight";
plugins: [
scopedPreflightStyles({
isolationStrategy: isolateInsideOfContainer(".react-root"),
}),
],
Now, instead of generating Preflight rules that affect every element on the page, it generates rules scoped to .react-root. For example, before scoping, the generated CSS looked something like this:
/* before: every element on the page */
*,
::before,
::after {
box-sizing: border-box;
border-width: 0;
}
After scoping, it looked like this:
/* after: only inside .react-root */
*:where(.react-root, .react-root *) {
box-sizing: border-box;
border-width: 0;
}
See the difference?
Instead of applying the reset globally, it now targets elements inside .react-root. Which means our legacy JSP elements can continue using their existing styles, while our React components get the Tailwind reset they need.
Same page, now scoped. Click the Title input, then the Tone select, and compare them with the two broken versions above:
So the legacy inputs keep their original 18px font size and padding, and our shadcn components inside React get the base styles they expect. And finally, both sides of the page were working correctly.
One Interesting Detail About :where()
You might have noticed that the generated CSS uses :where(). This is actually pretty useful.
In CSS, :where() has zero specificity. So even though we’re scoping the reset to .react-root, we’re not unnecessarily increasing the specificity of those base styles. This is important because Preflight is supposed to provide a foundation, not override every other style.
And since we still have important: true enabled, our Tailwind utility classes can continue taking priority over the legacy selectors inside React.
The Final Configuration
After all this, our Tailwind configuration came down to three important things:
// tailwind.config.js
prefix: "tw-", // Tailwind classes can't clash with legacy classes
important: true, // Tailwind utilities win over legacy selectors
plugins: [
scopedPreflightStyles({ // the reset only applies inside React
isolationStrategy: isolateInsideOfContainer(".react-root"),
}),
],
Here’s what each one solves:
1. prefix: "tw-"
Prevents Tailwind utility class names from colliding with the existing legacy CSS classes.
2. important: true
Makes Tailwind utilities use !important, helping them take priority over legacy CSS rules that might otherwise override our React components.
3. scopedPreflightStyles()
Keeps Tailwind Preflight enabled, but restricts its reset styles to elements inside .react-root, instead of applying them to the entire JSP page.
Notice that the last two work in opposite directions:
- Scoped Preflight stops Tailwind leaking out into the JSP page.
important: truestops legacy CSS leaking in, but only for properties you set with a Tailwind utility. A more specific legacy rule, like.editor label { … }, still reaches inside.react-rootfor anything you didn’t set.
Together, these three changes solved the CSS conflicts we were running into.
A Few Things I Learned
Looking back, there are a few things I wish I’d known before starting.
Add a Tailwind prefix from the beginning.
If you’re introducing React and Tailwind into an existing application with a lot of legacy CSS, don’t wait until class-name collisions start happening. Add the prefix before building your components. Otherwise, you’ll eventually have to go back and update all those class names.
Don’t blindly disable Preflight when using shadcn/ui.
It might fix your legacy page, but it can introduce a completely different set of problems inside React. If you need Preflight, scope it rather than disabling it entirely.
Most important lesson
Prefixing Tailwind classes doesn’t mean your CSS is isolated.
Even with a prefix, global element selectors like
input,button, andacan still affect parts of your application that you never intended to touch. You need to think about both utility classes and base styles.
Wrapping Up
What started as a pretty simple task, embedding a React-based AI writing assistant into our existing JSP editor, turned into a much bigger CSS debugging problem than I expected.
But in the end, we didn’t need to rewrite the editor, introduce an iframe, or change how the legacy application worked. We just needed to make sure Tailwind’s styles stayed where they belonged.
If you’re ever introducing React and Tailwind into an older application, especially one with years of existing CSS, hopefully this saves you some debugging time.