0

I’m working on a project using Vite and React to build a widget that users can integrate into their websites. I’m using Tailwind CSS for styling and injecting the styles into the DOM using the vite-plugin-css-injected-by-js plugin. However, I’m facing issues where my widget styles are overriding the host page styles, and sometimes the host page styles are overriding my widget styles. Here’s apart of my current Vite configuration:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';
import tailwindcss from '@tailwindcss/vite';

export default defineConfig({
  plugins: [
    react(),
    tailwindcss(),
    cssInjectedByJsPlugin({ styleId: 'widget-styles' }),
  ],
});

And here’s my index.css file:

@import 'tailwindcss' important;

@layer base {
  *,
  ::after,
  ::before,
  ::backdrop,
  ::file-selector-button {
    border-color: var(--color-gray-200, currentColor);
  }
}

@layer utilities {
  /* Chrome, Safari and Opera */
  .no-scrollbar::-webkit-scrollbar {
    display: none;
  }

  .no-scrollbar {
    -ms-overflow-style: none; /* IE and Edge */
    scrollbar-width: none; /* Firefox */
  }
}

Questions: 1-How can I ensure that my widget’s Tailwind CSS styles do not override the host page styles?

2-How can I prevent the host page styles from overriding my widget’s styles?

Are there any best practices or Vite/Tailwind configurations I can use to isolate my widget’s styles effectively?

I’ve used the cssInjectedByJsPlugin to inject styles with a unique styleId.

I’ve added @import 'tailwindcss' important; to my index.css to increase specificity.

3
  • From v4 onwards can use @utility CSS-first directive instead of @layer utilities. See more here: @utility directive and Adding custom utilities from v4 Commented Mar 19 at 14:17
  • Additionally, you should provide an example of something that doesn't look the way you want because something is overriding it. I like to see things, not just talk into the air, so it’s not very clear at first - maybe I just don't fully understand what you're trying to convey - but I don't really get how TailwindCSS wouldn't override your website's styles, yet it does override part of your website's styles. So, at first glance, it sounds like mission impossible. Commented Mar 19 at 14:22
  • That's why I left an answer; I think after a better understanding of layer specificity, you'll be able to fit your website's styles into the appropriate layer. Commented Mar 19 at 14:22

1 Answer 1

0

How can I ensure that my widget’s Tailwind CSS styles do not override the host page styles?

Don't use important. The important layers have too stronger specificity (stronger than unlayered important style). You added TailwindCSS with important, so it will always be overwritten. I think this is the wrong approach, and it's enough to just use:

@import "tailwindcss"; /* without important */

Alternatively, you can manually add what @import "tailwindcss"; would do and customize it:

/* instead of @import "tailwindcss"; */

@layer theme, base, components, utilities;

@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/preflight.css" layer(base);
@import "tailwindcss/utilities.css" layer(utilities);

How can I prevent the host page styles from overriding my widget's styles?

Anything defined outside of @layer (as Unlayered) will be stronger than @layer, see here:

In order for the host styles not to be stronger than TailwindCSS, you need to place them in a weaker layer or you can place it, for example, in the base layer.

The attached MDN Docs also show that in the case of important layers, the Unlayered one will be placed last. This is what causes your issue.

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

Comments

Your Answer

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.