My understanding of web components was that you can use it to prevent css leaks from the web component to the parent. I need exactly that but for some reason styling inside the web component affects the whole document.
My current pairing buddy (ChatGPT) also doesn't understand what is happening .
<template id="test-shadow-dom-template">
<slot name="menu-bar"></slot>
<span id="contents">
<slot>The content</slot>
</span>
</template>
<script type="text/javascript">
if(!customElements.get('test-shadow-dom')) {
customElements.define('test-shadow-dom', class extends HTMLElement {
constructor() {
super();
let template = document.getElementById("test-shadow-dom-template");
let templateContent = template.content;
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(templateContent.cloneNode(true));
}
});
}
</script>
<div id="test-shadow-dom-container" class="box" style="height: 100%">
<test-shadow-dom id="my-id">
<style>
* {
font-style: italic;
}
</style>
<h1>Here is the actual contents...</h1>
</test-shadow-dom>
</div>
Chrome Developer tools
My understanding is that only the contents of the test-shadow-dom
would be italic yet everything on the webpage is italic.
Any idea what is going on?