2

This is the code:

HTML:

<img src="..." />

// other stuff 

<div id="image">
<a href="" > bla bla bla </a>
<a href="" > ble ble ble </a>
</div>

CSS:

#image a:hover{color:green;}

I want this:

When the user put mouse over the image, then all the links in the div with id "image" become green ( like if the user put the mouse over the links. ).

If possible I prefer to do this with only CSS.

1
  • 1
    You really should use a class instead of an id, since technically you should only have 1 of each unique id. Commented May 1, 2013 at 16:44

3 Answers 3

7
img:hover + #image a {
    color: green;
    text-decoration: none;
}

http://jsfiddle.net/Tzpmd/

Sign up to request clarification or add additional context in comments.

4 Comments

Yep this is supported in IE 8 + and most versions of open source browsers
but this works only if there isn't nothign between the image and the links :( jsfiddle.net/QSy9H/4
Because its the <table> thats a sibling of the <img> no the <td> take a look: jsfiddle.net/Adrift/QSy9H/21 .. you need to use #topimg:hover ~ table #image td a instead
3
img:hover + #image a{color:green;}

Though there are some browser quirks with respect to :hover used with +, so you'll need to do some testing and see if your supported browsers are affected.

And of course + will not be supported in old browsers.


If there's an element in between, you can use ~ instead.

img:hover ~ #image a{color:green;}

4 Comments

No, some old browsers won't work. And there are some bugs in some browsers when combining :hover with +, so you should test your supported browsers.
but this works only if there isn't nothign between the image and the links :( jsfiddle.net/QSy9H/4
@xRobot: Right. That's what your markup example showed. You can change + to ~.
@xRobot: Because #image isn't a sibling of the img. If you want it to affect all nested a elements, use #topimg:hover ~ * a { jsfiddle.net/QSy9H/27, or make * specific to the sibling you want to affect.
1

@xRobot, the problem with your fiddle is that you're not referencing the sibling of #topimg, which is the table not the tr#image. The element you are referencing is the child of the table element, and niece of the #topimg element (not sibling).

Check this updated fiddle: http://jsfiddle.net/QSy9H/32/

... and using the examples on this page:

img:hover ~ table #image a{ color:green; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.