【发布时间】:2015-07-15 18:48:45
【问题描述】:
:)
为什么我的第一段没有红色背景?
HTML:
<div class="item">
<h1><a href="http://localhost/test">Test</a></h1>
<p>This is a new entry </p>
<p>This is a new entry</p>
</div>
CSS:
.item p:first-child {background:red}
演示: http://jsfiddle.net/tv90yrbz/
非常感谢您对此提供的任何帮助。
【问题讨论】:
-
因为
<p>不是第一个孩子:) -
不使用 first-chid 使用 .item p:nth-child(2) {background:red}
-
@siva:虽然它可能适用于所提供的确切示例代码,但它并不总是有效,因为在某些情况下
p也不是第二个孩子。
标签: html css css-selectors