If you know the number of elements before the first .el
one, you can target only the .el
ones by writing 2n+x
for odd and 2n+y
for even, where x is that number plus 1 and y is the number plus 2.
So if you have 2 elements, you get 2n+3
for odd and 2n+4
for even.
#content {
background:rgb(200,200,200);
}
.el {
height:20px;
background:grey;
margin-bottom:10px;
}
.el:nth-child(2n+3) { /* the number is the amount of unstyled elements before the first .el plus 1 */
background:green;
}
.el:nth-child(2n+4) { /* and here, the amount plus 2 */
background:red;
}
<div id="content">
<div>nothing</div> <!-- note, two elements inserted -->
<div>nothing</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
</div>
Edit: if you don't know the number of inserted elements, a possible solution might be to put all the .el
divs into a container of their own.
#content {
background: rgb(200, 200, 200);
}
.el {
height: 20px;
background: grey;
margin-bottom: 10px;
}
/* Note: more specific selector */
#content > div > .el:nth-child(odd) {
background: green;
}
#content > div > .el:nth-child(even) {
background: red;
}
<div id="content">
<div>nothing</div> <!-- note, two elements inserted -->
<div>nothing</div>
<div> <!-- and a new container for the els -->
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
</div>
</div>
Edit 2: If that's not possible, one other possibility could be to target each individual .el
separately. For instance, :not(.el) + .el
targets the first .el
, :not(.el) + .el + .el + .el
targets the third, etc.
The restriction here is that you must know beforehand how many .el
elements there will be at most. In this example, it will work for up to 10. If there may be more, you will have to use more (and longer) selectors.
#content {
background:rgb(200,200,200);
}
.el {
height:20px;
background:grey;
margin-bottom:10px;
}
#content .el {
background:red;
}
#content :not(.el) + .el,
#content :not(.el) + .el + .el + .el,
#content :not(.el) + .el + .el + .el + .el + .el,
#content :not(.el) + .el + .el + .el + .el + .el + .el + .el,
#content :not(.el) + .el + .el + .el + .el + .el + .el + .el + .el + .el
{
background:green;
}
<div id="content">
<div>nothing</div> <!-- note, two elements inserted -->
<div>nothing</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
</div>
And yet another solution could be to start counting at the end, by using nth-last-child
.
This one has as a restriction that you need to know if the number of .el
divs is odd or even. This example assumes it's even (because there were 6 in the original code); if it's an odd number you will have to change the styles around.
#content {
background:rgb(200,200,200);
}
.el {
height:20px;
background:grey;
margin-bottom:10px;
}
.el:nth-last-child(even) {
background:green;
}
.el:nth-last-child(odd) {
background:red;
}
<div id="content">
<div>nothing</div> <!-- note, two elements inserted -->
<div>nothing</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
<div class="el">Test 1</div>
</div>