05. Attribute Selectors

We can select elements based on their attributes, giving us a more power and flexiblity. Oftentimes this means that elements won't necessarily have to have an id or class. This is particularly useful to target an input type or a submit button.

Attribute Existence [ ]

To check whether an attribute exists, we can use the existence symbol. Simply use square brackets []:
abbr[title] {
  border: black solid 1px;
}

If an <abbr> tag has a title attribute, it will be given a border.

Attribute Equality [=]

We can also check to see if the attribute a specific value.

input[type="text"] {
  color: red;
}

This will select all <input> elements whose type attribute is set to text.

From a Group of Attribute Values [~=]

This matches a specific attribute whose value appears in a space-separated list of words.

img[alt~="pony"] {
  color: red;
}

This would choose all img elements that have an alt attribute with the word pony.

See the substring selector below if you don't want to be restricted to space-separated words.

Starts with Selector [^=]

We may also choose an attribute based on the prefix of its value, with the [^=] symbol. For example, this can be useful for differentiating between external and internal links.

[href^="http://"] { 
  color: red;
}

This would choose all outbound links from your website and color the text red.

Ends with Selector [$=]

Now to choose an attribute based on the suffix (end), we use [$=].

[href$=".pdf"] {
  color: blue;
}

This would select all PDF files and color them blue.

Selecting Attributes matching a Substring [*=]

To select attributes based on a substring, we use [*=].

Unlike the group attribute value selector, this phrase does not have to be space-separated.

img[title*="f"]{
  color: green;
}

As long as the img tag as the letter "f" in its title attribute, we color it green.

Multiple Attribute Selectors

We can also select multiple attributes at the same time by just placing them after another.

a[href^="http://"][href*="helloWorld"] {
  color: purple;
}

This will select all outbound anchor elements with helloWorld somewhere in its link.

Build modern and responsive webpages

The Modern Web

Build modern and responsive webpages Try Front-end

The Modern Web will guide you through the most important tools of device-agnostic web development, including HTML5, CSS3, and JavaScript. The plain-english explanations and practical examples emphasize the techniques, principles, and practices that you'll need to easily transcend individual browser quirks and stay relevant as these technologies are updated.

$ Check price
34.9534.95Amazon 4.5 logo(30+ reviews)

More Front-end resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Ad