This code is going to be used to overload the style of a page I dont control. I cannot change the HTML nor can I use JavaScript. I can only rely on good old CSS. The HTML is dynamic and look like this:
<div class="container">
<div class="item" style="display: none">m</div>
<div class="item">n</div>
<div class="item">o</div>
<div class="item">p</div>
</div>
I dont know how many children .container
have. No nth-child()
as far as I am aware of. The following example is valid too:
<div class="container">
<div class="item" style="display: none">l</div>
<div class="item" style="display: none">m</div>
<div class="item">n</div>
<div class="item">o</div>
<div class="item">p</div>
</div>
I want to apply a specific property to the first visible element. Here it is 'n'.
If all element are red:
.item {
background: red;
}
I would like to change it to blue:
??? {
background: blue;
}
I tried:
.item {
background: red;
}
.item:not([style='display: none']):first-child {
background: blue;
}
Demo:
.item {
background: red;
}
.item:not([style='display: none']):first-child {
background: blue;
}
<div class="container">
<div class="item" style="display: none">m</div>
<div class="item">n</div>
<div class="item">o</div>
<div class="item">p</div>
</div>
Here 'n' should have a blue background.
How can I achieve that? What selector should I use?