Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an element with empty class attribute (class="") using CSS?

Tags:

Is there a CSS way to select an element that looks like that by class?

<a class="" href="..."> 

Like a selector for empty class declarations?

like image 539
user1856596 Avatar asked May 03 '13 07:05

user1856596


People also ask

How do you select an element without a class 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.

How do you select an element with a class attribute?

Matches elements with an attr attribute whose value is exactly value or begins with value immediately followed by a hyphen. In the example below you can see these selectors being used. By using li[class] we can match any list item with a class attribute. This matches all of the list items except the first one.

How do you select an element in a class in CSS?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

How do you check if an element is empty in CSS?

An element with nothing between its tags is empty. That includes if an element contains a code comment. And if the CSS for the element has generated content — as from a pseudo-element like ::before or ::after — it is also still considered empty.


2 Answers

You can use element-attribute selector here with an empty class value

div[class=""] {     color: red; } 

Demo

Note: You can replace the div with required element

like image 97
Mr. Alien Avatar answered Sep 24 '22 08:09

Mr. Alien


Provided the class attribute is present as you say you can use the attribute selector like this:

jsFiddle

<a class="" href="...">asd</a>  a[class=""] {     color: red; } 

If you want this to work when there is no class attribute present on the element you can use :not([class]).

jsFiddle

<a href="...">asd</a>  a:not([class]) {     color: red; } 

These can then be combined together to handle both cases.

jsFiddle

<a href="...">asd</a> <a class="" href="...">asd</a>  a[class=""], a:not([class]) {     color: red; } 
like image 38
Daniel Imms Avatar answered Sep 24 '22 08:09

Daniel Imms