Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using * with :not in css

Im working on a html linter using css.

Reference: https://bitsofco.de/linting-html-using-css/

I like the idea of highlighting elements that have inline styles like so:

*[style] {
    color: red !important;
    border: 5px solid red !important; 
}

However, I do have certain instances where I have to use inline styles, ie canvas elements.

  1. How do I use the :not selector with the *?
  2. Can I have multiple :nots, ie :not(canvas):not(form), etc
like image 433
Carey Estes Avatar asked Mar 13 '17 22:03

Carey Estes


People also ask

What is * used in CSS?

Definition and Usage The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

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; }

How do you select non classes 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.


2 Answers

What you have works and excludes the canvas. And yes, you can chain multiple :not()s like that.

* {
  border: 1px solid black;
}
*[style]:not(canvas):not(form) {
    color: red !important;
    border: 5px solid red !important;
}
<canvas style="foo">canvas</canvas>
<form style="foo">form</form>
<div style="foo">div</div>
like image 59
Michael Coker Avatar answered Oct 22 '22 12:10

Michael Coker


the :not() rule matches anything not matching the subrule. The subrule is a valid css selector. writing [canvas] will match any element with a canvas attribute, so this isn't what you want.

The correct usage is:

*[style]:not(canvas):not(form)

like image 34
Ruben Vincenten Avatar answered Oct 22 '22 12:10

Ruben Vincenten