Zachary Sakowitz
Zachary Sakowitz

Reputation: 3

Is there a way to use a CSS attribute selector to select a class name?

Is there a way to get elements with a class with a CSS attribute selector?

Something like this:

[class.=className]

Upvotes: 0

Views: 458

Answers (2)

lrpe
lrpe

Reputation: 799

From MDN:

[attr~=value]

Represents elements with an attribute name of attr whose value is a whitespace-separated list of words, one of which is exactly value.

So this:

[class~=className]

targets all elements that has the class "className" regardless of whether is has other classes around it.

Upvotes: 2

BushBush Saur
BushBush Saur

Reputation: 44

You could use this selector:

[class="className"], [class^="className "], [class$=" className"], [class*=" className "]

It's a bit long because we need to check whether it's just that class, whether it's at the beginning of the class attribute, the end, or in the middle.

Upvotes: 1

Related Questions