CSS第一个孩子

Chi*_*ghE 3 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 {}
Run Code Online (Sandbox Code Playgroud)

它是否正确?如果是这样可以进一步简化?

感谢大家的帮助!

spl*_*ter 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 {}
    
    Run Code Online (Sandbox Code Playgroud)

  • 选择#container元素本身,以防它的第一个子元素是h*family之一.然后你无法用纯CSS实现这个,需要添加一个简单的JS(在这种情况下使用jQuery):

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