css - 为什么 p :first-child pseudo class does not apply to p element's first-child

标签 css css-selectors pseudo-class

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/

相关文章:

html - 移动屏幕上的固定页面大小

css - How to target a selector only if it's NOT child of a specific element (so only if it's the root)

javascript - Android网页中的链接变蓝

html - :not() is not ignoring class in list-items with links

javascript - 如何使用javascript/react获取两个日期之间的完整日期?

php - 如何在使用 PHP 'header' 时合并仅与一个页面相关的内联 CSS 样式和样式表?

jquery - 仅在视口(viewport)中初始化 RoyalSlider

html - 如何在元素聚焦时动态更改 sass 属性?

jquery - 无法选择 ID 为 =":1"的 div

css - 如何使伪类与 Internet Explorer 7/8 一起工作?