Is it possible to select every other group of three in CSS? And if it is; how?
So in the sample below apply style to the 4-6 and 10-12 li
s.
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
</ul>
I know [pure] JavaScript and jQuery can do this but I am looking for a pure CSS solution if it exists.
How do I target every third element in CSS? formula (an + b) In addition to the value n being able to be any number, you can also use a formula. nth-child(3n) would affect every third child element. nth-child(3n+1) would apply to every third element starting from the first one.
To group selectors, separate each selector with a comma.
It is possible to give several items on your page the same style even when they don't have the same class name. To do this you simply list all of the elements you want to style and put a comma between each one.
A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator. There are four different combinators in CSS: descendant selector (space)
You're looking for nth-child:
ul li:nth-child(6n+4),ul li:nth-child(6n+5),ul li:nth-child(6n+6) {
background:red;
}
http://jsfiddle.net/bhlaird/utEP4/1/
You could achieve this with a single selector, using a combination of :not
and :nth-child
.
ul > li:not(:nth-child(6n+1)):not(:nth-child(6n+2)):not(:nth-child(6n+3)) {
color:blue;
}
jsFiddle here
Using that selector by itself is pretty useless, though, considering you cannot style the other elements.
ul > li:not(:nth-child(6n+4)):not(:nth-child(6n+5)):not(:nth-child(6n+6)) {
color:red;
}
Using a combination of both will allow you to style everything, see the demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With