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.

  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.

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

Build modern and responsive webpages

High Performance Web Sites

Build modern and responsive webpages Try Front-end

Want your web site to display more quickly? This book presents 14 specific rules that will cut 25% to 50% off response time when users request a page. Author Steve Souders, in his job as Chief Performance Yahoo!, collected these best practices while optimizing some of the most-visited pages on the Web.

$ Check price
29.9929.99Amazon 4.5 logo(63+ reviews)

More Front-end resources