Shadow DOM styles encapsulation
I want to create a Chrome extension which will inject a widget into a web page.
My widget must have a consistent style when injected on any page. Afaik, the best way to achieve this is to use Shadow DOM.
It seems that by design Shadow DOM styles inherits from parent's page styles. One have to use all:initial
CSS property for host element to prevent parent page's styles leak into the shadow DOM styles.
So, I have a sample code:
(function addWidget() {
let rootEl = document.querySelector('body');
let mount = document.createElement('div');
rootEl.appendChild(mount);
let shadowRoot = mount.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<style>
:host {
all: initial;
}
div {
position: fixed;
z-index: 2147483647;
border: 1px solid black;
padding: 30px;
font-size: 30px;
background: tomato;
top: 10px;
right: 10px;
}
</style>
<div>Shadow DOM</div>
`;
}());
If you open Chrome's dev tools and execute this code in the console, it'll work as expected on most websites. But on some websites (like reddit.com) the styles are still inherited from the parent page.
What changes I have to make to be sure that the widget's styles will be consistent across all web pages.
css google-chrome-extension shadow-dom
add a comment |
I want to create a Chrome extension which will inject a widget into a web page.
My widget must have a consistent style when injected on any page. Afaik, the best way to achieve this is to use Shadow DOM.
It seems that by design Shadow DOM styles inherits from parent's page styles. One have to use all:initial
CSS property for host element to prevent parent page's styles leak into the shadow DOM styles.
So, I have a sample code:
(function addWidget() {
let rootEl = document.querySelector('body');
let mount = document.createElement('div');
rootEl.appendChild(mount);
let shadowRoot = mount.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<style>
:host {
all: initial;
}
div {
position: fixed;
z-index: 2147483647;
border: 1px solid black;
padding: 30px;
font-size: 30px;
background: tomato;
top: 10px;
right: 10px;
}
</style>
<div>Shadow DOM</div>
`;
}());
If you open Chrome's dev tools and execute this code in the console, it'll work as expected on most websites. But on some websites (like reddit.com) the styles are still inherited from the parent page.
What changes I have to make to be sure that the widget's styles will be consistent across all web pages.
css google-chrome-extension shadow-dom
2
Usemount.style.cssText='all:initial'
instead of :host
– wOxxOm
Nov 15 '18 at 13:16
Wow, thanks a lot!
– Eugene Karataev
Nov 15 '18 at 13:27
@EugeneKarataev wOxxOm is statistically unlikely to turn his comment into an answer, could you please write a self-answer to this?
– Xan
Nov 15 '18 at 13:53
@Xan, sure, done.
– Eugene Karataev
Nov 16 '18 at 4:39
add a comment |
I want to create a Chrome extension which will inject a widget into a web page.
My widget must have a consistent style when injected on any page. Afaik, the best way to achieve this is to use Shadow DOM.
It seems that by design Shadow DOM styles inherits from parent's page styles. One have to use all:initial
CSS property for host element to prevent parent page's styles leak into the shadow DOM styles.
So, I have a sample code:
(function addWidget() {
let rootEl = document.querySelector('body');
let mount = document.createElement('div');
rootEl.appendChild(mount);
let shadowRoot = mount.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<style>
:host {
all: initial;
}
div {
position: fixed;
z-index: 2147483647;
border: 1px solid black;
padding: 30px;
font-size: 30px;
background: tomato;
top: 10px;
right: 10px;
}
</style>
<div>Shadow DOM</div>
`;
}());
If you open Chrome's dev tools and execute this code in the console, it'll work as expected on most websites. But on some websites (like reddit.com) the styles are still inherited from the parent page.
What changes I have to make to be sure that the widget's styles will be consistent across all web pages.
css google-chrome-extension shadow-dom
I want to create a Chrome extension which will inject a widget into a web page.
My widget must have a consistent style when injected on any page. Afaik, the best way to achieve this is to use Shadow DOM.
It seems that by design Shadow DOM styles inherits from parent's page styles. One have to use all:initial
CSS property for host element to prevent parent page's styles leak into the shadow DOM styles.
So, I have a sample code:
(function addWidget() {
let rootEl = document.querySelector('body');
let mount = document.createElement('div');
rootEl.appendChild(mount);
let shadowRoot = mount.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<style>
:host {
all: initial;
}
div {
position: fixed;
z-index: 2147483647;
border: 1px solid black;
padding: 30px;
font-size: 30px;
background: tomato;
top: 10px;
right: 10px;
}
</style>
<div>Shadow DOM</div>
`;
}());
If you open Chrome's dev tools and execute this code in the console, it'll work as expected on most websites. But on some websites (like reddit.com) the styles are still inherited from the parent page.
What changes I have to make to be sure that the widget's styles will be consistent across all web pages.
css google-chrome-extension shadow-dom
css google-chrome-extension shadow-dom
edited Nov 15 '18 at 13:52
Xan
54.9k10109139
54.9k10109139
asked Nov 15 '18 at 13:08
Eugene KarataevEugene Karataev
367
367
2
Usemount.style.cssText='all:initial'
instead of :host
– wOxxOm
Nov 15 '18 at 13:16
Wow, thanks a lot!
– Eugene Karataev
Nov 15 '18 at 13:27
@EugeneKarataev wOxxOm is statistically unlikely to turn his comment into an answer, could you please write a self-answer to this?
– Xan
Nov 15 '18 at 13:53
@Xan, sure, done.
– Eugene Karataev
Nov 16 '18 at 4:39
add a comment |
2
Usemount.style.cssText='all:initial'
instead of :host
– wOxxOm
Nov 15 '18 at 13:16
Wow, thanks a lot!
– Eugene Karataev
Nov 15 '18 at 13:27
@EugeneKarataev wOxxOm is statistically unlikely to turn his comment into an answer, could you please write a self-answer to this?
– Xan
Nov 15 '18 at 13:53
@Xan, sure, done.
– Eugene Karataev
Nov 16 '18 at 4:39
2
2
Use
mount.style.cssText='all:initial'
instead of :host– wOxxOm
Nov 15 '18 at 13:16
Use
mount.style.cssText='all:initial'
instead of :host– wOxxOm
Nov 15 '18 at 13:16
Wow, thanks a lot!
– Eugene Karataev
Nov 15 '18 at 13:27
Wow, thanks a lot!
– Eugene Karataev
Nov 15 '18 at 13:27
@EugeneKarataev wOxxOm is statistically unlikely to turn his comment into an answer, could you please write a self-answer to this?
– Xan
Nov 15 '18 at 13:53
@EugeneKarataev wOxxOm is statistically unlikely to turn his comment into an answer, could you please write a self-answer to this?
– Xan
Nov 15 '18 at 13:53
@Xan, sure, done.
– Eugene Karataev
Nov 16 '18 at 4:39
@Xan, sure, done.
– Eugene Karataev
Nov 16 '18 at 4:39
add a comment |
1 Answer
1
active
oldest
votes
wOxxOm gave the correct answer:
Use mount.style.cssText='all:initial' instead of :host
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320205%2fshadow-dom-styles-encapsulation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
wOxxOm gave the correct answer:
Use mount.style.cssText='all:initial' instead of :host
add a comment |
wOxxOm gave the correct answer:
Use mount.style.cssText='all:initial' instead of :host
add a comment |
wOxxOm gave the correct answer:
Use mount.style.cssText='all:initial' instead of :host
wOxxOm gave the correct answer:
Use mount.style.cssText='all:initial' instead of :host
answered Nov 16 '18 at 4:38
Eugene KarataevEugene Karataev
367
367
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320205%2fshadow-dom-styles-encapsulation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Use
mount.style.cssText='all:initial'
instead of :host– wOxxOm
Nov 15 '18 at 13:16
Wow, thanks a lot!
– Eugene Karataev
Nov 15 '18 at 13:27
@EugeneKarataev wOxxOm is statistically unlikely to turn his comment into an answer, could you please write a self-answer to this?
– Xan
Nov 15 '18 at 13:53
@Xan, sure, done.
– Eugene Karataev
Nov 16 '18 at 4:39