2

I am looking for a way to make this work:

when I hover on one link or image, the corresponding paragraph should become visible

for instance: hover on #imgHover1 makes #basishover1 visible

<table>
<tr>
<td><a href="link"><img src="image"id="imgHover1"></a></td>
<td><a href="link"><img src="image"id="imgHover2"></a></td>
<td><a href="link"><img src="image"id="imgHover3"></a></td>
<td><a href="link"><img src="image"id="imgHover4"></a></td>
<td><a href="link"><img src="image"id="imgHover5"></a></td>
</tr>
<tr>
<td><p id="basishover1">Basis</p></td>
<td><p id="basishover2">Basis</p></td>
<td><p id="basishover3">Basis</p></td>
<td><p id="basishover4">Basis</p></td>
<td><p id="basishover5">Basis</p></td>
<tr>

this is my current css but it doesn't work

#basishover1 {
visibility: hidden;
}
#imgHover1:hover  #basishover1{
visibility: visible;
}
Sibeal
  • 65
  • 6
  • 3
    css selectors only allow for selection of child or sibling elements in the current spec. If you want to keep your html as you have it you will need to use JS to handle the hover event – haxxxton Aug 07 '14 at 01:24
  • What do you suggest that I change in my html to make it viable? – Sibeal Aug 07 '14 at 01:31

1 Answers1

1

edit: updated example to use sibling selectors

the rule you wrote:

#imgHover1:hover  #basishover1{
    visibility: visible;
}

applies an object whose id is #basishover1, inside of another object whose id is #imgHover1 and whose pseudoclass is hover. You don't have such an object in your DOM, no matter where you hover with your mouse, thus this rule is not going to match anything.

If you want it to work with just CSS you have to have the paragraph inside the same container as the object you are hovering, in this case maybe something like this:

<table>
<tr>
<td><a href="link" id="linkHover1"><img src="image"id="imgHover1"></a>
    <p id="basishover1">Basis</p></td>
<td><a href="link" id="linkHover2"><img src="image"id="imgHover2"></a>
    <p id="basishover2">Basis</p></td>
</tr>
</table>

with this CSS:

#basishover1 {
visibility: hidden;
}
#linkHover1:hover ~ #basishover1{
visibility: visible;
}

you may need to manually correct box positions and sizes to obtain the desired effect.

If you want to keep the original DOM structure you have to use little javascript. This has the additional advantage, by using some framework, you can animate transitions.

pqnet
  • 6,070
  • 1
  • 30
  • 51