XML technologies / CSS / CSS Cookbook / General information / Different selectors for assigning styles / Attribute selectors

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Attribute selectors offer four ways to target an element containing a matching attribute. The following examples show the options:

[Attribut] – Matches based on the attribute name.

a[href] {
 text-decoration: none;
}

Whenever the HTML element a contains the attribute href, the link is not underlined.

[Attribut="Wert"] – Matches based on the attribute value.

a[href="csscookbook.com"] {
 text-decoration: none;
}

Whenever a link to csscookbook.com appears in the HTML code, it is displayed without underlining.

[Attribut~="Wert"] – Selects any element based on one part of its space-separated attribute values.

a[title~="digital"] {
 text-decoration: none;
}

Whenever the attribute title of an a element contains the string “digital”, the link is displayed without underlining.

[Attribut|="Wert"] – Matches when the attribute value starts with “Wert” followed by a hyphen.

a[href|="digital"] {
 text-decoration: none;
}

If an anchor’s href element contains the string “digital”, the link is displayed without underlining.

Warning:
Attribute selectors are not always supported, even in modern browsers, especially Internet Explorer 6 for Windows. Mozilla, Firefox, Opera 5+ and Safari, however, handle them correctly.

  

 

 

 

back to top