2014-02-14 124 views
2

的第一个孩子我有一个HTML的结构如下所示:祖先CSS

<div class="outer"> 
    <div class="parent"> 
     <div class="child"> <!-- TARGET THIS CLASS --> 
     Words! 
     </div> 
    </div> 
    <div class="parent"> 
     <div class="child"> 
     Words! 
     </div> 
    </div> 
</div> 

我努力的目标在HTML中.child类的第一次出现。我曾尝试

.child:first-child 

但同时指定child类。我也试过

.outter > .child:first-child 

但是,这似乎并没有针对div根本。有什么建议么?

+0

可能只是您的文章,但你拼错在CSS声明'outer'。 – steinmas

回答

3

不幸的是,这不是在CSS中可用。然而,你可以这样做:

.parent:first-child .child:first-child { //css here } 

有一个假设,在这里,在第一parent元素中的第一child元素。如果不是,并且第一个parent元素是空的,则这不会成为目标。

2

您可以使用多个伪类(只是不一样的元素)

.outer .parent:first-child .child:first-child { //css here }

2

您需要根据像这样的父类的目标是:

.outer > .parent:first-child > .child:first-child 
0

获得第一外面的孩子然后得到那个父母的第一个孩子。

0

:first-child定位父元素中元素的第一次出现。因为这两个.child班都是他们父母的第一个孩子,所以他们都将成为您拥有的CSS的目标。

对于你想要的效果,使用此:

.outer .parent:first-child .child:first-child 
0
.parent:first-child 

似乎工作