Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery set alternating color rows

I'm looking for a specific answer to my question. And this is, I have a row containing a quantity of divs (is dynamically set). What I want is alternately the background of divs (3 divs) are set to another color like this: enter image description here Can this be done by the CSS odd and even pseudo classes or should this be done through jQuery?

like image 879
Ramirez Avatar asked Jan 18 '26 06:01

Ramirez


2 Answers

You can do this by adding this to your CSS.. foo:nth-child(n+3)

UPDATED:

ul li:nth-child(6n+4),
ul li:nth-child(6n+5),
ul li:nth-child(6n+6) {
  border: 1px solid red;
}

^ Altough I'm not a fan, it works.

http://codepen.io/pacMakaveli/pen/JdWYoM

like image 122
Vlad Avatar answered Jan 19 '26 20:01

Vlad


You can do by using:

li:nth-child(6n),
li:nth-child(6n - 1),
li:nth-child(6n - 2) {background: red;}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
  <li>Item 7</li>
  <li>Item 8</li>
  <li>Item 9</li>
  <li>Item 10</li>
  <li>Item 11</li>
  <li>Item 12</li>
  <li>Item 13</li>
  <li>Item 14</li>
  <li>Item 15</li>
</ul>
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
  <li>Item 7</li>
  <li>Item 8</li>
  <li>Item 9</li>
  <li>Item 10</li>
</ul>
like image 41
Praveen Kumar Purushothaman Avatar answered Jan 19 '26 18:01

Praveen Kumar Purushothaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!