1

Here is some mock code. Basically I have no control over the table elements ONLY the div's inside the td's. I need to be able to hover over any of the div's in the row and they all hover to the same state. Can this be done?

Fiddle: https://jsfiddle.net/sthompson/0nzszu10/

HTML:

<table>
  <tr>
    <td>
      <div class="row1 one">

      </div>
    </td>
    <td>
      <div class="row1 two">

      </div>
    </td>
    <td>
      <div class="row1 three">

      </div>
    </td>
  </tr>
</table>

CSS:

.one {
  background-color: #0000FF;
  width: 200px;
  height: 30px;
}

.two {
  background-color: #ff0000;
  width: 200px;
  height: 30px;
}

.three {
  background-color: #00FF00;
  width: 200px;
  height: 30px;
}


/*.one:hover, .two:hover, .three:hover {
  background-color: #000;
}*/

.row1:hover {
  background-color: #000;
}
kumarharsh
  • 17,121
  • 7
  • 69
  • 93

2 Answers2

3

In CSS there is no parent selector yet. Therefore, you can't do this directly.

However, you can try using :hover on the nearest common ancestor:

tr:hover .row1 {
  background-color: #000;
}

.one {
  background-color: #0000FF;
  width: 200px;
  height: 30px;
}
.two {
  background-color: #ff0000;
  width: 200px;
  height: 30px;
}
.three {
  background-color: #00FF00;
  width: 200px;
  height: 30px;
}
tr:hover .row1 {
  background-color: #000;
}
<table>
  <tr>
    <td>
      <div class="row1 one"></div>
    </td>
    <td>
      <div class="row1 two"></div>
    </td>
    <td>
      <div class="row1 three"></div>
    </td>
  </tr>
</table>

Note it's not exactly the same: if you hover the border between two cells, they will change color even if you aren't hovering any .row1.

Community
  • 1
  • 1
Oriol
  • 225,583
  • 46
  • 371
  • 457
0

I don't think that's possible using just CSS, given that you have no control / access whatsoever to the table or tr above. If you do have some access (or can say for sure that the divs will be wrapped in a tr, try this code:

(basically, put a rule on the grandfather tr)

tr:hover > td > div {
  background-color: black;
}

https://jsfiddle.net/zbqzu21r/


Weird idea:

You have the parent tr which you cannot control. Try making a table and nesting it inside the td. I'm assuming you can easily control anything done on this table. So, put your selectors on this table, and be done with it.

<tr>
  <td>
    <table class="mytable">
      <tr>
         <td><div class="row1 one"></div></td>
      </tr>
      <tr>
         <td><div class="row1 two"></div></td>
      </tr>
      <tr>
         <td><div class="row1 three"></div></td>
      </tr>
    </table>
  </td>
</tr>

CSS:

.mytable:hover tr > td > .row1 {
  background-color: black;
}
kumarharsh
  • 17,121
  • 7
  • 69
  • 93