CSS how to use pseudo-class :not with :nth-child

ⅰ亾dé卋堺 提交于 2020-01-03 06:44:09

问题


Is is possible to use :not() with 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 .


回答1:


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

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

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

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

Demo



来源:https://stackoverflow.com/questions/21829478/css-how-to-use-pseudo-class-not-with-nth-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!