Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> /* the document style will not apply to the shadow tree inside #elem*/ h1 { color: red; } </style> </head> <body> <div id="elem"></div> <script> elem.attachShadow({mode: 'open'}); // shadow tree has its own style elem.shadowRoot.innerHTML = ` <style> h1 { font-weight: bold; } </style> <h1>Welcome to W3Docs</h1> `; //need {mode: "open"}, otherwise elem.shadowRoot is null alert(elem.shadowRoot.host === elem); // true </script> </body> </html>