As stated by others, this is not possible in pure CSS. However using js it is quite possible and fairly easy too.
For ease I implemented this in jQuery but you could do with pure JS.
http://jsfiddle.net/sg3s/Suf3p/
I basically made a small jQuery plugin that colors the selector you apply it on with the primary color, and uses the subselect to get a matching child to color with the secondary color and so on until no children matching the subselect are left.
jQuery(function($) {
$.fn.alternateNestedBgColor = function(subselect, colors) {
// While not a great optimization, length of the colors array always stays the same
var l = colors.length;
// Itterate over all element in possible array
// jQuery best practice to handle initializing multiple elements at once
return this.each(function() {
var $sub = $(this), i = 0;
// Executes code, at least once
do {
// Set bg color for current $sub element
$sub.css('backgroundColor', colors[i++ % l]);
// Set $sub to direct children matching given selector
$sub = $sub.children(subselect);
// Will repeat the do section if the condition returns true
} while ($sub.length > 0);
});
};
// target first div in the body
// first argument = child selector
// second argument = array list of colors
$('body>div').alternateNestedBgColor('div', ['red', 'green', 'blue', 'purple', 'grey']);
});
Update As requested an update detailing how apply
and modulo
were used.
It's been almost 2 year since I recently posted this. And while working, the solution I made back then was a bit verbose and confusing, as for instance, I never needed apply
. I got a little bit more comfortable with scopes, so I revised the function to be much simpler.
The only situation where apply
is useful is when you need to pass a value to the this
variable inside the function scope. Unless working with classes there aren't a whole lot of situations you should have a need for apply
or call
. If you want to read up on it I would like to refer you to this answer which explains it in context of classes. The MDN link is a good resource as well (for this and other javascript constructs/concepts).
As for modulo
, this is basic math and this question explains the operation quite well. In short it will give you the full integer remainder after dividing a number by another. So 33 % 8 = 1
which you could write as 33-parseInt(33/8)*8
in js though that would be grossly inefficient. The result of the operation will always be 0 (when the number perfectly divides) to the 2nd argument minus 1 (so 7 in my example).
0 % 3 = 0 // technically doesn't exist (as you can't divide 0 with anything) but 0 for practicality in all programming languages afaik(?)
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0 etc...
It's one of those operations which is inherently simple for your CPU, in fact without it being able to do this we wouldn't have computers.
In the javascript revision I've written the selection of the color from the given array as colors[i++ % l]
.
In writing this would be give me the remainder of i / l
and use that integer as an index for the colors
array which returns a color for use.
The ++
will add 1 only after returning the value of i
for use in the modulo, this behaviour would be reversed if I had written ++i
, but that wouldn't work for our purpose here.
For reference here is the MDN article on do...while.
I hope that cleared some things up! :)