2011-07-11 64 views
3

“设定的任何H1,H2,H3,H4,H5的第一胎后代元素的任何内容ID标记的元素,H6如下:”CSS首先,孩子

我以前做的选择创建如下:

#content:first-child h1, 
#content:first-child h2, 
#content:first-child h3, 
#content:first-child h4, 
#content:first-child h5, 
#content:first-child h6 {} 

这是正确的吗?如果可以的话可以进一步简化?

感谢大家的帮助!

+0

@Ischin标签:':第一child'不是在CSS3介绍,自从CSS2以来一直存在。 – BoltClock

+0

什么是':第一胎'适用于你写的内容?这是什么意思适用于? –

+0

@Chris“h1,h2,h3,h4,h5和h6中的任何一个的第一个孩子的后代元素” – BoltClock

回答

7

的描述是一点点,但目前还不清楚,但是,从我能理解你想要么

  • 选择那些H *元素,然后风格会是这样:

    #content h1:first-child, 
    #content h2:first-child, 
    #content h3:first-child, 
    #content h4:first-child, 
    #content h5:first-child, 
    #content h6:first-child {} 
    

Example

  • 选择#container元素本身,以防其第一个孩子是h *系列之一。然后,你不能用纯CSS实现这一目标,需要添加一个简单的JS一样(在这种情况下使用jQuery):

    $('#content').has('h1:first-child, h2:first-child, h3:first-child, h4:first-child, h5:first-child, h6:first-child'); 
    

Example

+0

是的,这是非常多的。无论哪种方式,':* - 孩子'伪类应该附加给孩子而不是父母。 – BoltClock

+0

第一个是完美的,谢谢! – ChickSentMeHighE

1

您可以在class="header"添加到您的所有H的*标签。所以,你的CSS现在是

#content:first-child .header { /* whatever */ } 

这里的问题是,你需要记住把class="header"到每一个需要