HTML Element Style How to - Fill nth-child








Question

We would like to know how to fill nth-child.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
table tr:nth-child(even) td {<!-- w w w .j  av a  2s  .c  o m-->
  background-color: yellow;
}

table tr:nth-child(2n+1) td {
  padding-left: 10px;
}

table tr:nth-child(3n+2) td {
  color: blue;
}
</style>
</head>
<body>
  <table border="1">
    <tr>
      <td>row 1, cell 1</td>
      <td>row 1, cell 2</td>
    </tr>
    <tr>
      <td>row 2, cell 1</td>
      <td>row 2, cell 2</td>
    </tr>
    <tr>
      <td>row 3, cell 1</td>
      <td>row 3, cell 2</td>
    </tr>
    <tr>
      <td>row 5, cell 1</td>
      <td>row 5, cell 2</td>
    </tr>
    <tr>
      <td>row 6, cell 1</td>
      <td>row 6, cell 2</td>
    </tr>
    <tr>
      <td>row 7, cell 1</td>
      <td>row 7, cell 2</td>
    </tr>
    <tr>
      <td>row 8, cell 1</td>
      <td>row 8, cell 2</td>
    </tr>
  </table>
</body>
</html>

The code above is rendered as follows: