Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CSS :not selector in LESS nested rules

.outerclass {
    h3 {
        color: blue;
    }
    p:not(.nested) {
        color: green;
    }
}

In the LESS example above I wish to target all "p" elements within a div class "outerclass" but NOT the p elements within the further nested div called ".nested" - it does not work but instead makes all p elements green. I have tried...

p:not(.nested p) // excludes all p elements 

and also...

p:not(.nested > p) // excludes all p elements 

...to no avail. Is this possible or what am I missing? I am brand new to LESS

like image 383
user2317093 Avatar asked Nov 12 '13 15:11

user2317093


People also ask

Can I use CSS not selector?

The :not() property in CSS is a negation pseudo class and accepts a simple selector or a selector list as an argument. It matches an element that is not represented by the argument. The passed argument may not contain additional selectors or any pseudo-element selectors.

How do I exclude a specific tag in CSS?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

What is not () in CSS?

:not() The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the negation pseudo-class. /* Selects any element that is NOT a paragraph */ :not(p) { color: blue; }


1 Answers

It is not a LESS issue as much as your css selector syntax. The p:not(.nested) is saying all p elements without the .nested class themselves, what you state is that the .nested class is on a div in which the p resides, so you need this:

.outerclass {
    h3 {
        color: blue;
    }
    :not(.nested) p,
    > p {
        color: green;
    }
}

UPDATE: I removed the div and added the direct child p instance, so that the CSS output would properly target all p in .outerclass except for the exception.

CSS Output for p elements would be

.outerclass :not(.nested) p,
.outerclass > p {
  color: green;
}

The above will target any direct child p elements and any nested p elements in .outerclass except those that are children of your .nested element.

An issue

The issue BoltClock is noting is if the p is nested deeper than being the immediate child of the .nested element. See this fiddle where the last p element is still targeted even though it is within a .nested class.

If the p element will always be the direct child of .nested there is no issue. Or if the .nested is always the direct child of .outerclass but the p maybe nested deeper, then the above selector can be changed to > :not(.nested) p to produce CSS of .outerclass > :not(.nested) p which will then work for all p under .nested.

The problem will be if the .nested in relation to .outerclass and the p within .nested are both at some arbitrary depth to those parents. No css selector could handle that adequately.

like image 144
ScottS Avatar answered Oct 19 '22 01:10

ScottS