Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a CSS selector for element without any class?

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).

like image 736
gorn Avatar asked Apr 01 '16 11:04

gorn


People also ask

How do you select an element that does not have a specific class?

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(.

How do I select a specific element in CSS?

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.

What is the difference between element selector and class selector?

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.

What is an element type CSS selector?

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 .


1 Answers

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>

Further reading

  • CSS attribute selector, browser support
  • :not
like image 197
Marvin Avatar answered Sep 22 '22 18:09

Marvin