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.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top