2015-02-09 79 views

回答

1

可以按如下方式與CSS nth child selector做到這一點:

#menu-default li:nth-child(1) a { 
    color: green; 
} 
#menu-default li:nth-child(2) a { 
    color: red; 
} 
#menu-default li:nth-child(3) a { 
    color: yellow; 
} 
#menu-default li:nth-child(4) a { 
    color: orange; 
} 
#menu-default li:nth-child(5) a { 
    color: blue; 
} 
+0

@hungerstar答案已經更新對不起 – 2015-02-09 16:01:55

0

,可以在列表元素上使用:nth-child。添加到您的css文件

#menu-default > li:nth-child(1) > a { color: green; } 
#menu-default > li:nth-child(2) > a { color: red; } 
#menu-default > li:nth-child(3) > a { color: yellow; } 
#menu-default > li:nth-child(4) > a { color: white; } 
#menu-default > li:nth-child(5) > a { color: orange; } 
1

每這些菜單項中有一個獨特的ID和匹配類,li.menu-item-39li#menu-item-39。您可以使用CSS來定位這些ID或類,無論哪個更適合您。

li.menu-item-39 > a { /* Home */ 
    color: green; 
} 
li.menu-item-43 > a { /* News */ 
    color: red; 
} 
li.menu-item-47 > a { /* Blog */ 
    color: yellow; 
} 

編輯,以解決其他答案:可以使用:nth-child僞類要做到這一點,但只會在支持CSS3瀏覽器。

+2

我覺得應該注意的是,雖然這種解決方案具有更大的瀏覽器兼容性,它可能比'不靈活第n-child' 。如果用戶在某個時候改變了菜單項,那麼'menu-item- *'類將會改變。即'menu-item-46' =>'menu-item-67'。 – hungerstar 2015-02-09 16:04:08

+1

好點,hungerstar。可以使用下面的CSS2選擇器來填充':nth-​​child'僞類:'#menu-default li:first-child> a'等價於'#menu-default li:nth-​​child(1)> '#menu-default li:first-child + li> a'等價於'#menu-default li:nth-​​child(2)> a','#menu-default li:first-child + li + li> a'相當於'#menu-default li:nth-​​child(3)> a',_etc._ – 2015-02-09 16:09:11

-2

您需要進入navigation.php文件,我假設併爲每個參數添加css id參數,然後在styles.css中爲它們分配顏色。

如果您不熟悉編輯文件或更改代碼,這不是一個簡單的修復。

下面是一些CSS代碼:

#menu-default > li:nth-child(1) > a { color: green; } 
#menu-default > li:nth-child(2) > a { color: red; } 
#menu-default > li:nth-child(3) > a { color: yellow; } 
#menu-default > li:nth-child(4) > a { color: white; } 
#menu-default > li:nth-child(5) > a { color: orange; } 
+0

如果您可以訪問您的css文件或者可以通過WordPress管理員進行編輯,這不是一個難題。 。不知道你爲什麼提到navigation.php。 WordPress爲您添加每個「li」的ID。你正在使這件事情變得比需要的複雜。 – hungerstar 2015-02-09 16:07:07

+0

它是如果你沒有編輯WP文件的經驗。你假設他有一個正確編碼的主題。 – kensavage 2015-02-09 16:15:45