Analyzing the existing selector:
*:not(my-component) *
* at the beginning is irrelevant.
:not(my-component) selects everything that is not my-component, which includes <html> and <body>.
The universal selector at the end selects everything descendant from <html>. Which is everything except the <html> itself.
That is not what we want.
Analyzing alternative:
body :not(my-component *)
body at the beginning to limit ourselves to descendants of <body>.
my-component * selects all children of my-component.
Then we pass the above as the argument of :not() i.e. :not(my-component *) to say everything that is not a descendant of my-component.
All together we have:
All elements that are descendants of body but not descendants of my-component.
This does include my-component itself but I believe that is what you are looking for:
Style all elements that are NOT inside my-component
my-component * {
color: green;
}
/* Style all elements that are descendants of body and NOT inside my-component */
body :not(my-component *) {
color: red;
}
body :not(my-component *) h4 {
color: blue;
}
.container {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 20px;
}
<div class="container">
<p>This paragraph is outside the special container → should be <strong style="color:red;">RED</strong>.</p>
<h4>Demostration h4 → should be BLUE.</h4>
</div>
<my-component class="container">
<p>This paragraph is inside the special container → should be <strong style="color:green;">GREEN</strong>.</p>
<span>Another element inside → GREEN</span>
</my-component>
<div class="container">
<p>Another outside paragraph → RED</p>
</div>