Shadow DOM styles encapsulation












2















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.



reddit.com-vs-example.com



What changes I have to make to be sure that the widget's styles will be consistent across all web pages.










share|improve this question




















  • 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
















2















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.



reddit.com-vs-example.com



What changes I have to make to be sure that the widget's styles will be consistent across all web pages.










share|improve this question




















  • 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














2












2








2








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.



reddit.com-vs-example.com



What changes I have to make to be sure that the widget's styles will be consistent across all web pages.










share|improve this question
















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.



reddit.com-vs-example.com



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





    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














  • 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








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












1 Answer
1






active

oldest

votes


















1














wOxxOm gave the correct answer:




Use mount.style.cssText='all:initial' instead of :host







share|improve this answer
























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    wOxxOm gave the correct answer:




    Use mount.style.cssText='all:initial' instead of :host







    share|improve this answer




























      1














      wOxxOm gave the correct answer:




      Use mount.style.cssText='all:initial' instead of :host







      share|improve this answer


























        1












        1








        1







        wOxxOm gave the correct answer:




        Use mount.style.cssText='all:initial' instead of :host







        share|improve this answer













        wOxxOm gave the correct answer:




        Use mount.style.cssText='all:initial' instead of :host








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 4:38









        Eugene KarataevEugene Karataev

        367




        367
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values