how to restyle element in #shadow-root ( shadow DOM )












1















the following code is a view from chrome dev tools



<textarea>
#shadow-root (user-agent)
<p> This I want to restyle </p>
<textarea>


what CSS selector I have to use if I would like to restyle element in shadow DOM ?



thank you










share|improve this question


















  • 1





    Get deep into that; gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813, ...but answering your question it's impossible to style it with any selector.

    – Jakub Chlebowicz
    Nov 22 '18 at 12:47






  • 2





    You cannot access the content of a user-agent Shadow DOM. stackoverflow.com/a/38736220/4600982

    – Supersharp
    Nov 22 '18 at 13:44











  • @connexo :) actually in my case textarea has two divs, one for placeholder and second for the content itself. To my surprise, I could restyle placeholder directly from CSS with this ::placeholder, but still struggling with the content div restyling

    – Pavel Franta
    Nov 26 '18 at 9:41











  • @connexo I didn't customize anything, it is like it was, plain HTML element textarea. So it should be valid i guess :D

    – Pavel Franta
    Nov 26 '18 at 13:08













  • @connexo ? If I inspect plain, empty textarea in chrome dev tools, it contains at least empty <div></div> in shadow-root ... what exactly is unclear about that ? Question was how to restyle this div... if you dont know any useful answer so please don't waste your time with my question :)

    – Pavel Franta
    Nov 27 '18 at 15:06


















1















the following code is a view from chrome dev tools



<textarea>
#shadow-root (user-agent)
<p> This I want to restyle </p>
<textarea>


what CSS selector I have to use if I would like to restyle element in shadow DOM ?



thank you










share|improve this question


















  • 1





    Get deep into that; gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813, ...but answering your question it's impossible to style it with any selector.

    – Jakub Chlebowicz
    Nov 22 '18 at 12:47






  • 2





    You cannot access the content of a user-agent Shadow DOM. stackoverflow.com/a/38736220/4600982

    – Supersharp
    Nov 22 '18 at 13:44











  • @connexo :) actually in my case textarea has two divs, one for placeholder and second for the content itself. To my surprise, I could restyle placeholder directly from CSS with this ::placeholder, but still struggling with the content div restyling

    – Pavel Franta
    Nov 26 '18 at 9:41











  • @connexo I didn't customize anything, it is like it was, plain HTML element textarea. So it should be valid i guess :D

    – Pavel Franta
    Nov 26 '18 at 13:08













  • @connexo ? If I inspect plain, empty textarea in chrome dev tools, it contains at least empty <div></div> in shadow-root ... what exactly is unclear about that ? Question was how to restyle this div... if you dont know any useful answer so please don't waste your time with my question :)

    – Pavel Franta
    Nov 27 '18 at 15:06
















1












1








1


1






the following code is a view from chrome dev tools



<textarea>
#shadow-root (user-agent)
<p> This I want to restyle </p>
<textarea>


what CSS selector I have to use if I would like to restyle element in shadow DOM ?



thank you










share|improve this question














the following code is a view from chrome dev tools



<textarea>
#shadow-root (user-agent)
<p> This I want to restyle </p>
<textarea>


what CSS selector I have to use if I would like to restyle element in shadow DOM ?



thank you







html css3 shadow-dom






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 12:44









Pavel FrantaPavel Franta

334215




334215








  • 1





    Get deep into that; gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813, ...but answering your question it's impossible to style it with any selector.

    – Jakub Chlebowicz
    Nov 22 '18 at 12:47






  • 2





    You cannot access the content of a user-agent Shadow DOM. stackoverflow.com/a/38736220/4600982

    – Supersharp
    Nov 22 '18 at 13:44











  • @connexo :) actually in my case textarea has two divs, one for placeholder and second for the content itself. To my surprise, I could restyle placeholder directly from CSS with this ::placeholder, but still struggling with the content div restyling

    – Pavel Franta
    Nov 26 '18 at 9:41











  • @connexo I didn't customize anything, it is like it was, plain HTML element textarea. So it should be valid i guess :D

    – Pavel Franta
    Nov 26 '18 at 13:08













  • @connexo ? If I inspect plain, empty textarea in chrome dev tools, it contains at least empty <div></div> in shadow-root ... what exactly is unclear about that ? Question was how to restyle this div... if you dont know any useful answer so please don't waste your time with my question :)

    – Pavel Franta
    Nov 27 '18 at 15:06
















  • 1





    Get deep into that; gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813, ...but answering your question it's impossible to style it with any selector.

    – Jakub Chlebowicz
    Nov 22 '18 at 12:47






  • 2





    You cannot access the content of a user-agent Shadow DOM. stackoverflow.com/a/38736220/4600982

    – Supersharp
    Nov 22 '18 at 13:44











  • @connexo :) actually in my case textarea has two divs, one for placeholder and second for the content itself. To my surprise, I could restyle placeholder directly from CSS with this ::placeholder, but still struggling with the content div restyling

    – Pavel Franta
    Nov 26 '18 at 9:41











  • @connexo I didn't customize anything, it is like it was, plain HTML element textarea. So it should be valid i guess :D

    – Pavel Franta
    Nov 26 '18 at 13:08













  • @connexo ? If I inspect plain, empty textarea in chrome dev tools, it contains at least empty <div></div> in shadow-root ... what exactly is unclear about that ? Question was how to restyle this div... if you dont know any useful answer so please don't waste your time with my question :)

    – Pavel Franta
    Nov 27 '18 at 15:06










1




1





Get deep into that; gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813, ...but answering your question it's impossible to style it with any selector.

– Jakub Chlebowicz
Nov 22 '18 at 12:47





Get deep into that; gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813, ...but answering your question it's impossible to style it with any selector.

– Jakub Chlebowicz
Nov 22 '18 at 12:47




2




2





You cannot access the content of a user-agent Shadow DOM. stackoverflow.com/a/38736220/4600982

– Supersharp
Nov 22 '18 at 13:44





You cannot access the content of a user-agent Shadow DOM. stackoverflow.com/a/38736220/4600982

– Supersharp
Nov 22 '18 at 13:44













@connexo :) actually in my case textarea has two divs, one for placeholder and second for the content itself. To my surprise, I could restyle placeholder directly from CSS with this ::placeholder, but still struggling with the content div restyling

– Pavel Franta
Nov 26 '18 at 9:41





@connexo :) actually in my case textarea has two divs, one for placeholder and second for the content itself. To my surprise, I could restyle placeholder directly from CSS with this ::placeholder, but still struggling with the content div restyling

– Pavel Franta
Nov 26 '18 at 9:41













@connexo I didn't customize anything, it is like it was, plain HTML element textarea. So it should be valid i guess :D

– Pavel Franta
Nov 26 '18 at 13:08







@connexo I didn't customize anything, it is like it was, plain HTML element textarea. So it should be valid i guess :D

– Pavel Franta
Nov 26 '18 at 13:08















@connexo ? If I inspect plain, empty textarea in chrome dev tools, it contains at least empty <div></div> in shadow-root ... what exactly is unclear about that ? Question was how to restyle this div... if you dont know any useful answer so please don't waste your time with my question :)

– Pavel Franta
Nov 27 '18 at 15:06







@connexo ? If I inspect plain, empty textarea in chrome dev tools, it contains at least empty <div></div> in shadow-root ... what exactly is unclear about that ? Question was how to restyle this div... if you dont know any useful answer so please don't waste your time with my question :)

– Pavel Franta
Nov 27 '18 at 15:06














1 Answer
1






active

oldest

votes


















3














ShadowDOM was designed to prevent CSS from leaking INTO or OUT OF the shadowDOM. It is kindof a replacement for <iframe> which had the same limitations on it. Any CSS in the <iframe> can not affect the content outside of the <iframe> and the CSS outside the <iframe> can not affect the content inside <iframe>.



But you can affect the inner CSS by using one of the following options:




None of the options below work for existing HTML elements. These examples are only for custom elements you write.




The first way to style an element in shadowDOM is by placing the styles in the shadowDOM with the content.






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: #A00; color: white; }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

<my-el></my-el>





The second, more limited way, is to use CSS variables:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: var(--bgcolor, #A00); color: var(--color, white); }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

body {
--bgcolor: yellow;
--color: navy;
}

<my-el></my-el>





The third way, also limited, is through attributes or properties:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<p>inner content</p>`;
}

set bgColor(val) {
this.shadowRoot.querySelector('p').style.backgroundColor = val;
}

set color(val) {
this.shadowRoot.querySelector('p').style.color = val;
}
}

customElements.define('my-el', MyEl);

const myEl = document.querySelector('my-el');
myEl.bgColor = '#090';
myEl.color = 'white';

<my-el></my-el>








share|improve this answer


























  • Doesn't each of these methods require that you have control over the source code of the element you are trying to style? What about mis-behaving third party components? for example i had a Auto Complete Search component that didn't have a z-index correctly set so the suggestions appeared behind other elements. We should be able to explicitly target this component.

    – Ryan B
    2 days ago











  • You should be able to change the z-index on the element itself and the shadowDOM should move with it.

    – Intervalia
    2 days ago











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%2f53431330%2fhow-to-restyle-element-in-shadow-root-shadow-dom%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









3














ShadowDOM was designed to prevent CSS from leaking INTO or OUT OF the shadowDOM. It is kindof a replacement for <iframe> which had the same limitations on it. Any CSS in the <iframe> can not affect the content outside of the <iframe> and the CSS outside the <iframe> can not affect the content inside <iframe>.



But you can affect the inner CSS by using one of the following options:




None of the options below work for existing HTML elements. These examples are only for custom elements you write.




The first way to style an element in shadowDOM is by placing the styles in the shadowDOM with the content.






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: #A00; color: white; }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

<my-el></my-el>





The second, more limited way, is to use CSS variables:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: var(--bgcolor, #A00); color: var(--color, white); }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

body {
--bgcolor: yellow;
--color: navy;
}

<my-el></my-el>





The third way, also limited, is through attributes or properties:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<p>inner content</p>`;
}

set bgColor(val) {
this.shadowRoot.querySelector('p').style.backgroundColor = val;
}

set color(val) {
this.shadowRoot.querySelector('p').style.color = val;
}
}

customElements.define('my-el', MyEl);

const myEl = document.querySelector('my-el');
myEl.bgColor = '#090';
myEl.color = 'white';

<my-el></my-el>








share|improve this answer


























  • Doesn't each of these methods require that you have control over the source code of the element you are trying to style? What about mis-behaving third party components? for example i had a Auto Complete Search component that didn't have a z-index correctly set so the suggestions appeared behind other elements. We should be able to explicitly target this component.

    – Ryan B
    2 days ago











  • You should be able to change the z-index on the element itself and the shadowDOM should move with it.

    – Intervalia
    2 days ago
















3














ShadowDOM was designed to prevent CSS from leaking INTO or OUT OF the shadowDOM. It is kindof a replacement for <iframe> which had the same limitations on it. Any CSS in the <iframe> can not affect the content outside of the <iframe> and the CSS outside the <iframe> can not affect the content inside <iframe>.



But you can affect the inner CSS by using one of the following options:




None of the options below work for existing HTML elements. These examples are only for custom elements you write.




The first way to style an element in shadowDOM is by placing the styles in the shadowDOM with the content.






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: #A00; color: white; }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

<my-el></my-el>





The second, more limited way, is to use CSS variables:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: var(--bgcolor, #A00); color: var(--color, white); }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

body {
--bgcolor: yellow;
--color: navy;
}

<my-el></my-el>





The third way, also limited, is through attributes or properties:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<p>inner content</p>`;
}

set bgColor(val) {
this.shadowRoot.querySelector('p').style.backgroundColor = val;
}

set color(val) {
this.shadowRoot.querySelector('p').style.color = val;
}
}

customElements.define('my-el', MyEl);

const myEl = document.querySelector('my-el');
myEl.bgColor = '#090';
myEl.color = 'white';

<my-el></my-el>








share|improve this answer


























  • Doesn't each of these methods require that you have control over the source code of the element you are trying to style? What about mis-behaving third party components? for example i had a Auto Complete Search component that didn't have a z-index correctly set so the suggestions appeared behind other elements. We should be able to explicitly target this component.

    – Ryan B
    2 days ago











  • You should be able to change the z-index on the element itself and the shadowDOM should move with it.

    – Intervalia
    2 days ago














3












3








3







ShadowDOM was designed to prevent CSS from leaking INTO or OUT OF the shadowDOM. It is kindof a replacement for <iframe> which had the same limitations on it. Any CSS in the <iframe> can not affect the content outside of the <iframe> and the CSS outside the <iframe> can not affect the content inside <iframe>.



But you can affect the inner CSS by using one of the following options:




None of the options below work for existing HTML elements. These examples are only for custom elements you write.




The first way to style an element in shadowDOM is by placing the styles in the shadowDOM with the content.






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: #A00; color: white; }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

<my-el></my-el>





The second, more limited way, is to use CSS variables:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: var(--bgcolor, #A00); color: var(--color, white); }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

body {
--bgcolor: yellow;
--color: navy;
}

<my-el></my-el>





The third way, also limited, is through attributes or properties:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<p>inner content</p>`;
}

set bgColor(val) {
this.shadowRoot.querySelector('p').style.backgroundColor = val;
}

set color(val) {
this.shadowRoot.querySelector('p').style.color = val;
}
}

customElements.define('my-el', MyEl);

const myEl = document.querySelector('my-el');
myEl.bgColor = '#090';
myEl.color = 'white';

<my-el></my-el>








share|improve this answer















ShadowDOM was designed to prevent CSS from leaking INTO or OUT OF the shadowDOM. It is kindof a replacement for <iframe> which had the same limitations on it. Any CSS in the <iframe> can not affect the content outside of the <iframe> and the CSS outside the <iframe> can not affect the content inside <iframe>.



But you can affect the inner CSS by using one of the following options:




None of the options below work for existing HTML elements. These examples are only for custom elements you write.




The first way to style an element in shadowDOM is by placing the styles in the shadowDOM with the content.






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: #A00; color: white; }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

<my-el></my-el>





The second, more limited way, is to use CSS variables:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: var(--bgcolor, #A00); color: var(--color, white); }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

body {
--bgcolor: yellow;
--color: navy;
}

<my-el></my-el>





The third way, also limited, is through attributes or properties:






class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<p>inner content</p>`;
}

set bgColor(val) {
this.shadowRoot.querySelector('p').style.backgroundColor = val;
}

set color(val) {
this.shadowRoot.querySelector('p').style.color = val;
}
}

customElements.define('my-el', MyEl);

const myEl = document.querySelector('my-el');
myEl.bgColor = '#090';
myEl.color = 'white';

<my-el></my-el>








class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: #A00; color: white; }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

<my-el></my-el>





class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: #A00; color: white; }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

<my-el></my-el>





class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: var(--bgcolor, #A00); color: var(--color, white); }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

body {
--bgcolor: yellow;
--color: navy;
}

<my-el></my-el>





class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<style>
p { background-color: var(--bgcolor, #A00); color: var(--color, white); }
</style>
<p>inner content</p>`;
}
}

customElements.define('my-el', MyEl);

body {
--bgcolor: yellow;
--color: navy;
}

<my-el></my-el>





class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<p>inner content</p>`;
}

set bgColor(val) {
this.shadowRoot.querySelector('p').style.backgroundColor = val;
}

set color(val) {
this.shadowRoot.querySelector('p').style.color = val;
}
}

customElements.define('my-el', MyEl);

const myEl = document.querySelector('my-el');
myEl.bgColor = '#090';
myEl.color = 'white';

<my-el></my-el>





class MyEl extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'}).innerHTML = `
<p>inner content</p>`;
}

set bgColor(val) {
this.shadowRoot.querySelector('p').style.backgroundColor = val;
}

set color(val) {
this.shadowRoot.querySelector('p').style.color = val;
}
}

customElements.define('my-el', MyEl);

const myEl = document.querySelector('my-el');
myEl.bgColor = '#090';
myEl.color = 'white';

<my-el></my-el>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 17:27

























answered Nov 22 '18 at 16:59









IntervaliaIntervalia

4,70211134




4,70211134













  • Doesn't each of these methods require that you have control over the source code of the element you are trying to style? What about mis-behaving third party components? for example i had a Auto Complete Search component that didn't have a z-index correctly set so the suggestions appeared behind other elements. We should be able to explicitly target this component.

    – Ryan B
    2 days ago











  • You should be able to change the z-index on the element itself and the shadowDOM should move with it.

    – Intervalia
    2 days ago



















  • Doesn't each of these methods require that you have control over the source code of the element you are trying to style? What about mis-behaving third party components? for example i had a Auto Complete Search component that didn't have a z-index correctly set so the suggestions appeared behind other elements. We should be able to explicitly target this component.

    – Ryan B
    2 days ago











  • You should be able to change the z-index on the element itself and the shadowDOM should move with it.

    – Intervalia
    2 days ago

















Doesn't each of these methods require that you have control over the source code of the element you are trying to style? What about mis-behaving third party components? for example i had a Auto Complete Search component that didn't have a z-index correctly set so the suggestions appeared behind other elements. We should be able to explicitly target this component.

– Ryan B
2 days ago





Doesn't each of these methods require that you have control over the source code of the element you are trying to style? What about mis-behaving third party components? for example i had a Auto Complete Search component that didn't have a z-index correctly set so the suggestions appeared behind other elements. We should be able to explicitly target this component.

– Ryan B
2 days ago













You should be able to change the z-index on the element itself and the shadowDOM should move with it.

– Intervalia
2 days ago





You should be able to change the z-index on the element itself and the shadowDOM should move with it.

– Intervalia
2 days ago




















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%2f53431330%2fhow-to-restyle-element-in-shadow-root-shadow-dom%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()