Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get different style in nested element depending to depth level?
    primarykey
    data
    text
    <p>i have nested element and i want that every level have different style</p> <p>level 1, 5, 9, 13 ... (4n+1) style1</p> <p>level 2, 6, 10, 14... (4n+2) style2</p> <p>level 3, 7, 11, 17... (4n+3) style3</p> <p>level 4, 8, 12, 18... (4n+3) style4</p> <p><a href="http://jsfiddle.net/UBJUB/" rel="nofollow">view exemples</a></p> <p>how to get the 3rd exemple style without using many class when i have infinity level of nested ul</p> <p>html</p> <pre><code>enter code here &lt;div id="content"&gt; &lt;h1&gt;Exemple 1 neighbor ul and nth-child(an+b) pseudo class&lt;/h1&gt; &lt;div class="exemple"&gt; &lt;ul class="clearfix"&gt; &lt;li&gt; item 1 1st ul&lt;/li&gt; &lt;li&gt; item 2 1st ul&lt;/li&gt; &lt;li&gt; item 3 1st ul&lt;/li&gt; &lt;/ul&gt; &lt;ul class="clearfix"&gt; &lt;li&gt; item 1 2nd ul&lt;/li&gt; &lt;li&gt; item 2 2nd ul&lt;/li&gt; &lt;li&gt; item 3 2nd ul&lt;/li&gt; &lt;/ul&gt; &lt;ul class="clearfix"&gt; &lt;li&gt; item 1 3rd ul&lt;/li&gt; &lt;li&gt; item 2 3rd ul&lt;/li&gt; &lt;li&gt; item 3 3rd ul&lt;/li&gt; &lt;/ul&gt; &lt;ul class="clearfix"&gt; &lt;li&gt; item 1 4th ul&lt;/li&gt; &lt;li&gt; item 2 4th ul&lt;/li&gt; &lt;li&gt; item 3 4th ul&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;h1&gt;Exemple 2 nested ul and nth-child(an+b) pseudo class&lt;/h1&gt; &lt;div class="exemple"&gt; &lt;ul class="clearfix"&gt; &lt;li&gt; item 1 1st ul &lt;ul class="clearfix"&gt; &lt;li&gt; item 1 2nd ul &lt;ul class="clearfix"&gt; &lt;li&gt; item 1 3rd ul&lt;/li&gt; &lt;li&gt; item 2 3rd ul&lt;/li&gt; &lt;li&gt; item 3 3rd ul &lt;ul class="clearfix"&gt; &lt;li&gt; item 1 4th ul&lt;/li&gt; &lt;li&gt; item 2 4th ul&lt;/li&gt; &lt;li&gt; item 3 4th ul&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt; item 2 2nd ul&lt;/li&gt; &lt;li&gt; item 3 2nd ul&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt; item 2 1st ul&lt;/li&gt; &lt;li&gt; item 3 1st ul&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;h1&gt;Exemple 3 nested ul and 4 class for different colors&lt;/h1&gt; &lt;div id="exemple3"&gt; &lt;ul class="clearfix level1"&gt; &lt;li&gt; item 1 1st ul &lt;ul class="clearfix level2"&gt; &lt;li&gt; item 1 2nd ul &lt;ul class="clearfix level3"&gt; &lt;li&gt; item 1 3rd ul&lt;/li&gt; &lt;li&gt; item 2 3rd ul&lt;/li&gt; &lt;li&gt; item 3 3rd ul &lt;ul class="clearfix level4"&gt; &lt;li&gt; item 1 4th ul&lt;/li&gt; &lt;li&gt; item 2 4th ul&lt;/li&gt; &lt;li&gt; item 3 4th ul&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt; item 2 2nd ul&lt;/li&gt; &lt;li&gt; item 3 2nd ul&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt; item 2 1st ul&lt;/li&gt; &lt;li&gt; item 3 1st ul&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>css</p> <pre><code>enter code here .clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; } .clearfix:after { clear: both; } .clearfix { zoom: 1; } #content{ width:500px; margin:0 auto; } h1{text-align:center; font-size:2em;} ul {list-style: none; padding:10px; margin:10px;} ul li {color:#fff; float:left; margin:10px 30px; position:relative; white-space:nowrap;} ul ul { position:absolute; top:31px; left:-50px} ul ul ul { position:absolute; top:10px; left:116px} .exemple ul:nth-child(4n+1) { background: navy; } .exemple ul:nth-child(4n+2) { background: green; } .exemple ul:nth-child(4n+3) { background: maroon; } .exemple ul:nth-child(4n+4) { background: purple; } #exemple3 .level1 { background: navy; } #exemple3 .level2 { background: green; } #exemple3 .level3 { background: maroon; } #exemple3 .level4 { background: purple; } </code></pre>
    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.
 

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