Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As stated by others, this is not possible in pure CSS. However using js it is quite possible and fairly easy too.</p> <p>For ease I implemented this in jQuery but you could do with pure JS.</p> <p><a href="http://jsfiddle.net/sg3s/Suf3p/" rel="nofollow noreferrer">http://jsfiddle.net/sg3s/Suf3p/</a></p> <p>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.</p> <pre><code>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 &gt; 0); }); }; // target first div in the body // first argument = child selector // second argument = array list of colors $('body&gt;div').alternateNestedBgColor('div', ['red', 'green', 'blue', 'purple', 'grey']); }); </code></pre> <p><strong>Update</strong> As requested an update detailing how <code>apply</code> and <code>modulo</code> were used.</p> <p>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 <code>apply</code>. I got a little bit more comfortable with scopes, so I revised the function to be much simpler.</p> <p>The only situation where <code>apply</code> is useful is when you need to pass a value to the <code>this</code> variable inside the function scope. Unless working with classes there aren't a whole lot of situations you should have a need for <code>apply</code> or <code>call</code>. If you want to read up on it I would like to refer you to <a href="https://stackoverflow.com/questions/8659390/context-to-use-call-and-apply-in-javascript">this answer which explains it in context of classes</a>. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply" rel="nofollow noreferrer">The MDN link</a> is a good resource as well (for this and other javascript constructs/concepts).</p> <p>As for <code>modulo</code>, this is basic math and <a href="https://stackoverflow.com/questions/2664301/how-does-modulus-divison-work">this question explains the operation quite well</a>. In short it will give you the full integer remainder after dividing a number by another. So <code>33 % 8 = 1</code> which you could write as <code>33-parseInt(33/8)*8</code> 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).</p> <pre><code>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... </code></pre> <p>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.</p> <p>In the javascript revision I've written the selection of the color from the given array as <code>colors[i++ % l]</code>.</p> <p>In writing this would be give me the remainder of <code>i / l</code> and use that integer as an index for the <code>colors</code> array which returns a color for use. </p> <p>The <code>++</code> will add 1 only after returning the value of <code>i</code> for use in the modulo, this behaviour would be reversed if I had written <code>++i</code>, but that wouldn't work for our purpose here.</p> <p>For reference <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while" rel="nofollow noreferrer">here is the MDN article on do...while</a>.</p> <p>I hope that cleared some things up! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload