I'm looking for a way to select, using pure CSS selectors, the "closest" descendant.
Basically, I have a hierachical structure of container/containee divs. When I put a specific CSS marker on the container, I want to apply some rules on its containee. But as its hierarchical, I don't want to apply the rules to containee in nested containers.
This could have been easily using the >
operator, but I don't control the markup between the container and its containee, it may have several level of other markup.
To reproduce my issue, here is a jsfiddle : http://jsfiddle.net/stevebeauge/2xjh1jdf/
It includes this markup:
<div class="container">
<div class="some">
<div class="containee">Border should not be dotted</div>
</div>
</div>
<div class="container marker">
<div class="containee">Border should be dotted</div>
</div>
<div class="container marker">
<div class="containee">
<p>Border should be dotted</p>
<div class="container">
<div class="containee">
<p>Border should not be dotted</p>
<div class="container marker">
<div class="some">
<div class="containee">Border should be dotted</div>
</div>
</div>
</div>
</div>
</div>
</div>
and using this CSS :
div {
margin:3px;
border: solid 3px;
}
.container {
border-color: red
}
.containee {
border-color : green;
border-style: solid;
}
.marker .containee {
border-style: dotted;
border-color:blue;
}
The actual result is:
Whereas the expected result is:
I known I could use a javascript solution (and it's actually my alternative), but I'd prefer a pure CSS solution to avoid polluting the application with such JS.
I also tried using some weird selectors :
.marker .containee,
.marker > div:not(.container) > .containee,
.marker > div:not(.container) > div:not(.container) > .containee {
border-style: dotted;
border-color:blue;
}
But not only this does not solve the problem, but I requires to create the rule for each expected depth level of containee...