开发者社区 问答 正文

有时候不起作用 :first-child

比如下面的代码,不知为什么第一个article的字体不会变色?
html

<h1>logo</h1>
<article>article1</article>
<article>article2</article>
<article>article3</article>

css

article:first-child {
    color: red;
}​

screenshot

展开
收起
杨冬芳 2016-06-14 14:09:41 2308 分享 版权
1 条回答
写回答
取消 提交回答
  • 对于 :first-child 这个伪类,规范里是这么定义的:

    The :first-child pseudo-class represents an element that is the first child of some other element.

    到你的选择符上 article:first-child 这意味着这条规则只对article是其它元素的第一个子代的时候生效,你试一下把<h1>logo</h1> 删除,或者移到后面,就会生效

    2019-07-17 19:37:30
    赞同 展开评论
问答分类:
问答地址: