Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css selector, elements between two classes

I am trying to write a css selector that can select the elements between two clases .

Example:

<div class='start'></div>
<div></div> 
<div></div>
<div class='end'></div>

My question is following: is there a way to use css selector to select two divs between the .start div and .end div?

Any help is appreciated!

like image 908
boandriy Avatar asked Oct 16 '25 02:10

boandriy


2 Answers

I think your best option is to use :not() selector in CSS. CSS does not have a mechanism where you can select elements in between.

div:not(.start):not(.end) {
  background: red;
}
<div class='start'>A</div>
<div>B</div>
<div>C</div>
<div class='end'>D</div>
like image 101
Nidhin Joseph Avatar answered Oct 18 '25 20:10

Nidhin Joseph


In Addtion to @Nidhin Josephr's answer you can also do it without adding .start & .end class using :not() with :nth-child :nth-last-child pseudo selector.

Codepen

HTML

<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>

CSS

div:not(:nth-child(1)):not(:nth-last-child(1)){
  background: red;
}
like image 37
Sumit Patel Avatar answered Oct 18 '25 18:10

Sumit Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!