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.

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Build modern and responsive webpages

JavaScript & JQuery

Build modern and responsive webpages Try Front-end

This book was written for anyone who wants to use JavaScript to make their websites a little more interesting, engaging, interactive, or usable. In particular, it is aimed at people who do not have a degree in computer science (well, not yet anyway).

$ Check price
39.9939.99Amazon 4.5 logo(385+ reviews)

More Front-end resources

Ad