2

I would like to apply some styling only to the first TD of a TABLE with a specific class. I am using:

.class td:first-child {
    background-color: red;
}

But If there are nested TD they get styled too.

How can I apply this only to the first TD?

See JSfiddle here (I would like test1 to get red, but not test4)

MeV
  • 3,761
  • 11
  • 45
  • 78

4 Answers4

3

Unless you want to use classes or something, you could use the direct child selector:

.pretty > tbody > tr > td:first-child {
    background-color: red;
}
dsgriffin
  • 66,495
  • 17
  • 137
  • 137
2

Child selector should work with the addition of the tr:first-child so your only selecting the first row also.

.pretty > tbody > tr:first-child > td:first-child {
    background-color: red;
}
<table id="logtable" class="pretty">
    <thead>
        <tr>
            <th>Time</th>
            <th>From</th>
            <th>To</th>
            <th>Payload</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>test1</td>
            <td>test2</td>
            <td>test3</td>
            <td>
                <table>
                    <tr>
                        <td>test4</td>
                        <td>test5</td>
                    </tr>
                </table>
            </td>
         </tr>
         <tr>
             <td>test1</td>
             <td>test2</td>
             <td>test3</td>
             <td>
                 <table>
                     <tr>
                         <td>test4</td>
                         <td>test5</td>
                     </tr>
                 </table>
             </td>
         </tr>
         <tr>
             <td>test1</td>
             <td>test2</td>
             <td>test3</td>
             <td>
                 <table>
                     <tr>
                         <td>test4</td>
                         <td>test5</td>
                     </tr>
                 </table>
             </td>
         </tr>
     </tbody>
</table>
John Slegers
  • 45,213
  • 22
  • 199
  • 169
Aaron
  • 10,187
  • 3
  • 23
  • 39
  • Ive added more table rows so you can see the result of using the `:first-child` pseudo selector on the row. – Aaron Jan 25 '16 at 17:10
  • amazing. do you have an idea how to do this in SASS? – MeV Jan 25 '16 at 17:12
  • Unless you need to style each of those elements individually then I don't think you would need to do it any differently for SASS. – Aaron Jan 25 '16 at 17:14
1

You could use the child selector, >

.pretty > tbody > tr > td:first-child {
  background-color: red;
}

.pretty > tbody > tr > td:first-child {
  background-color: red;
}
<table id="logtable" class="pretty">
  <thead>
    <tr>
      <th>Time</th>
      <th>From</th>
      <th>To</th>
      <th>Payload</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>test1</td>
      <td>test2</td>
      <td>test3</td>
      <td>
        <table>
          <tr>
            <td>test4</td>
            <td>test5</td>
          </tr>
        </table>
      </td>
    </tr>
  </tbody>
</table>
j08691
  • 204,283
  • 31
  • 260
  • 272
1

This should do the trick :

.pretty > * > tr > td:first-child {
    background-color: red;
}
<table id="logtable" class="pretty">
    <thead>
        <tr>
            <th>Time</th>
            <th>From</th>
            <th>To</th>
            <th>Payload</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>test1</td>
            <td>test2</td>
            <td>test3</td>
            <td>
                <table>
                    <tr>
                        <td>test4</td>
                        <td>test5</td>
                    </tr>
                </table>
            </td>
         </tr>
         <tr>
             <td>test1</td>
             <td>test2</td>
             <td>test3</td>
             <td>
                 <table>
                     <tr>
                         <td>test4</td>
                         <td>test5</td>
                     </tr>
                 </table>
             </td>
         </tr>
         <tr>
             <td>test1</td>
             <td>test2</td>
             <td>test3</td>
             <td>
                 <table>
                     <tr>
                         <td>test4</td>
                         <td>test5</td>
                     </tr>
                 </table>
             </td>
         </tr>
     </tbody>
</table>

See also this Fiddle.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • @MaRco85 : If you want to restrict your selection to only `td` elements that are part of the tbody, replace the `*` with `tbody`. If you want your code to apply to `th` elements as well, just replace `td` with `th`. etc. – John Slegers Jan 25 '16 at 17:41