Css 第一个孩子

Css 第一个孩子,css,css-selectors,Css,Css Selectors,将任何内容id标记的元素设置为具有h1、h2、h3、h4、h5和h6中任何一个的第一个子后代元素,如下所示: 我创建的选择器如下所示: #content:first-child h1, #content:first-child h2, #content:first-child h3, #content:first-child h4, #content:first-child h5, #content:first-child h6 {} 这是正确的吗?若然,可否进一步简化 谢谢大家的帮助

将任何内容id标记的元素设置为具有h1、h2、h3、h4、h5和h6中任何一个的第一个子后代元素,如下所示:

我创建的选择器如下所示:

#content:first-child h1,
#content:first-child h2, 
#content:first-child h3, 
#content:first-child h4, 
#content:first-child h5, 
#content:first-child h6 {}
这是正确的吗?若然,可否进一步简化


谢谢大家的帮助

描述有点不清楚,但据我所知,你想

  • 选择这些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 {}
    

  • 如果#container元素的第一个子元素是h*族的一个子元素,请选择该元素本身。那么,使用纯CSS无法实现这一点,需要添加一个简单的JS,如(在本例中使用jQuery):


您可以将
class=“header”
添加到所有的h*标记中。所以你的css现在应该是

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

这里的问题是,您需要记住将
class=“header”
放在您需要的每个标记上

是的,基本上就是这样。无论哪种方式,
:*-child
伪类都应该附加到子类而不是父类。在您所写的内容中,
:first child
应用于什么?它适用于什么?@Ischin:
:第一个孩子
不是在CSS3中引入的,它从CSS2开始就存在了。@Chris“h1、h2、h3、h4、h5和h6中任何一个的第一个孩子后代元素”@BoltClock:我写这篇文章是为了让@Chicksentmehire思考一下,看看他所说的有什么不对,而不仅仅是给出答案(特别是因为它被标记为“家庭作业”)。
#content:first-child .header { /* whatever */ }