3

I need to create a "plugin" that contains a user interface which will be displayed on many different vendor websites. This is a CMS agnostic plugin. I cannot use an iframe for SEO reasons. I need to isolate the plugin's css (and maybe js) from the rest of the website, and stop the rest of the website's css from getting to this plugin. How can I do this?

Update:

Ok, so I've asked a question that's a little too specific to my setup/tech. The question should have been: How do I isolate an html element from the rest of the document styles? This is answered here;

New Question: How do I scope Vue CSS so that it doesn't propagate up, but propagates to child components?

E.g I have the main Vue component which includes bootstrap.scss, i need that to apply to all child components, but I don't want it to leak into the main website. Adding scoped to style stops the leak upward, but I want it to apply to child classes as well.

2
  • 1
    I think you can use <style></style> tags within the .vue file, that makes the style work for only what is enclosed in the <template></template> tag of that file Commented Oct 16, 2017 at 19:24
  • I also need to use bootstrap but I want it's own separate version. I also don't want the rest of the website's css to leak into the Vue app. Is this even possible? Commented Oct 16, 2017 at 19:42

3 Answers 3

7

Ok, I've figured it out.

Pretty simple really, combined with this answer to prevent parent -> child inheritance. I scoped all Vue css into #app { /*styles*/ } INCLUDING the bootstrap import. E.g.

<style type="text/scss" lang="scss">
    #app {
        @import '../node_modules/bootstrap/scss/bootstrap';

        // rest of vue global styles go here.
        // child components may use scoped
    }
</style>

Note: I am NOT using scoped attribute on the root vue component.

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

4 Comments

This will include all of bootstrap's css - do you need all of it? Is file size/ load time not a concern for you?
No, it's not. These are small sites with not too much traffic.
Thanks you for this, I have been using Less and this was not working, changing to scss works for me
@Jeff Thanks for sharing the answer. This worked for me albeit a bit too well. I find that the styles defined inside my Vue components are also ignored. This happens regardless of whether they are scoped or not. Might I be missing something here?
1

I solved this as follows, using the postcss-plugin-namespace plugin:

  1. Add an extra wrapper in the /src/App.vue file, e.g. if your main index.html app file has <div id="myapp">, in /src/App.vue add <div id="myapp-inner"> and </div> to your <template>. This will mean the actual HTML remains the same, but the generated HTML structure (viewed in the browser console's inspector) has this additional wrapper div just inside the main one.

  2. Using PostCSS, in /postcss.config.js, add require('postcss-plugin-namespace')('#myapp') where the parameter matches the outer div. This will mean all CSS directives get prefixed with #myapp. (You may need to change other plugins already listed there from the object key-value pairs format {'...': '', '...': ''} to the array list requires format: [require(...), require(...)] format.)

  3. If you have any existing CSS in components that are defined as top-level, you will need to adjust these to reference the inner div, e.g. #myapp {-webkit-font-smoothing: antialiased;} needs to become #myapp-inner {-webkit-font-smoothing: antialiased;} - as these will then resolve eventually to #myapp #myapp-inner {-webkit-font-smoothing: antialiased;}.

  4. Then yarn build as normal.

This approach feels architecturally cleanest, because the postcss-plugin-namespace basically does the prefixing of the CSS at the very end of the build process.

Comments

0

I think this is what you’re looking for. In your .vue file you can add style tags to the template, then Vue will create Shadow DOM styles that only apply to your application. In the final product the styles are rendered via a data-v attribute to prevent class name conflicts.

https://vue-loader.vuejs.org/en/features/scoped-css.html

(Copied from my reddit answer) https://www.reddit.com/r/vuejs/comments/76ss46/how_to_isolate_a_vue_application_from_the_rest_of/?st=J8UMA1JQ&sh=c3ebf5b1

1 Comment

Hey, I am using that attribute currently, but I have a new issue. Please see my updated question.

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.