p 标签的 CSS 伪类选择器(first-child)适用于作为其父级的直接第一个子元素的所有 p,为什么不作为 p 本身的直接第一个元素的 p 标签。
p:first-child {
color: blue;
}
<body>
<p>This P is body's first Child.</p>
<p>This is body's second child.</p>
<div>
<p> This P is div's first Child </p>
<p> This is div's second child.</p>
</div>
<p>
<p> This P is P's first child :: Why it does not get pseudo class</p>
<p> This is P's second child </p>
</p>
</body>
最佳答案
<p>
标签不能嵌套在 HTML 中。这是因为它们的存在只是为了将文本格式化为..好吧..一个段落,您可以阅读更多相关信息 here .
简而言之,任意开<p>
标签只是关闭最后一个 <p>
无论语法如何,都要打开。
关于css - 为什么 p :first-child pseudo class does not apply to p element's first-child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980736/