CSS :nth-of-type() and :not() selector?

标签 css css-float css-selectors

我并排放置了 25% 宽的文章。我要添加 clear:both在每四个元素之后。但是我需要在元素之间插入一个图形分节符。它必须在 <ul> 内.为了有效,我将“分节符”(下面示例中的第一个 li 项)包装到 <li> 中。

<ul>
    <li class="year"><h1>THIS IS A SECTION Break and 100% wide</h1></li>
    <li>This is a article and only 25% wide</li>
    <li>This is a article and only 25% wide</li>
    <li>This is a article and only 25% wide</li>
    <li>This is a article and only 25% wide</li>
</ul>

我希望每四个元素都是一个换行符,所以我使用...

ul li:nth-of-type(4n+1) { clear: both; }

但是我想要 li.year不受此行为的影响,所以我尝试了这个

ul li:not(.year):nth-of-type(4n+1) { clear: both; }

现在是最后一个<li>在我上面的示例代码中 float 到下一行但是自第一个 <li> 以来不应该发生这种情况不是 float 文章之一,但包含标题。

是否可以堆叠 :notnth-of-type()选择器相互叠加?

最佳答案

你的选择器--

ul li:not(.year):nth-of-type(4n+1) { background: yellow; }

-- 完全正确 ( as shown by highlighting the selector )。

问题在于您如何使用 clear。如果您使用 clear:right,然后在以下元素上使用 clear:left,它会起作用:

ul li:not(.year):nth-of-type(4n+1) { clear: right;  }
ul li:not(.year):nth-of-type(4n+2) { clear: left;  }

根据评论编辑 删除的文字是无稽之谈。根据 BoltClock 的回答,真正的问题是 :not 伪类不影响 nth-of-type。在这种情况下,调整选择器偏移量是巧合,但如果 :not 模式不同,则不会起作用。

ul li:not(.year):nth-of-type(4n+2) { clear: left;  }

http://jsfiddle.net/8MuCU/

关于CSS :nth-of-type() and :not() selector?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12356158/

相关文章:

css - Vaadin 网格行高

css - 简单 - 无法向右移动导航

css - 调整大小时,自定义图标不会留在标题的右侧

html - 列表不包含其 float 列表项

css - CSS 选择器中的特殊字符

css - 背景图片在 iPad 1 上不显示,但在 iPad 2 上显示

php - 如何使用 span 设置 PHP 回显输出的样式

html - 当我在 li 中使用 float 时,list-style-type 在 IE 7 中不起作用

CSS @supports (::伪元素)

html - 尝试在嵌套的 <li> 样式菜单中选择 CSS3 元素