html - html疑惑中的第n个 child

标签 html css

我希望 p:nth-child(2) 突出显示 p 的第二项,即第二段,但为什么在这种情况下第一段是突出显示?我删除了 h1p:nth-child(2) 突出显示了第 3 项,为什么? h1 是否也考虑 p

p:nth-child(2) {
    background: #ff0000;
}
<h1>test</h1>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>

最佳答案

p:nth-child(2) 表示如果 p 是集合中的第二个元素,那么它会被高亮显示。使用 p:nth-of-type(2) 准确突出显示 ph1 组合集合中的第二个 p 元素

关于html - html疑惑中的第n个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27887009/

相关文章:

jquery - 如何将鼠标单击更改为鼠标悬停?

html - 如何将图像设置为响应图像周围的框架?

javascript - 复制表单部分的 jQuery clone() 是在单击时添加多个部分

Rails 形式的 Javascript slider

php - 如果在 PHP 中选中复选框,如何读取?

html - 禁用特定的 CSS 样式

javascript - 将类添加到 SVG 路径

jquery - 为什么 JQuery .hide() 函数不能与引导微调器一起使用?

html - 下载的字体不显示特殊字符

动画中的 CSS Slide 在段落和 h1 上工作但不在 div 上工作