Skip to main content

CSS Attribute Selectors

With attribute selectors, you can target and style HTML elements by an attribute, by an attribute value, or by both. They are written inside square brackets [].

Here are the ones CSS provides:

  • [attribute] - Matches elements that have the attribute
  • [attribute="value"] - Matches elements whose attribute holds exactly that value
  • [attribute~="value"] - Matches elements whose attribute value includes a specific word
  • [attribute|="value"] - Matches elements whose attribute value is exactly the value, or begins with the value plus a hyphen (-)
  • [attribute^="value"] - Matches elements whose attribute value begins with a specific value
  • [attribute$="value"] - Matches elements whose attribute value finishes with a specific value
  • [attribute*="value"] - Matches elements whose attribute value has a specific value somewhere inside it

Tip: Matching is case-sensitive by default. Adding an i before the closing bracket, as in [attribute="value" i], makes it case-insensitive.


CSS [attribute] Selector

Simply having the attribute is enough for [attribute] to match an element.

In the example below, it picks out every <a> element that has a target attribute:

Example

Each <a> element that has a target attribute gets a yellow background.

Prompt
Demonstrate the CSS [attribute] selector. Use the selector "a[target]" to set background-color: yellow on all elements that have a target attribute. Include several links, some with a target attribute and some without, to show the difference.
Output: complete HTML page

CSS [attribute="value"] Selector

With [attribute="value"], the attribute must also hold one exact value.

In the example below, only <a> elements carrying target="_blank" are matched:

Example

Only <a> elements carrying target="_blank" end up with the yellow background.

Prompt
Demonstrate the CSS [attribute="value"] selector. Use the selector a[target="_blank"] to set background-color: yellow on all elements with target="_blank". Include several links with different target values (e.g., _blank, _self, _top) and some without a target attribute, to show that only target="_blank" links are styled.
Output: complete HTML page

CSS [attribute~="value"] Selector

Looking for one specific word within an attribute value is the purpose of [attribute~="value"].

In the example below, it matches elements whose title attribute is a space-separated list of words that includes "flower":

Example

A yellow border marks each element whose title attribute has the word "flower" in it.

Prompt
Demonstrate the CSS [attribute~="value"] selector. Use the selector [title~="flower"] to set a 5px solid yellow border on all elements whose title attribute contains the word "flower" as a separate word. Include elements with title="flower", title="summer flower", title="flower new", title="my-flower", and title="flowers" to show that only space-separated whole-word matches are selected.
Output: complete HTML page

CSS [attribute|="value"] Selector

An element matches [attribute|="value"] when the attribute value equals the specific value, or begins with that value and a hyphen (-) right after it.

Note: A whole word is required, either on its own like top, or with a hyphen (-) after it like top-text.

Example

A yellow background goes on elements whose class attribute value is exactly "top" or begins with "top-".

Prompt
Demonstrate the CSS [attribute|="value"] selector. Use the selector [class|="top"] to set background: yellow on elements whose class attribute value is exactly "top" or starts with "top" followed by a hyphen. Include elements with class="top", class="top-text", class="top-header", class="topcontent", and class="content" to show which ones match.
Output: complete HTML page