Table of Contents

How to select elements based on attribute ? (with the selector API, css)

About

This page is about selector expressions that select elements based on attribute.

Example

Elements with an id and class attribute

#IdName.className  { color:blue; }
/* shorthand for */
*[id=IdName][class=className] { color:blue; }
<p id="IdName" class="className">The CSS style will be applied on this paragraph</p>
<p class="className">Not on this one, even if the class value matches</p>

Elements with a class attribute that has a specific value

p.className  { color:blue; }
/* equivalent to the tilde selector (ie the class value should have the word className word) */
p[class~=className]  { color:blue; }
<p class="className bar">The CSS style will be applied on this text because it's a p element where the class value has the <b>className</b> word</p>
<p class="foo">Not on this one because it has not the class word  <b>className</b></div>

Elements that have a class value that starts with

Notation example: A class that starts with the string foo

[class^="foo"]

Full example

*[class^="foo"] {
  color: red
}
<p>A paragraph without class</p>
<p class="foo-suffix other-class">A paragraph with a class that starts with foo</p>

Select elements with the role attribute as button

<button role="button" aria-label="Delete item 1">Delete</button>
*[role="button"] {
  /* the attribute selector */
	background-color:#44c767;
	border-radius:28px;
	display:inline-block;
	color:#ffffff;
        padding:16px 31px;
}
*[role="button"]:hover {
	background-color:#5cbf2a;
}
*[role="button"]:active {
	position:relative;
	top:1px;
}

Syntax

This paragraph shows you:

that you can use to select element based on attributes.

Value Equality

Exact equality

E[foo="bar"]
*[foo="bar"]

Contains a word (Tilde Equality Operator)

The tilde equality 1) is a equality operator that matches an element where the attribute whose value is a whitespace-separated list of words.

For instance, the below expression means that the foo attribute should contains the word bar in its value.

E[foo~="bar"]

Starts with followed by minus (Pipe Equality Operator)

The below expression 2) means that the foo attribute value:

E[foo|="bar"]

This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML ). For lang (or xml:lang) language subcode matching, please see the :lang() pseudo-class.

Other example on lang, the following selector represents an a element for which the value of the hreflang attribute:

a[hreflang|="en"]

Starts with (Caret equality operator)

Example:

E[foo^="bar"]

The Caret equality operator 3) represents an element with the att attribute whose value begins with the prefix “val” (If “val” is the empty string then the selector does not represent anything)

This start with operator permits to define a starts with comparison

Ends with (Dollar equality operator)

E[foo$="bar"]

The dollar equality operator represents an element with the att attribute whose value ends with the suffix “val” (If “val” is the empty string then the selector does not represent anything)

Contains (The star equality operator)

It represents an element with the att attribute whose value contains at least one instance of the substring “val” (If “val” is the empty string then the selector does not represent anything)

E[foo*="bar"]

Logical Operators

Have an attribute

E[foo]
*[foo]

Html example: the selector represents a span element whose class attribute has exactly the value example:

span[class="example"]

Not - Have not an attribute

With the not pseudo-class function

E:not([foo])
*:not([foo])

Example: Colors element without a class attribute

<p class="foo">A paragraph with only the class attribute</p>
<div id="foo">A div with only the id attribute</div>
body *:not([class]) { color: skyBlue };

And - Have attributes A and B

To create a AND operation, just add another attribute expression (expression between brackets).

Example:

span[hello="Cleveland"][goodbye="Columbus"]

where this selector represents a span element:

1) , 2)
https://www.w3.org/TR/selectors-4/#attribute-representation|Specification - Attribute presence and value selectors]]