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.
-
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
3 Answers
Reset to default 7Ok, 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:
Add an extra wrapper in the
/src/App.vue
file, e.g. if your mainindex.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.Using PostCSS, in
/postcss.config.js
, addrequire('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.)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;}
.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
版权声明:本文标题:javascript - How to scope internal Vue application css from rest of the website? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742035866a2417288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论