Change background color of every 2nd and 3rd and 4th element - HTML CSS CSS Property

HTML CSS examples for CSS Property:background-color

Description

Change background color of every 2nd and 3rd and 4th element

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <style id="compiled-css" type="text/css">

.item:nth-child(4n-7) {<!-- ww w  .j  a va  2s . c  om-->
   background-color:green;
}
.item:nth-child(4n-6) {
   background-color:red;
}
.item:nth-child(4n-5) {
   background-color:blue;
}
.item:nth-child(4n-4) {
   background-color:black;
}
.item{
   width:100%;
   height:100px;
   margin-bottom:10px;
}


      </style> 
 </head> 
 <body> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div> 
  <div class="item"> 
  </div>  
 </body>
</html>

Related Tutorials