how to select two nth children in css

i have a container that contains 6 children, i want to select the 3rd and 4th children .
24 Replies
bonaventure
bonaventure3w ago
i know i could do div:nth-child(3), div:nth-child(4){} but is there any way i could do it in one line of code like div:nth-child(3,4) this doesnt work
ἔρως
ἔρως3w ago
do those children have a class? and what are you trying to do? maybe you're going about it the wrong way, and there could be an easier way
bonaventure
bonaventure3w ago
yes those children have the same class all 6 same class
ἔρως
ἔρως3w ago
what are you trying to do?
bonaventure
bonaventure3w ago
https://preview.colorlib.com/theme/feliciano/index.html like the menu section of this page i want the middle row's children to be row-reverse btw im not sure if colorlib is trustable site to visit i always wanna pre-warn others so as to not get them in trouble
ἔρως
ἔρως3w ago
what that theme is doing is to manually put the content where it should be if the image is on the left, the markup has the image on the left if the image is on the right, the markup has the image on the right
MarkBoots
MarkBoots3w ago
with grid (subgrid in my case) and a little bit of js, you can loop over the items and check if the row is odd or not. and style according to it https://codepen.io/MarkBoots/pen/mdYXzXN?editors=1111 (if im not mistaken they are working on css row/column selectors for grid, but probably will take a while) it's also responsive and doesn't minds the amount of childs
No description
ἔρως
ἔρως3w ago
https://jsfiddle.net/c3rm6yzg/ <-- you can hard-code it as well, or automatically generate it with sass
ἔρως
ἔρως3w ago
No description
ἔρως
ἔρως3w ago
the trick is math every 2*cols - [0..cols-1], you set the style if you want 3 columns, it's 2*3n - [0..2] -> [6n, 6n - 1, 6n - 2], which selects the 6th, 5th and 4th elements, and then the 12th, 11th and 10th, and then the 18th, 17th and 16th ...
Chris Bolson
Chris Bolson3w ago
:nth-child(n+3):nth-child(-n+4){
}
:nth-child(n+3):nth-child(-n+4){
}
would achieve it in a single line but it isn't really that different to what you allready tried.
ἔρως
ἔρως3w ago
that doesn't work
ἔρως
ἔρως3w ago
No description
ἔρως
ἔρως3w ago
it will work for 2 columns, but not for 3
Chris Bolson
Chris Bolson3w ago
I was really just answering the original question. I know it won't work for 3 columns but that wasn't what bonaventure was originally asking. Both you and Mark have provided much more complete solutions, I was just concentrating on the original question.
ἔρως
ἔρως3w ago
yours is good for the exact solution, but it isn't very extensible in the future, and if you need 3 or 4 columns it will have to be re-written mine is very loopable
Chris Bolson
Chris Bolson3w ago
I absolutely agree
ἔρως
ἔρως3w ago
i agree as well but it is a nice trick, not gonna lie
ἔρως
ἔρως3w ago
yours has another problem: it only works for the 3rd and 4th, and doesn't select the 8th and 9th
No description
ἔρως
ἔρως3w ago
which, again, for this exact question works fine
MarkBoots
MarkBoots3w ago
if only we had :nth-row() / :nth-column()
ἔρως
ἔρως3w ago
we kinda do :nth-child(n of .selector) if js sets the class, for example, then it's done but then, that's dumb and js can just set the class wait, no, this makes no sense, ignore it https://jsfiddle.net/c3rm6yzg/1/ <-- i remade this in sass and ... 😭 it was horrible!
Kevin Powell
Kevin Powell3w ago
Was talking with Tab Atkins at CSS day and it sounds like we'll be able to select specific grid cells one of these days 😄
ἔρως
ἔρως3w ago
hopefully it works for tables too