2013-01-18 32 views
0

正常工作,爲什麼文章的行爲不同,以div的,其中最後一個孩子而言最後孩子沒有與文章

HTML

<div class="parent"> 
<div class="example">111</div> 
<div class="example">111</div> 
<div class="example">111</div> 
<div class="example">111</div> 
</div> 

<br> 

<div class="parent2"> 
<article class="example">111</div> 
<article class="example">111</div> 
<article class="example">111</div> 
<article class="example">111</div> 
</div> 

CSS

.parent .example{ background-color: red;} 
.parent .example:last-child{background-color: yellow;} 

.parent2 .example{ background-color: red;} 
.parent2 .example:last-child{background-color: yellow;} 

DEMOhttp://jsfiddle.net/chLLa/1/

+2

因爲你使用div標籤關閉您的文章標籤?如果您修復這些錯別字,似乎工作正常。 http://jsfiddle.net/j08691/chLLa/2/ – j08691

+0

[Validators](http://validator.nu/)是你的朋友。 – steveax

+0

好的 - 對不起...其實我在我的實際代碼中仍然存在這個問題,所以我會試着重新編寫它 – byronyasgur

回答

3

您不關閉您的標籤正確

<div class="parent"> 
<div class="example">111</div> 
<div class="example">111</div> 
<div class="example">111</div> 
<div class="example">111</div> 
</div> 

<br> 

<div class="parent2"> 
<article class="example">111</article> 
<article class="example">111</article> 
<article class="example">111</article> 
<article class="example">111</article> 
</div> 
2

您正在用<div>關閉第二組<article>。從本質上講,你正在關閉你的.parent2 div太早,無法讓正確的最後一個孩子工作。

另請注意,JSFiddle.net有一個很好的「整理」功能,可以說明這一點。

+0

必須嘗試收拾謝謝 – byronyasgur