admin管理员组

文章数量:1317898

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 ponents?

E.g I have the main Vue ponent which includes bootstrap.scss, i need that to apply to all child ponents, 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.

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 ponents?

E.g I have the main Vue ponent which includes bootstrap.scss, i need that to apply to all child ponents, 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.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 16, 2017 at 19:05 JeffJeff 2,0682 gold badges16 silver badges18 bronze badges 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 – Michael Oshosanya 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? – Jeff Commented Oct 16, 2017 at 19:42
Add a ment  | 

3 Answers 3

Reset to default 7

Ok, I've figured it out.

Pretty simple really, bined 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 ponents may use scoped
    }
</style>

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

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 ponents 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 bee #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.

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/en/features/scoped-css.html

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

本文标签: javascriptHow to scope internal Vue application css from rest of the websiteStack Overflow