p:first-child
选择的元素满足两个要求:
下面来看一个案例:
p:first-child {
color: red;
}
li:first-child {
color: blue;
}
<div>
<p>These are the necessary steps:</p>
<ul>
<li>Intert Key</li>
<li>Turn key <strong>clockwise</strong></li>
<li>Push accelerator</li>
</ul>
<p>Do <em>not</em> push the brake at the same time as the accelerator.</p>
</div>
分析:
效果如下:
These are the necessary steps:
Do not push the brake at the same time as the accelerator.
注意:如果HTML代码改成如下情况,由于p元素不再是第一个子元素,而第一个子元素又不是p,因此结果会发生变化。
<div>
<ul>These are the necessary steps:</ul>
<ul>
<li>Intert Key</li>
<li>Turn key <strong>clockwise</strong></li>
<li>Push accelerator</li>
</ul>
<p>Do <em>not</em> push the brake at the same time as the accelerator.</p>
</div>
Do not push the brake at the same time as the accelerator.
原文:https://www.cnblogs.com/dailymatters/p/12630377.html