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?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ 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

Ad