1

I want to change the background of the <TD> when the user hovers a link <a> in the <td>

Part of my HTML:

<tr class="menuBarBottomSelected">
<td width="20%">
<a class="menuBarBottomUnselected" href="./url.php">Getting&nbsp;Started</a>
</td>
</tr>

This is my css:

.menuBarBottomSelected{
    background-image: url('/auth/radius/submenu_bg.jpg');
    background-repeat: repeat-x;
    font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
    color : #ffffff;
    border-top:#666666 solid 1px;
}

tr.menuBarBottomUnselected td a:hover{
    color:black;    
    background-image: url('/auth/radius/submenu_bg_hover.jpg');
    background-repeat: repeat-x;}

The html is being generated so I am not sure why they would use the same class for the link and the td. I was able to assign a :hover on the link but only the background of the text changed not the whole <td>

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Denoteone
  • 4,043
  • 21
  • 96
  • 150

3 Answers3

5

You should display the link as a block and set width and height to 100%; this would fill your td with the anchor element and every pixel of the td will be act as a link(you can click on it). If that's fine then here is the solution:

.menuBarBottomSelected td a{
    display:block;
    width:100%;
    height:100%;
    background-image: url('/auth/radius/submenu_bg.jpg');
    background-repeat: repeat-x;
    font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
    color : #ffffff;
    border-top:#666666 solid 1px;
}

tr.menuBarBottomUnselected td a:hover{
    color:black;    
    background-image: url('/auth/radius/submenu_bg_hover.jpg');
    background-repeat: repeat-x;}

Look at example here: http://jsfiddle.net/mohsen/LnabQ/ This works in any browser

But if you don't want to fill your td with the anchor tag then you need to use JavaScript to add and remove classes to the td. in my knowledge you can't have td:hover

Mohsen
  • 64,437
  • 34
  • 159
  • 186
  • The display block does not fill the whole bg of the the width is fine but the height is still a little screwy. – Denoteone Sep 15 '11 at 17:46
  • Maybe your setup is different. margins or... look at my example: http://jsfiddle.net/mohsen/LnabQ/ – Mohsen Sep 15 '11 at 17:50
1

One way to do this is with an event. Assign onmouseover on the <a> tag to call a function that then changes the background for the parent <td>.

Side note: In the future, a great way to do this with CSS would be with a :parent selector, which, unfortunately, does not currently exist.

EDIT: If you're certain the <td> will only contain the anchor tag, you could just make the :hover apply to the <td>. Then changing backgrounds is no problem.

<tr class="menuBarBottomSelected">
    <td class="menuBarBottomUnselected" width="20%">
        <a href="./url.php">Getting&nbsp;Started</a>
    </td>
</tr>

and...

tr.menuBarBottomUnselected td:hover{
     background-image: url('/auth/radius/submenu_bg_hover.jpg');
     background-repeat: repeat-x;
}
Community
  • 1
  • 1
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
1

couldn't you just change the background of the a, like this?

a.menuBarBottomUnselected {background-image: url('/auth/radius/submenu_bg.jpg'); }

a.menuBarBottomUnselected:hover {background-image: url('/auth/radius/submenu_bg_hover.jpg');}