HTML Element Style How to - Style nth child div








Question

We would like to know how to style nth child div.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
div.pair:nth-of-type(2) {<!--  w w w  . jav  a  2  s.  c om-->
  color: red;
  background: #C0C0C0;
}

div.pair:nth-of-type(3) {
  color: green;
  background: #E0E0E0;
}
</style>
</head>
<body>
  <div class="h2 colors">Title: Colors</div>
  <div class="pair">Hello world</div>
  <div class="pair">Hello world</div>
  <div class="pair">Hello world</div>
  <div class="pair">Hello world</div>
</body>
</html>

The code above is rendered as follows: