Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在CSS中只选择一些子级?_Css_Css Selectors - Fatal编程技术网

如何在CSS中只选择一些子级?

如何在CSS中只选择一些子级?,css,css-selectors,Css,Css Selectors,我的HTML中有一些div,一个动态的div数 如何仅选择div 2,5,8,11 我尝试了这个:第n个孩子(2n+3),但这并不是我所需要的 代码如下: <!DOCTYPE html> <html> <head> <style> .a:nth-child(2n+3) { background:#ff0000; } </style> </head> <body&g

我的HTML中有一些div,一个动态的div数

如何仅选择div 2,5,8,11

我尝试了这个
:第n个孩子(2n+3)
,但这并不是我所需要的

代码如下:

<!DOCTYPE html>
<html>
  <head>
      <style>
          .a:nth-child(2n+3) { background:#ff0000; }
      </style>
  </head>
  <body>
    <p class="a">The first paragraph.</p>
    <p class="a">The second paragraph.</p>
    <p class="a">The third paragraph.</p>
    <p class="a">The fourth paragraph.</p>
    <p class="a">The fifth paragraph.</p>
    <p class="a">The sixth paragraph.</p>
    <p class="a">The seventh paragraph.</p>
    <p class="a">The eight paragraph.</p>
    <p class="a">The ninth paragraph.</p>
    <p class="a">The seventh paragraph.</p>
    <p class="a">The eight paragraph.</p>
    <p class="a">The ninth paragraph.</p>
  </body>
</html>

.a:n个孩子(2n+3){背景:#ff0000;}

第一段

第二段

第三段

第四段

第五段

第六段

第七段

八段

第九段

第七段

八段

第九段

E:n子元素(n)E元素,其父元素的第n个子元素

例如:

 div:nth-child(2),div:nth-child(5){color:red;}

如果div是动态插入的,您能不能给每个div一个id,比如'nested1,nested2'等等?如果使用这种方法,请记住不要以整数开头id,因为CSS不会拾取它。

编辑如果只需要2,5,8,11,则答案是:

p:nth-child(3n+2)
{
   background: #ccc;
}

选择第2、3、5、8、11段:

p:nth-child(3n+2),p:nth-child(3)
{
   background: #ccc;
}


我不得不单独添加p:n个孩子(3),因为它不适合每次+3的一般模式。

为什么不使用类?向我们展示你的html代码,这样我们就可以使用你得到的东西,只需要第2、5、8、11段……艾奇说2、3、5、8、11p:n个孩子(2),p:n个孩子(3n+2){背景:红色;}好吧,你说得对,所以要么是问题不对,要么是他的评论;)这是我的错误,我不需要儿童3首先,不要在段落之间添加
使用css,例如页边底部:10px。。。但是如果您必须使用它,那么将第n个child更改为第n个类型,例如p:n个类型(3n+2){background:#ccc;}