2011-09-20 96 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{...},因为前者更为具体。