3

Let me give an example to clarify my question.

Assume I have a table like below:

<table>
  <tr>
    <th>header1</th>
    <th>header2</th>
  </tr>

  <tr>
    <td>elem1</td>
    <td>elem2</td>
  </tr>

  <tr>
    <td>elem3</td>
    <td>elem4</td>
  </tr>
</table>

I want to show that table row containing <td> element is clickable, so I'm trying to change the mouse pointer cursor to hand when it hovers on <tr> that contains <td> element.

I can do that by setting the style like below:

table tr:not(:first-child) {cursor:pointer;}

The first child of a table is normally the <tr> that contains <th>.

Here comes the question, how to write the css style to eliminate <tr> that contains <th> as direct child with more intuitive way?

Douwe de Haan
  • 5,038
  • 1
  • 24
  • 37
Eugene
  • 8,507
  • 3
  • 35
  • 55
  • Possible duplicate of [Is there a CSS parent selector?](https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – Jon P Apr 23 '18 at 07:26

5 Answers5

3

tr td {cursor: pointer} will do just fine:

tr td {cursor: pointer}
<table>
  <tr>
    <th>header1
    <th>header2
  </tr>
  <tr>
    <td>elem1
    <td>elem2
  </tr>
  <tr>
    <td>elem3
    <td>elem4
  </tr>
</table>

Or just td {cursor: pointer} is fine too.

VXp
  • 10,307
  • 6
  • 25
  • 40
3

Make use of the thead and tbody element, and then use CSS to only target the rows in the tbody:

table tbody tr {
  cursor: pointer;
}
<table>
  <thead>
    <tr>
      <th>header1</th>
      <th>header2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>elem1</td>
      <td>elem2</td>
    </tr>
    <tr>
      <td>elem3</td>
      <td>elem4</td>
    </tr>
  </tbody>
</table>
Douwe de Haan
  • 5,038
  • 1
  • 24
  • 37
1

You can add class to your editable tr:

tr.editable {
  cursor: pointer;
}
<table> 
   <tr>
     <th>header1</th>
     <th>header2</th>
   </tr> 
   <tr class="editable">
     <td>elem1</td>
     <td>elem2</td>
   </tr>
   <tr class="editable">
     <td>elem3</td>
     <td>elem4</td>
   </tr> 
</table>
Ambroży
  • 75
  • 4
1

You may also use CSS pseudo-class:

It will select from 2nd to last row in your table.

table tr:nth-child(n+2) {
   cursor: pointer;
}
<table> 
   <tr>
     <th>header1</th>
     <th>header2</th>
   </tr> 
   <tr class="editable">
     <td>elem1</td>
     <td>elem2</td>
   </tr>
   <tr class="editable">
     <td>elem3</td>
     <td>elem4</td>
   </tr> 
</table>
Ambroży
  • 75
  • 4
  • It could be a solution, but it might not be a good idea to couple CSS style with the element appeared sequence. – Eugene Nov 11 '19 at 03:21
-3

You could simply refer to all tr that have as their child <td> it will automatically omit the tr with <th> tags

 table tr td {
    cursor:pointer;
 }
GGO
  • 2,700
  • 3
  • 16
  • 35
VivekG
  • 1
  • 2