html - 复杂的 CSS/LESS : Why doesn't this nth-child(even) ~ p work?

标签 html css less frontend

我正在尝试使用第 nth-child(even) 制作交替模式,其中 even 元素和它各自的一般 sibling 都是红色的。

但是,尝试了一段时间后,我无法弄清楚如何使“通用兄弟选择器”与交替颜色的元素颜色相同....

这是一个片段:

h1:nth-child(even),
h1:nth-child(even) ~ p {
  color: red;
}
<h1>Section 1</h1>
<p>I am odd.</p>
<p>I am odd.</p>

    
<h1>Section 2</h1>
<p>I am even and I should be red.</p>
<p>I am even and I should be red.</p>

<h1>Section 1</h1>
<p>I am odd.</p>
<p>I am odd.</p>
    
<h1>Section 2</h1>
<p>I am even and I should be red.</p>
<p>I am even and I should be red.</p>

此外,这里有一个 jsbin 中的示例: http://jsbin.com/tixobu/edit?html,css,output

这可能吗?任何帮助或花絮将不胜感激!

谢谢

最佳答案

你需要Adjacent sibling selectors不是General sibling selectors

h1:nth-child(even),
h1:nth-child(even) + p, h1:nth-child(even) + p + p{
  color: red;
}
<h1>Section 1</h1>
<p>I am odd.</p>
<p>I am odd.</p>

    
<h1>Section 2</h1>
<p>I am even and I should be red.</p>
<p>I am even and I should be red.</p>

<h1>Section 1</h1>
<p>I am odd.</p>
<p>I am odd.</p>
    
<h1>Section 2</h1>
<p>I am even and I should be red.</p>
<p>I am even and I should be red.</p>

关于html - 复杂的 CSS/LESS : Why doesn't this nth-child(even) ~ p work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35422614/

相关文章:

未找到 JavaScript/jQuery key

javascript - 如何将淡入淡出改为缓和?

android - html中的图像大或小

html - 使用 CSS 的样式表之外的选择器

css - 如何在 ReSharper 6 中禁用 .less (dotLess CSS) 语法分析?

html - iOS中的Flex图像,使图像宽度均匀

php - 尝试使用多列 HTML PHP 显示多个复选框

html - 在垂直 div 之间添加垂直线

javascript - ExpressJS - 我的 Jade 布局中的编译错误较少

javascript - 在类下使用 twitter bootstrap