MVCE
You can execute this code snippet:
const a = document.createElement("div");
// display big red rectangle in top left
a.style.position = "absolute"; a.style.left = "10px"; a.style.top = "10px";
a.style.background = "red";
a.style.color = "green";
a.style.fontSize = "large";
document.body.appendChild(a);
// attach shadow root
a.attachShadow({mode: 'closed'}).innerHTML = `
<style>
:host {
all: initial;
display: block;
}
</style>
<p>
A paragraph element which should not inherit any styles from its parent webpage (outside the shadow DOM).
</p>`;
Description
I have copied the shadow root section verbatim from lamplightdev, but the same code is also given on various Stack Overflow threads. Due to this code, the <p> element is NOT supposed to inherit any styles from its parent body.
Problem
You can run the code snippet to see that the paragraph element appears green with large font size which is unexpected because I have set :host { all: initial; }.
In DevTools I can also see that the paragraph element is showing style rules that are "inherited from div" which is outside my web component.
Question
I want my web component to not inherit parent page styles, yet why is it doing so?
