Is there a CSS selector for element without any class? For example in HTML
<section>Section A</section> <section class="special">Section B</section> <section class="">Section C</section>
I would like to select Section A (or maybe Section A and Section C, it does not matter that much), by saying something like
section:not(.*) { color: gray }
I understand that I could define it to section and reset it back in all particular classes, like in
section { color: gray } section.special { color: black }
but this is not what I want, because it is not very manageable once the styles get complex and in some cases it is hard to do the "reset" properly (of course not in this simplified example).
To select element that does not have specific class with JavaScript, we can use the document. querySelector method with the :not pseudoclass. const li = document. querySelector("li:not(.
The CSS id Selector The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
The difference between Class and ID selectorIDs are only used when one element on the page should have a particular style applied to it. However, a class can be used to identify more than one HTML element.
The CSS type selector matches elements by node name. In other words, it selects all elements of the given type within a document. /* All <a> elements. */ a { color: red; } Type selectors can be namespaced when using @namespace .
With section:not([class])
you select every section without the class attribute. Unfortunately, it won't select those sections with an empty class attribute value. So in addition, we have to exclude these sections:
section:not([class]) { /* every section without class - but won't select Section C */ color: red; } section[class=""] { /* selects only Section C */ font-weight: bold; }
<section>Section A</section> <section class="special">Section B</section> <section class="">Section C</section>
:not
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