CSS3奇数和偶数只有可见行 [英] CSS3 odd and even only visable rows

查看:133
本文介绍了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>


推荐答案

: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.

您仍然可以通过在<$ c $后创建元素 c> .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;    
}

您可以继续为越来越多的sibling组合添加类似的规则 .hide p ,但这是非常不灵活。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆