To understand why you aren't seeing the result you want, you need to first understand CSS Specificity and what weight selectors have (which is too long of a discussion as an answer to this question).
First, your original set of rules:
.wrapper > * { // [0,0,0,1,0]
background: deepskyblue;
}
.item { // [0,0,0,1,0]
background: deeppink;
}
Both of these rules have a specificity of [0,0,0,1,0]
because of the single class within each selector. The *
selector has a weight of 0, so it does not add any weight to the first rule. Since both rules affect the same property on the same element, the rule that comes last wins: in this case, background: deeppink;
However, your actual rules are different:
.wrapper[_ngcontent-c1] > *[_ngcontent-c1] { // [0,0,0,3,0]
background: deepskyblue;
}
.item[_ngcontent-c1] { // [0,0,0,2,0]
background: deeppink;
}
In this case, the first rule wins because it is more specific. Attribute selectors have a weight of [0,0,0,1,0]
.
To remedy this, you have to make a selector that equals or surpasses the specificity of the rule to be overridden.
For example:
.wrapper[_ngcontent-c1] > *[_ngcontent-c1] { // [0,0,0,3,0]
background: deepskyblue;
}
.wrapper[_ngcontent-c1] > .item[_ngcontent-c1] { // [0,0,0,4,0]
background: deeppink;
}