tags:

views:

219

answers:

4

I have a webpage that displays a calendar, and I want to change the background-color of any td that has their day number linked, so when I hover over that day, it changes the background-color.

I thought this would work:

.main-calendar td {
    width:14%;
    height:100px;
    text-align:center;
    border:1px solid #000;
    font-size:20px;
    vertical-align:middle;
    display:block;
}

table.main-calendar td a:hover,
table.main-calendar td a:visited {
    display:block;
    text-decoration:none;
    background-color:red;
    color:#fff;
}

table.main-calendar td a:link {
    display:block;
    text-decoration:none;
    background-color:green;
    color:#fff;
}

Any help is appreciated.

A: 

Have you tried

table.main-calendar td:hover a

?

What does Firebug say?

Pekka
td:hover would require additional Javascript. I'd look into jQuery -- it's VERY easy to do this. http://webdevel.blogspot.com/2006/10/jquery-tip-highlight-row-on-hover.html
Nathan Loding
Only if you are determined to achieve a cosmetic effect on IE6. Adding 16k of JS for that really isn't worth it.
David Dorward
16k of javascript... loaded from MS or Google for free... and it's only loaded once...
Nitrodist
Only once... per page?
ANeves
@sr pr, only once per cache, assuming your visitors have a copy of the file -visited a site that uses the same Google/whoever-hosted file- they won't have to download it at all.
David Thomas
sr pt, no, only once until they clear their cache. The formula might look like this for bandwidth downloaded for the client (16k + number of times visiting your web site * those pages). Like ricebowl mentioned, the js might already be loaded from another website (not your own) since many websites use MS's cache or Google's. The only downside for server is serving the extra JS linking code and the functions themselves.
Nitrodist
+3  A: 

looks to me like you should just change the order of the last two rules. All other things being equal, you should declare :link before :hover and :visited, otherwise it gets over-ruled by the cascade.

graphicdivine
The order of `:link` and `:visited` doesn't matter (since they are mutually exclusive), but the order of `:link`+`:visited`, `:hover`, `:focus` and `:active` is significant.
David Dorward
A: 

Your code will only set the background colour of the link, not of the whole table cell, because the CSS matches the <a> elements (not their <td> parents). In general there's no way in CSS to select an element based on it's children (see http://stackoverflow.com/questions/45004/complex-css-selector-for-parent-of-active-child), so you although you can (assuming you don't care about IE6) write a rule for

table.main-calendar td:hover

and have it highlight the cell when you hover over it, you can't work out whether or not this cell contains a link (visited or otherwise).

So you'll need to go for an alternative approach, either making the link take up the entire table cell (if there's only one link per cell) or using some sort of JavaScript.

Bruce
It already has code to make the link fill the cell.
David Dorward
A: 

Note that in IE6, the hover property is only recognized on anchor tags. So be careful using hover on non-anchors to communicate essential information. If it is non-essential, go ahead and apply hover to the td.

kingjeffrey