2015-10-05 66 views
0

所以我在html中有一些有序列表的代碼,並且我正在嘗試爲添加的每個深度添加不同顏色的樣式表。在CSS樣式表中更改列表的顏色

例如:

<ol> 
<li>This is red</li> 
    <ol> 
    <li>This is blue</li> 
    </ol> 
</ol> 

如何做到這一點與CSS樣式表?

試過這樣的事情:

ol.a { 
color:blue; 
} 
ol.b { 
color:red; 
} 

顯然沒有奏效,否則我就不會在這裏了。

回答

1

答案很簡單:

ol { 
color:blue; 
} 
ol ol { 
color:red; 
} 
2

下面是正確的CSS

ol li { 
color:blue; 
} 
ol li ol li { 
color:red; 
} 
+0

我的版本是簡單的,因爲它不指'li'元素 – Nayuki

4

HTML:

<ol class="a"> 
<li>This is red</li> 
    <ol class="b"> 
    <li>This is blue</li> 
    </ol> 
</ol> 

CSS:

ol.a { 
color:red; 
} 
ol.b { 
color:blue; 
} 
1

您可以在醇應用CSS這樣 -

ol > ol:nth-child(2) { 
 
    background-color:red; 
 
} 
 

 
ol > ol:nth-child(3) { 
 
    background-color:blue; 
 
} 
 

 
ol > ol:nth-child(4) { 
 
    background-color:green; 
 
}
<ol> 
 
<li>This is red</li> 
 
<ol> 
 
    <li>This is blue</li> 
 
</ol> 
 
<ol> 
 
    <li>This is blue</li> 
 
</ol> 
 
    <ol> 
 
    <li>This is blue</li> 
 
</ol> 
 
    
 
</ol>