Styling Shadow DOM

In this example below you will see how to do a Styling Shadow DOM with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by vladimirs, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright vladimirs ©
  • HTML
  • CSS
  • JavaScript
    <span>test</span>

/*Downloaded from https://www.codeseek.co/vladimirs/styling-shadow-dom-rxzymx */
    body {
  color: black;
}

span {
  color: blue;
}


/*Downloaded from https://www.codeseek.co/vladimirs/styling-shadow-dom-rxzymx */
    var html = '<span>Shadow DOM</span>';

// span appended to shadow dom will not get outer styles applied
var p = document.createElement('p');
p.createShadowRoot();
p.shadowRoot.innerHTML = html;
document.querySelector('span').appendChild(p);

// span appended to <body> will get styles outer applied
var p2 = document.createElement('p');
p2.createShadowRoot();
p2.shadowRoot.innerHTML = html;
document.body.appendChild(p2);

Comments