Css for Same Level

Css for Same Level

If I have 3 divs at the same level ( not one in another ) . How can I change the color of the other div when hover one without using IDs and classes. I would like somthing like :

<div id="1" ></div>
<div></div>
<div></div>

And CSS :

#1 :hover < body > div
{
    //Here I change the things
}
1

2 Answers

Use the general sibling combinator

#yourId:hover ~ div
{
    color:red;
}

Also note that Id's must begin with a letter. W3 ID Attribute

Example

3

Put a wrapper around them, then put the hover on the wrapper.

<div class="wrapper">
    <div class="element">foo</div>
    <div class="element">bar</div>
    <div class="element">baz</div>
</div>

.wrapper:hover .element {
    color: red;
}

Example:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.