Maybe it’s a strange question since I don’t wanna know ‘how’ but ‘why’, but I think the answer might be valuable for those who wish to understand the way css works better.
I’m trying to make each div on my page reveal an ‘x’ div which will allow the user to close that div. There are several dynamically created divs.
I have this (dynamic) html:
<div class="box">
<div class="x">X</div>
</div>
<div class="box">
<div class="x">X</div>
</div>
[the number of ‘box’ divs varies]
And this css:
.x {
visibility: hidden;
}
.box:hover .x {
visibility: visible;
}
I really didn’t think this could work but somehow it does:
But how does this simple css code ‘knows’ which x div should be revealed, where there are no ids to distinct the ‘box’ divs nor the ‘x’ divs?
Because it looks for the descendent
.xSo when you hover over box 2 it applies the
:hoverand according to the css rule the.xthat’s inside the.boxwith the:hovershould be visible.Wouldn’t really know how to explain it differently 😛