views:

125

answers:

3

I have a table where:

  • The rows and cells normally have no background specified; the table's background shows through.
  • Rows with the :hover pseudo-class have a background color to highlight them -- e.g., to track rows as the mouse moves over the table (in IE7 and anything else modern).
  • Some cells need to be highlighted with a different background color (and various other styles); these have their own class.

The problem is that I want the special cells not to be special when the row is being highlighted by the hover; it looks really strange. I need to support IE7 (thankfully not 6!) and naturally want to support Chrome, Firefox, Safari, and most other modern browsers.

Here are trimmed-down versions of the relevant rules:

table.status
{
    border-collapse:  collapse;
    background-color: #CDD8ED;
}
table.status tr:hover
{
    background-color: #FAF0BD;
}
/* "down" cells are special */
table.status td.down
{
    background-color: #D22;
    color:            white;
}

Things I've tried:

  • The :not pseudo-class from CSS3: table.status tr:not(:hover) td.down. Works in Chrome and Firefox, not in IE7.
  • A more-specific rule (e.g., table.status tr:hover td.down) that reiterates the properties using the keyword inherit. (Blech, fragile, have to be sure when adding styles to the one rule that you add inherit styles to the other rule.) Again works in Chrome and Firefox, not in IE7.

Things I'd really rather not do:

  • A more-specific rule setting the relevant styles so they match the styles of the table. Blech, even more fragile than the inherit thing above. But functional.
  • Use JavaScript for the hover effect (although that would have the advantage of supporting IE6 as well). Hovers in JavaScript tend to have more overhead than built-in ones.

Is there another CSS solution?

FWIW, here's my test page:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Hover Test Page</title>
<style type='text/css'>
body
{
    font-family: sans-serif;
}
table.status
{
    border-collapse: collapse;
    background-color: #CDD8ED;
}
table.status th
{
    text-align: left;
    background-color: #14429E;
    color: #FEFEFE;
}
table.status td, table.status th
{
    padding: 2px 0.5em;
}
table.status tr:hover
{
    background-color: #FAF0BD;
}
/*table.status tr:not(:hover) td.down*/
table.status td.down
{
    background-color:   #D22;
    color:              white;
    font-weight:        bold;
}
/*table.status tr:hover td.down
{
    background-color:   inherit;
    color:              inherit;
    font-weight:        inherit;
}*/
</style>
</head>
<body>
<table class='status'>
    <thead>
        <tr>
            <th>Server</th>
            <th>Details</th>
            <th>www</th>
            <th>mail</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Server1</td>
            <td>blah blah blah</td>
            <td class='up'>Up</td>
            <td class='up'>Up</td>
        </tr>
        <tr>
            <td>Server2</td>
            <td>blah blah blah</td>
            <td class='up'>Up</td>
            <td class='down'>Down</td>
        </tr>
        <tr>
            <td>Server3</td>
            <td>blah blah blah</td>
            <td class='sched'>Down (scheduled)</td>
            <td class='sched'>Down (scheduled)</td>
        </tr>
        <tr>
            <td>Server4</td>
            <td>blah blah blah</td>
            <td class='up'>Up</td>
            <td class='up'>Up</td>
        </tr>
    </tbody>
</table>
</body>
</html>
A: 

I am not sure that I understand you.... but I think this could work:

table.status td.down, table.status td.down:hover
{
    background-color: #D22;
    color:            white;
}
Allan Kimmer Jensen
Thanks. That would actually be the opposite of what I want to do; I want that rule to *not* apply to the cells when they're being hovered.
T.J. Crowder
I am afraid this cannot be done in CSS 2.1... But then again i am not sure. I would "overwrite" the styles again in .status tr:hover .down.
Allan Kimmer Jensen
Thanks again. (Not my downvote, btw.) If I have to, I have to, was just hoping for something less fragile.
T.J. Crowder
No problem, PS i did not downvote you... People with less with 100 points cannot donwvote.
Allan Kimmer Jensen
Oh, I didn't think you did. It's not the down-votes, it's the *not knowing why* that bothers me. I've only asked a couple of questions (I've answered hundreds), if I'm doing it wrong, I want to know how so I can do it right in the future.
T.J. Crowder
Good :) Because i think it is a good question, and i think that is why they support it in CSS3. Have a nice day!
Allan Kimmer Jensen
A: 

What's wrong with simply removing the color white from the special cells when you are hovering over the table row? (Not tested - only have IE6 at work).

/* "down" cells are special, but when hovered make them look like the others */

table.status tr:hover td.down
{ 
    background-color: #FAF0BD; 
    color:            black; 
} 

This might be more effective if you had a complete doctype. e.g.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;

Edit: Nevermind - didn't read the full question - good luck with that!

Traingamer
Thanks anyway. Re complete doctype: That **is** a complete doctype: http://dev.w3.org/html5/spec/syntax.html#the-doctype
T.J. Crowder
+1  A: 

Since you used border-collapse on the table, you can achieve what you want by doing this:

table.status tr:hover td
{
    background-color: #FAF0BD;
}

..instead of..

table.status tr:hover
{
    background-color: #FAF0BD;
}

I understood your question as you want the tr:hover rules to have priority over the special cells. In this case, in your markup, these are the cells with class="down", correct? Since you're using border-collapse, the background colors of the <td>'s blend together within a <tr>. Hope this helps.

nickelleon
Thanks, that improves things **markedly**. Clever trick for the background color, it totally handles that part of things without requiring duplicated information. (I wouldn't have thought the hover rule was more specific for the cells than the class rule, since the hover related to the row, but apparently so -- learned something there.) It doesn't handle the other cases (`color` and `font-weight`, although I've dropped `font-weight` because of the resizing effects it causes), any similarly-clever ideas there? Or is it a case of waiting for `:not` to be more widely-supported?
T.J. Crowder
@All: Looks like this is as close as we're going to get until `:not` is well-supported.
T.J. Crowder