0

I have this table that I want to change color when hovered, but will also change different color when a link is inside the TD.

<table>
    <tr>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
    </tr>

    <tr>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
    </tr>

    <tr>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>
            <a>HOVERING THIS LINK WILL MAKE WHOLE TR BG COLOR BLUE</a>
        </td>
    </tr>
</table>

I've tried in CSS

table tr:hover:not(table td a) {
    background-color: red !important;
}

table tr:hover{ 
    background-color: blue;
}

But I have no luck. Is this possible without using Javascript/Jquery?

MJ Doe
  • 21
  • 2

2 Answers2

1

you may use pseudo elements layed under tr that will be used to draw the backgrounds.

z-index will manage to pile them under contents.

example:

table tr {
  position: relative;
}

table tr a:before,
tr:before {
  content: '';
  position: absolute;
  float:left;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: -2;
}
table tr:hover:before {
  background: red;  
}

table tr a:hover:before {
  z-index: -1;
  background-color: blue;
}

/*and  eventually: */a {display:block;background:rgba(0,0,0,0.0001);}a:hover {color:white;}
<table>
    <tr>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
    </tr>

    <tr>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
    </tr>

    <tr>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>Hovering this part will make the whole TR bgcolor red</td>
        <td>
            <a>HOVERING THIS LINK WILL MAKE WHOLE TR BG COLOR BLUE</a>
        </td>
    </tr>
</table>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
0

It's unusual that you do it without some javascript. But if you really want to do it, you can check this questions out and try it by yourself.

Question 1

Question 2

Hope I could help...

Community
  • 1
  • 1
  • those questions/answers are about sibblings, not parents. it won't help :( Your answer looks much more like a comment than else ;) – G-Cyrillus Apr 12 '17 at 12:10
  • @GCyrillus Can't comment because of my reputation points :) But thanks for correcting my answer. Obviously you're right ;) – CallMeLeonardo Apr 12 '17 at 12:13