CSS如何使用伪类:不使用:nth-​​child

时间:2022-05-24 20:33:08

Is is possible to use :not() with nth-child ?

是否可以使用:not()与nth-child?

I tried something like this without any luck :

我没有运气就试过这样的事:

td:not(:nth-child(4n)){
  text-align:center;
}

However this seems to work :

然而,这似乎工作:

td:not(:first-child){
  text-align:center;
}

What I am trying is to center align all table columns except 2nd and 4th column. The columns are dynamically generated to add a custom class to these column .

我想要的是居中对齐除第2和第4列以外的所有表格列。动态生成列以向这些列添加自定义类。

1 个解决方案

#1


28  

:not(:nth-child(4n)) will get you anything that isn't :nth-child(4n), i.e. anything that isn't the 4th, 8th and so on. It won't exclude the 2nd child because 2 isn't a multiple of 4.

:not(:nth-​​child(4n))会得到任何不是:nth-​​child(4n),即任何不是第4,第8等的东西。它不会排除第二个孩子,因为2不是4的倍数。

To exclude the 2nd and 4th you need either one of:

要排除第2和第4,您需要以下任何一个:

  • td:not(:nth-child(2n)) if you have fewer than 6 columns, or

    td:not(:nth-​​child(2n))如果你的列少于6列,或者

  • td:not(:nth-child(2)):not(:nth-child(4)) if you have at least 6 columns and only want to exclude the 2nd and 4th, and not every even column.

    td:not(:nth-​​child(2)):not(:nth-​​child(4))如果你有至少6列,只想排除第2和第4列,而不是每个偶数列。

Demo

演示

#1


28  

:not(:nth-child(4n)) will get you anything that isn't :nth-child(4n), i.e. anything that isn't the 4th, 8th and so on. It won't exclude the 2nd child because 2 isn't a multiple of 4.

:not(:nth-​​child(4n))会得到任何不是:nth-​​child(4n),即任何不是第4,第8等的东西。它不会排除第二个孩子,因为2不是4的倍数。

To exclude the 2nd and 4th you need either one of:

要排除第2和第4,您需要以下任何一个:

  • td:not(:nth-child(2n)) if you have fewer than 6 columns, or

    td:not(:nth-​​child(2n))如果你的列少于6列,或者

  • td:not(:nth-child(2)):not(:nth-child(4)) if you have at least 6 columns and only want to exclude the 2nd and 4th, and not every even column.

    td:not(:nth-​​child(2)):not(:nth-​​child(4))如果你有至少6列,只想排除第2和第4列,而不是每个偶数列。

Demo

演示