Select Page

Attribute Selectors


From targeting elements based on their attributes to styling based on partial values, attribute selectors offer flexibility and control. Elevate your CSS skills as we dive into the nitty-gritty of attribute selectors.


nanadwumor

May 10, 2024

Attribute Selectors in CSS


  • CSS 2 introduces attribute selectors for styling elements based on attributes and their values.
  • Simple selectors style elements with specific attributes; multiple attributes can also be used.
  • Exact value selectors apply styles based on specific attribute values.
  • Partial-match selectors include pipe-equals and tilde-equals, matching values with hyphens or space-separated words.

RECOMMENDED ARTICLES


Combining a class and pseudo-class
Combining a class and pseudo-class

Can you  combine class and pseudo-class to style elements? Learn how to precisely target and style elements with advanced pseudo-classes in CSS! Combining classes and pseudo-classes styles elements based on both their type and class, such as...

Types of Pseudo-classes
Types of Pseudo-classes

Know the types of Pseudo-classes, master CSS styling with pseudo-classes for precise element targeting and design control! Structural pseudo-classes style elements based on their position or hierarchy in the HTML document, like...

How to use Pseudo-classes in CSS
How to use Pseudo-classes in CSS

Discover how pseudo-classes in CSS enhance user interaction and styling! Pseudo-classes apply styles based on user interaction, element position, or relationships between elements. Normal classes are applied directly as attributes, whereas...


In CSS 2, attribute selectors were introduced. Attribute Selectors can be used to select elements based on their attributes and value of the attributes. 

There are four basic types of attribute Selectors.

These are simple attribute selectors, exact attribute value selectors, partial-match attribute value selectors, and leading-value attribute selectors.

Simple Attribute Selectors

It is possible to style HTML elements that have specific attributes. For example, in the code below we style the <a> element which has an attribute called target to have a red background. This means

Output

attribute selectors in css

More than one attribute can also be used to select an element to style. 

For example, we can select the <a> element that has both href and target attribute using the style:

Output

attribute selectors using href and target attributes

Selecting an element using attribute and value

We can also select an element for styling by using the attribute and its value. 

For example, let’s say you want to boldface any hyperlink that points to a certain document on the web server. This would look something like the following:

Output

attribute selectors using full attribute and value

Combining more than one attribute and values

Just as we can combine more than one attributes, we can also combine more than one attributes and values. For example,

Output

Attribute Selectors

The above will style the <a> element with the exact attributes

Note that ID selectors and attribute selectors that use the id attribute are not

exactly the same. That is, there exists difference between div#main-section and div[id=”main-section”].

Selection using Partial Attribute Values

You don’t have to know the exact attribute and value of an element before you use it to style the element. You can select element by knowing just some of the characters in the attribute or value.

(1) The Pipe-equals selector

This attribute selector selects an element which has the specified attribute and whose value is exactly the specified value, or the specified value followed by hyphen (-). For example,

Output

attribute selectors using pipe equals to value

The first three <p> elements will appear red. The last <p> element has “greeting” characters in its value but it won’t be affected because there’s no hyphen between the “greeting” and “Anyday”. 

This attribute selector is commonly used to match language values.

(2) The tilde-equals Selector

This attribute selector selects an element which has the specified attribute and a specified word, or in space-separated value. For example,

Output

attribute selectors-selecting an image in css

We observe that only the first and third <img> elements are targeted by the style because in the first, “language” is separated from preceding word by space, in the third, it’s only “language” which is not connected to any other word. In the second <img>, the word “language” is connected to “python” by hyphen so it’s not affected. 

The example above will match elements with title=~”object-oriented programming language”, title~=”language of choice”, but won’t match title ~=”best_language”.