4

I want to apply CSS on $100 that is a div element inside a table. It should be red in color, but my CSS selector does not work.

<!DOCTYPE html>
    <html>
    <head>
    <style>
    table { 
      display: table;
      border-collapse: separate;
      border-spacing: 2px;
      border-color: gray;
    }
    
    .new > div{
    color:red;
    }
    
    </style>
    </head>
    <body>
    
    <table class="new">
      <tr>
        <th>Month</th>
        <th>Savings</th>
      </tr>
      <tr>
        <td>January</td>
        <td>
        <div>$100</div>
        </td>
      </tr>
      <tr>
        <td>February</td>
        <td>$80</td>
      </tr>
    </table>
    
    </body>
    </html>
weegee
  • 3,256
  • 2
  • 18
  • 32
Kunvar Singh
  • 1,779
  • 2
  • 13
  • 21

4 Answers4

3

You can do something like this:

table.new td div{
color:red;
}

It will change the css of Div inside a td tag.

Shiv Shankar
  • 1,007
  • 2
  • 8
  • 13
1
.new div{
  color:red;
}

simple change or add class to div

Jeon
  • 97
  • 1
  • 7
0

First option is to use table tr td div (select div, that is inside a td, inside tr, inside table in that order).

Second option would be to use a class. This way you can reuse the same style on whatever element you need to by just adding the same class.

The reason why .new > div you used does not work is that > selector is used for selecting direct child element only. Since div is not directly inside the table element it will not be selected.

table tr td div {
  color:red;
}

.red-text {
  color:red;
}
<!DOCTYPE html>
<html>
<head>
<style>
table { 
  display: table;
  border-collapse: separate;
  border-spacing: 2px;
  border-color: gray;
}

.new > div{
color:red;
}

</style>
</head>
<body>

<table class="new">
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>
    <div>$100</div>
    </td>
  </tr>
  <tr>
    <td>February</td>
    <td class="red-text">$80</td>
  </tr>
</table>

</body>
</html>
Cray
  • 2,774
  • 7
  • 22
  • 32
0

You can do it by following ways:

1) Using Inline CSS

<div style="color:red;">$100</div>

2) Using Internal CSS.

In this method, we will first assign a class to the div which contains $100. Then we will give CSS to the class in <style> tag.

<style>
.red
{
color:red;
}
</style>


<div class="red">$100</div>

Following is the code using 2nd method:

<!DOCTYPE html>
    <html>
    <head>
    <style>

table { 
      display: table;
      border-collapse: separate;
      border-spacing: 2px;
      border-color: gray;
    }
    
    .new > div{
    color:red;
    }
    .red
    {
    color:red;
    }
    
  </style>
    </head>
    <body>
    
    <table class="new">
      <tr>
        <th>Month</th>
        <th>Savings</th>
      </tr>
      <tr>
        <td>January</td>
        <td>
        <div class="red">$100</div>
        </td>
      </tr>
      <tr>
        <td>February</td>
        <td>$80</td>
      </tr>
    </table>
    
    </body>
    </html>
Vintage Coders
  • 162
  • 1
  • 4