Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I target 2nd and 3rd elements with nth-child using just one line of css?

Tags:

html

css

I would like to target the 2nd and 3rd elements in my div using nth-child but I only want to use one line of css instead of two.

p:nth-child(2) {
  color: red;
}
p:nth-child(3) {
  color: red;
}
<div>
  <p>One</p>
  <p>Two</p>
  <p>Three</p>
  <p>Four</p>
</div>

Is there any way to use the nth-child selector to target both?

like image 914
ilikeuxandvr Avatar asked Jan 06 '23 09:01

ilikeuxandvr


1 Answers

You can specifically target each p tag by its nth-child and separate with a comma.

Or you can reduce selectors by specifying a range like:

JS Fiddle

p:nth-child(n+2):nth-child(-n+3)  { color: red; } 
like image 138
Derek Story Avatar answered Jan 13 '23 09:01

Derek Story