最简单的方法将CSS背景颜色应用到第3个孩子3到10(Simplest way to apply CSS background color to nth child 3 to 10)

编程入门 行业动态 更新时间:2024-10-27 06:22:56
最简单的方法将CSS背景颜色应用到第3个孩子3到10(Simplest way to apply CSS background color to nth child 3 to 10)

我想将CSS背景颜色应用到第3个孩子3到10

<div class="roundperiodbg" id="roundPeriod1" title="PERIOD 1"></div> <div class="roundperiodbg" id="roundPeriod2" title="PERIOD 1"></div> <div class="roundperiodbg" id="roundPeriod3" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod4" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod5" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod6" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod7" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod8" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod9" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod10" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod11" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod12" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod13" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod14" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod15" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod16" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod17" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod18" title="PERIOD 3"></div>

这工作,但有没有更简单的方法在CSS?

.roundperiodbg:nth-child(3), .roundperiodbg:nth-child(4), ..... .roundperiodbg:nth-child(10) { background : green; }

I want to apply CSS background color to nth child 3 to 10

<div class="roundperiodbg" id="roundPeriod1" title="PERIOD 1"></div> <div class="roundperiodbg" id="roundPeriod2" title="PERIOD 1"></div> <div class="roundperiodbg" id="roundPeriod3" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod4" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod5" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod6" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod7" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod8" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod9" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod10" title="PERIOD 2"></div> <div class="roundperiodbg" id="roundPeriod11" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod12" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod13" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod14" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod15" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod16" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod17" title="PERIOD 3"></div> <div class="roundperiodbg" id="roundPeriod18" title="PERIOD 3"></div>

This works but is there any simpler way in CSS?

.roundperiodbg:nth-child(3), .roundperiodbg:nth-child(4), ..... .roundperiodbg:nth-child(10) { background : green; }

最满意答案

你可以使用两个:nth-child()选择器来创建一个如下所示的范围:

.roundperiodbg:nth-child(n+3):nth-child(-n+10){ background : green; }

看这个演示小提琴

说明:

:nth-child(n+3)选择索引等于或大于3的所有元素

------ n | i ------ 0 | 3 ------ 1 | 4 ------ 2 | 5 ------ 3 | 6 ------ ..| ..

:nth-child(-n+10)选择索引等于或小于10的所有元素

------ n | i ------ 0 | 10 ------ 1 | 9 ------ 2 | 8 ------ 3 | 7 ------ ..| 1

当您在同一个CSS规则中应用这两个元素时,元素必须符合两个条件,因此它仅适用于索引在3到10之间的元素。


你也可以查看这个链接以获得更多有用的:nth-child()选择器。 谢谢@Rajaprabhu

You can use two :nth-child() selectors to create a range like this:

.roundperiodbg:nth-child(n+3):nth-child(-n+10){ background : green; }

See this demo fiddle.

Explanation:

:nth-child(n+3) selects all elements with an index equal or great than 3

------ n | i ------ 0 | 3 ------ 1 | 4 ------ 2 | 5 ------ 3 | 6 ------ ..| ..

:nth-child(-n+10) selects all elements with an index equal or lower than 10

------ n | i ------ 0 | 10 ------ 1 | 9 ------ 2 | 8 ------ 3 | 7 ------ ..| 1

When you apply both in the same CSS rule the element would have to match both conditions therefore it only applies to elements with index between 3 and 10.


Also you can check out this link for more cool uses of the :nth-child() selector. Thanks @Rajaprabhu

更多推荐

本文发布于:2023-04-29 09:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335860.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最简单   到第   颜色   背景   孩子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!