0

I have searched and searched, but can't seem to figure out why this is not working.

Goal: set the background color to red for the first td in the first table, while not setting any background colors for the second table.

#table1 > tr > td:nth-child(1)
{
  background-color: red;  
}

/*Ignore this*/
table td{
  padding: 10px;  
  border: 1px solid black;
  border-collapse: collapse;
}
<table id='table1'>
<tr>
  <td>1</td>
  <td>2
  
    <table>
      <tr>
        <td>3</td>
        <td>4</td>
      </tr>
    </table>
    
  </td>
</tr>
</table>
Heretic Monkey
  • 11,078
  • 7
  • 55
  • 112
user7560542
  • 497
  • 1
  • 4
  • 13
  • 1
    Add a `tbody` before the `tr` in your first selector. Browsers automatically add one if you don't specify it in the HTML/ – Heretic Monkey Mar 17 '17 at 16:40

2 Answers2

0

#table1>tbody>tr>td:first-child
{
  background-color: red;  
}

table td{
  padding: 10px;  
}
<table id='table1' border=1>
<tbody>
<tr>
  <td>1</td>
  <td>2
  
    <table border=1>
      <tr>
        <td>3</td>
        <td>4</td>
      </tr>
    </table>
    
  </td>
</tr>
</tbody>
</table>
Brad
  • 6,899
  • 8
  • 35
  • 47
0

Adding <tbody> fixed it (Thanks Mike!)

#table1 > tbody > tr > td:nth-child(1)
{
  background-color: red;  
}

/*Ignore this*/
table td{
  padding: 10px;  
  border: 1px solid black;
  border-collapse: collapse;
}
<table id='table1'>
  <tbody>
    <tr>  
      <td>1</td>
      <td>2

        <table>
          <tbody>
            <tr>
              <td>3</td>
              <td>4</td>
            </tr>
          </tbody>
        </table>

      </td>  
    </tr>
  </tbody>
</table>
j08691
  • 197,815
  • 30
  • 248
  • 265
user7560542
  • 497
  • 1
  • 4
  • 13