', always starting with the same color, means that the first line after group / subgroup starts with red
If so, you can set the background-color first .row red, and the rest magenta :
.group ~ .row { background-color: magenta; } .group + .row { background-color: red; }
Jsbin demo
These selectors are called Common Word ~ and Combined Adjacent Block + , you can find more details here .
Update
All new CSS3 selectors, such as :nth-child(n) :nth-of-type(n) correspond to every element that is the n child or type of its parent .
Thus, the only way to achieve this is by .row in each block:
<div class="group">This is a group</div> <div class="group subgroup">This is a subgroup</div> <div class="wrap"> <div class="row">This is the first row</div> <div class="row">This is the second row</div> <div class="row">This is the third row</div> <div class="row">This is the forth row</div> </div>
And selecting odd and even lines based on their position in .wrap er (their parent):
.row:nth-child(odd) { background-color: red; } .row:nth-child(even) { background-color: magenta; }
JSBin Demo # 2
source share