CSS3奇数行和偶数行

CSS3奇数行和偶数行

本文介绍了CSS3奇数行和偶数行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试去除交替元素的颜色.但是我希望行颜色仅替换可见行.如果您查看下面的内容,这是我尝试使其正常工作的尝试.

I'm trying to stripe the colours of alternating elements. But I want the row colors to alternate only the visible rows. If you have a look at the below here is my attempt at trying to get it working.

http://jsfiddle.net/kuwFp/3/

<!DOCTYPE html>
<html>
<head>
<style>
p:not(.hide):nth-child(odd)
{
background:#ff0000;
}
p:not(.hide):nth-child(even)
{
background:#0000ff;
}
.hide { display:none; }
</style>
</head>
<body>

<p>The first paragraph.</p>
<p class="hide">The second paragraph.</p>
<p>The third paragraph.</p>

</body>
</html>

推荐答案

您不能使用纯CSS进行此操作,因为:nth-child选择器是针对元素计算的,并且:not不会过滤元素在位置中的位置DOM.您需要使用JavaScript以获得完全灵活的解决方案.

You can't do this with pure CSS because the :nth-child selector is calculated with respect to the element and :not does not filter element position in the DOM. You need to use JavaScript for a fully flexible solution.

通过使.hide之后的元素与:nth-child交替使用它们应该是的颜色,您仍然可以灵活地

It's still possible for you to do this inflexibly by making elements after .hide with :nth-child alternate the color they should be:

.hide + p:nth-child(odd) {
    background: #0000ff;
}

您可以继续为越来越多的同级.hidep组合添加类似的规则,但这非常不灵活.

You can continue to add similar rules for more and more combinations of sibling .hide and p, but this is very inflexible.

这篇关于CSS3奇数行和偶数行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:37