I have a problem regarding a hover function programmed to change the width of three divs once one of these divs is hovered. This means that if you hover the first div
, the second and third div
also change in width, but all with different width values. Also, once you hover the second div
, the first and third div
should also change in width. This should work the same for the third div.
The problem starts once you hover the second div
, because then the second and third div only change in width, but not the first div
. The first div
then doesn't change in width anymore. This problem continues once you hover the third div
, then both the first and second div
aren't changing in width, only the third div
does.
There is however, no problem if you hover the first div
, then all the other divs change to the correct width.
For this I used the sibling combinator ( ~ ) in CSS. For example, once you hover div 1
the style of div 2
is changed using this selector: .tile-1:hover ~ .tile-2
.
Please see my code on CodePen or below:
<div class="tile tile-1"></div>
<div class="tile tile-2"></div>
<div class="tile tile-3"></div>
<style>
/* simple styling */
.tile {
background-color: #000;
width: 200px;
height: 100px;
border-style: solid;
border-color: white;
}
/* tile 1 */
.tile-1:hover {
width: 250px;
}
.tile-1:hover ~ .tile-2 {
width: 300px;
}
.tile-1:hover ~ .tile-3 {
width: 350px;
}
/* tile 2 */
.tile-2:hover ~ .tile-1 {
width: 350px;
}
.tile-2:hover {
width: 300px;
}
.tile-2:hover ~ .tile-3 {
width: 250px;
}
/* tile 3 */
.tile-3:hover ~ .tile-1 {
width: 300px;
}
.tile-3:hover ~ .tile-2 {
width: 250px;
}
.tile-3:hover {
width: 350px;
}
</style>
I hope anyone can help by having the selectors .tile-2:hover ~ .tile-1
, .tile-3:hover ~ .tile-1
and .tile-3:hover ~ .tile-2
work, since on the current code their width value isn't applied.