Yes, you can do this with nth-of-type . Set all of them to background-color: red , then you can target each .some-class starting from the 1st and rewrite the div color to blue:
.some-class div{ background-color: red; } .some-class:nth-of-type(1n+2) div{ background-color: blue; }
Fiddle
UPDATE FOR ckuijjer
Who said: don't think this is a solution. The question is can you color the child <div> a red if there it the only child and blue if there are two children. don't think this is a solution. The question is can you color the child <div> a red if there it the only child and blue if there are two children.
Yes, you can. Here's how:
.some-class:nth-of-type(1n+2) div:first-child{ background-color: red; } .some-class:nth-of-type(1n+2) div:nth-of-type(1n+2){ background-color: blue; }
Fidddle
The bottom line is how to do it, regardless of what the OP tried to portray in his / her explanation, and regardless of whether he or anyone else understood what he / she really wanted, the answer (name of the question : Is it possible to do this with only CSS? ): Yes, this can only be done using CSS. You just need the right equation.
jmore009
source share