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: green; } </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> `; // <h1> is only visible from queries inside the shadow tree alert(document.querySelectorAll('h1').length); // 0 alert(elem.shadowRoot.querySelectorAll('h1').length); // 1 </script> </body> </html>