2013-10-24 24 views
1

只要我有一個規則,如果有3個div,另一個是4個div。如果x元素的Css選擇器目標

標記

//one set of rules 
<div></div> 
<div></div> 
<div></div> 

//second fules 
<div></div> 
<div></div> 
<div></div> 
<div></div> 

CSS

/* three items */ 
div:nth-child(1):nth-last-child(3), 
div:nth-child(2):nth-last-child(2), 
div:nth-child(3):nth-last-child(1) 

/* fouritems */ 
div:nth-child(1):nth-last-child(4), 
div:nth-child(2):nth-last-child(3), 
div:nth-child(3):nth-last-child(2), 
div:nth-child(4):nth-last-child(1) 

我已經按照這樣的:http://lea.verou.me/2011/01/styling-children-based-on-their-number-with-css3/但與多少運氣黯然。

+0

那麼,首先,你的第一組選擇器使用'li',而你的第二組選擇器只有'v'。如果這只是一個錯字,你可能想糾正它,再次測試一下,以確保你知道你真的嘗試了一個有效的選擇器,但它仍然無法正常工作。 – BoltClock

+1

還有其他重要的一點,比如這些是唯一的孩子,還是與其他元素混合在一起(向我們顯示您的實際標記),您測試過的瀏覽器等等。 – BoltClock

+0

:)這些都是typeos。 v的意思是'cntrl-v'的確,真正的代碼實際上並沒有使用div的 –

回答

1

在你的小提琴,以下的選擇:

.stepNav li span:nth-child(1):nth-last-child(4), 
.stepNav li span:nth-child(2):nth-last-child(3), 
.stepNav li span:nth-child(3):nth-last-child(2), 
.stepNav li span:nth-child(4):nth-last-child(1) 

匹配所有span元素當有相同的父正好4人。但是你只有一個spanli,使得每個span唯一的孩子,而不是四個之一。

什麼你希望做的是,當恰好有4個li元素樣式的span元素,所以你需要通過移動僞類:

.stepNav li:nth-child(1):nth-last-child(4) span, 
.stepNav li:nth-child(2):nth-last-child(3) span, 
.stepNav li:nth-child(3):nth-last-child(2) span, 
.stepNav li:nth-child(4):nth-last-child(1) span 

Updated fiddle,在這裏你可以看到所有span現在是黑色陰影。

+0

arghh天才!!!!它非常明顯:(我認爲你是先生,很狡猾的一個,因爲我仍然認爲最初的問題會讓更多的人接受這個問題,我想我會更新這個問題來包括這兩個問題。 –