Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 .

like image 277
sakhunzai Avatar asked Feb 17 '14 12:02

sakhunzai


People also ask

How do I hide the nth child in CSS?

jQuery selector is described in the Selectors/nthChild docs, and the above can be accomplished with $("li. mybutton:nth-child(2)"). hide() .

Is nth child a pseudo element?

Pseudo-class :nth-child()The :nth-child() pseudo-class represents an element that has an+b siblings before it in the document tree, for any positive integer or zero value of n, and has a parent element.

Is pseudo selector The first child?

The first-child is a pseudo class in CSS which represents the first element among a group of sibling elements. The :first-child Selector is used to target the first child element of it's parent for styling. Example: HTML.


1 Answers

: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

like image 157
BoltClock Avatar answered Sep 19 '22 18:09

BoltClock