2012-10-12 54 views
1

方案你可以用nth-child選擇最後3個項目並選擇奇數還是偶數?

例如說我有以下幾點:

1 | 2 | 3 
4 | 5 | 6 
7 | 8 | 9 

<ul> 
    <li>1</li><li>2</li><li>3</li> 
    <li>4</li><li>5</li><li>6</li> 
    <li>7</li><li>8</li><li>9</li> 
</ul> 

我有奇數和偶數默認背景:

li:nth-child(even) { 
    background: #ff0000; 
} 

li:nth-child(odd) { 
    background: #00ff00; 
} 

問題

但我想要爲拉斯維加斯應用不同的背景t 3結果,也取決於它們是偶數還是偶數:

li:nth-last-child(-n + 3)(even) { 
    background: #0000ff; 
} 

li:nth-last-child(-n + 3)(odd) { 
    background: #ffff00; 
} 

這可能嗎?

回答

4

你需要重複:nth-child部分以及:

li:nth-last-child(-n + 3):nth-child(even) { 
    background: #0000ff; 
} 

li:nth-last-child(-n + 3):nth-child(odd) { 
    background: #ffff00; 
} 

此外,這取決於你有多少個元素都有,:nth-child(odd):nth-last-child(odd)(以及他們的even同行)可以選擇不同的元素。

+0

在第十一個孩子(或具有特定班級的孩子)之後是否可以選擇偶數和奇數? – Venugopal

+0

@Venu Gopal:你可以使用'~'組合器來選擇下面的兄弟姐妹,但它取決於你的意思是「偶數」和「奇數」。您可能想發佈一個新問題。 – BoltClock

+0

@ boltclocks-a-unicorn請參閱[這裏](http://stackoverflow.com/questions/20564695/select-even-and-odd-childs-of-a-particular-class-after-a-specific-class-兒童) – Venugopal

相關問題