为什么p:first-child伪类不适用于p元素的first-child [duplicate]

问题描述 投票:2回答:1

此问题已经在这里有了答案:

p标签的CSS伪类选择器(第一个孩子)适用于作为其父对象的直接第一个孩子的所有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>
css css-selectors pseudo-class
1个回答
4
投票

<p>标记不能嵌套在HTML中。这是因为它们仅存在以将文本格式设置为....段落,您可以阅读更多有关here的信息。

简而言之,任何打开的<p>标记都会简单地关闭要打开的最后一个<p>,而与语法无关。

© www.soinside.com 2019 - 2024. All rights reserved.