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!
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>
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With