In this code:
<div id="Container">
<span class='first'>First</span>
<span class='second'>Second</span>
<span class='third'>Third</span>
</div>
I want to change color, when :hover
.
.first:hover
) THEN .first { color: red; }
.second:hover
) THEN .first, .second { color: red; }
.third:hover
) THEN .first, .second, .third { color: red; }
Is this possible without JS? I can accept CSS Hacks :)
Possible answers:
More difficult version:
<div id="Container">
<span class='first' style='color: red'>First</span>
<span class='second' style='color: green'>Second</span>
<span class='third' style='color: blue'>Third</span>
</div>
.first:hover
) THEN .first { color: pink; }
.second:hover
) THEN .first, .second { color: pink; }
.third:hover
) THEN .first, .second, .third { color: pink; }
Answers:
In CSS there is no previous sibling selector, but... you can sometimes do it using known selectors. Sometimes.
<style>
span {color: #000; display: block;}
div:hover span {color: #f00;}
span:hover ~ span {color: #000}
</style>
<div id="FirstSecondThird-Container">
<span class='first'>First</span>
<span class='second'>Second</span>
<span class='third'>Third</span>
</div>
https://jsfiddle.net/45zLdcvr/
It works with block
span
s (of floated, of course). With span
s has default display: inline
it will blink when you will hover div
in space between spans.
UPDATE:
You updated the question when each span has own color, then it could be:
span {color: red}
.second {color: green}
.third {color: blue}
span {display: block;}
div:hover span {color: pink;}
span:hover ~ .second {color: green}
span:hover ~ .third {color: blue}
https://jsfiddle.net/45zLdcvr/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With