Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector to invert selection

Tags:

css

selector

Is there any advantage or disadvantage in using :not() over an inverted selector logic? May it be in performance, safety or browser support, which approach is recommended?

Either:

.imageSlider img:not(:first-child) {
  display: none;
}

Or:

.imageSlider img {
  display: none;
}

.imageSlider img:first-child {
  display: block;
}
like image 707
user1438038 Avatar asked Nov 18 '25 00:11

user1438038


1 Answers

Sometimes it's could be better to use :not.

<p class="my-paragraph">
    <div class="something"></div>
    <div class="something-else"></div>
    <div class="an-other-thing"></div>
    <div class="an-other-thing"></div>
    <div class="last-one"></div>
</p>

In this case, if you want to hide everything except div .an-other-thing it will be quicker to write :

.my-paragraph div:not(.an-other-thing) {
    display: none;
}

Instead of:

.my-paragraph div {
    display: none;
}

.my-paragraph div.an-other-thing {
    display: block;
}

In most of cases, a longer CSS means longer time to execute it

like image 87
Magicprog.fr Avatar answered Nov 19 '25 15:11

Magicprog.fr



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!