选择最后一个孩子当奇数,2最后一个孩子当偶数 [英] Select last child when odd, 2 last childs when even
问题描述
我在一个情况下,显示的元素数量是可变的,我需要一个奇怪的解决方案,我不能实现,我甚至怀疑,如果它是可实现的只有css。
如果元素的数量是奇数,我需要选择最后一个子元素,如果元素的数量是偶数,我需要选择最后一个子元素。
我一直在使用 nth-last-child
,:not nth-last-child())
,奇数和偶数,但从来没有一个好的解决方案。
任何人都有任何想法/建议
这是一种方法...
.wrap div:last-child,.wrap div :nth-last-of-type(-n + 2):not(:nth-child(even)){color:red;}
< div class =wrap> < div> Odd< / div> < div> Even< / div> < div> Odd< / div> < div> Even< / div> < div> Odd< / div> < div> Even< / div>< / div>< hr>< div class =wrap> < div> Odd< / div> < div> Even< / div> < div> Odd< / div> < div> Even< / div> < div> Odd< / div>< / div>
I'm in a situation where the number of elements showed is variable, and I need a strange solution which I'm not able to achieve, I even doubt if it's achievable only with css.
I need to select the last-child if my number of elements is odd, and the last 2 child if the number of elements is even.
I've been trying with nth-last-child
, :not(:nth-last-child())
, odd and even, but never got a good solution.
Anyone has any idea/advice about this issue a part of adding a class "odd" like on html tables?
Thanks a lot in advance!
Here is one way...
.wrap div:last-child,
.wrap div:nth-last-of-type(-n+2):not(:nth-child(even)) {
color: red;
}
<div class="wrap">
<div>Odd</div>
<div>Even</div>
<div>Odd</div>
<div>Even</div>
<div>Odd</div>
<div>Even</div>
</div>
<hr>
<div class="wrap">
<div>Odd</div>
<div>Even</div>
<div>Odd</div>
<div>Even</div>
<div>Odd</div>
</div>
这篇关于选择最后一个孩子当奇数,2最后一个孩子当偶数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!