html - CSS 样式 :nth-child even nested elements

标签 html css

即使元素嵌套在 div 中,我如何设置 :nth-child(4n) 样式,这里的示例是 p div:

p:nth-child(2) {
    background: red;
}
<p>The first paragraph.</p>
<div>
  <p>The second paragraph.</p>
</div>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>

最佳答案

body p:nth-child(2n) {
    background: red;
}
body div:nth-child(2n) p {
    background: red;
}
<body>
<p>The first paragraph.</p>
<div>
  <p>The second paragraph.</p>
</div>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
<body>

关于html - CSS 样式 :nth-child even nested elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43607712/

相关文章:

html - 如何将 "last child"样式应用于而不是 :last-child element?

html - float 元素永远不能离开 div

html - 带相交线的圆 Angular 矩形

javascript - <HTML> 自动设置我认为由 Google 的 JSAPI 设置的内联样式

javascript - 如何自动播放没有声音的视频?

javascript - IE 不为 GET 表单上的电子邮件输入传递 @ 符号

html - 如何更改type = time的html输入标签随附的小时钟图标的颜色

html - 将多个 html 模板组合成单个 index.html 文件

html - 使用css的曲线框

html - 如何在不发生换行的情况下将按钮放入文本中?