2011-09-20 97 views
2

我做錯了什麼?pseudoclass:第一個孩子不工作

CSS /頂嘴:

#section 
    article 
    border-top: 1px solid black 
    &:first-child 
    border: none !important 

HTML/HAML:

#section 
    %h2 title 
    %article 
    stuff here. There is still a top border here despite first-child style. 
    %article 
    stuff here. 
    %article 
    stuff here. 

這不起作用,而且還有第一<article>邊框。我必須在第一篇文章中創建另一個課程並執行諸如article.noborder之類的操作,以獲得無邊框。任何幫助將不勝感激... CSS恨我。

+2

你做錯的事是誤解':first-child'。 'article:first-child'並不意味着「匹配第一個'article'元素」,這意味着「匹配第一個孩子**如果**它是一個'article'元素」 - 在這種情況下,第一個孩子是' h2'。 – thirtydot

回答

14

由於第一個article之前的h2,您必須使用:first-of-type

+0

謝謝!是的,我有一種感覺,這是因爲H2,但不知道該看什麼。 – butterywombat

+1

這工作。 〜將選擇除第一個之外的所有元素。 在我的情況下 .row .row {margin-top:0px; } .row〜.row {margin-top:10px; } – Hendrik

1

在我看來,H2是該部分的第一個孩子,而不是第一篇文章。

1
section article:first-child{ 
    border:none; 
} 

section article:nth-child(2){ 
    border:2px solid yellow; 
} 

我以前遇到過這個問題,試着記住不要用不同的方式來調用相同的元素。

如果使用body section article { border:2px solid yellow}將重疊
section article:first-child{...},因爲前者更爲具體。