Forum Moderators: not2easy

Message Too Old, No Replies

CSS3 guide: 3.2. Attribute Selectors

         

swa66

9:27 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a post in a series, please see the Table of Contents [webmasterworld.com] for the other posts in the series.

3.2. Attribute Selectors

Attribute Selectors [w3.org] allow CSS authors to specify rules that match elements which have certain attributes defined in the source document, the first 4 were available in CSS2.1 and the second three are new to CSS3. support for them all is very good even in IE7.

The existing 2.1 Attribute Selectors

Syntax: [att]

    Matches elements to an attribute whatever the value of the attribute.

Syntax: [att="value"]

    Matches elements to an attribute that is exactly "
    value
    ".

Syntax: [att~="value"]

    Matches elements to an attribute that is a whitespace-separated list of words, one of which is exactly "
    value
    ".
    If "value" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "value" is an empty string, it will never represent anything.

Syntax: [attŠ="value"] (1)

    Matches elements to an attribute that is either exactly "value" or beginning with "value" immediately followed by "-"
    This is primarily intended to allow language subcode matches.

Attribute Selectors New to CSS3

Syntax: [att^="value"]

    Matches elements to an attribute that starts with "
    value
    ".

Syntax: [att$="value"]

    Matches elements to an attribute that ends with "
    value
    ".

Syntax: [att*="value"]

    Matches elements to an attribute that contains "
    value
    ".

Example:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>CSS2.1 and CSS3 Attribute Selector Tests</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<style type="text/css" media="screen">
div {border: 1px solid #abc; margin: 10px 0;}
p {margin: 10px;}
/* CSS2.1 attribute Selectors */
p[valign] {background: #ffa;}
p[bgcolor="red"] {background: #dad;}
p[class~="two"] {background: #aad;}
p[lang¦="x"] {background: #aaa;} /* change this pipe to an unbroken one */
/* New to CSS3 Attribute Selectors */
p[class^="one"] {background: #ffa;}
p[class$="2"] {background: #dad;}
p[class*="ee"] {background: #aae;}
</style>
</head>
<body>
<div>
<h3>CSS2.1 Attribute Selectors</h3>
<p valign="top">&lt;p valign="top"&gt; this should match p[valign]
and have a yellow background &lt;/p&gt;</p>
<p bgcolor="red">&lt;p bgcolor="red"&gt; this should match
p[bgcolor="red"] and have a purple background &lt;/p&gt;</p>
<p class="one two three">&lt;p class="one two three"&gt; this
should match p[class~="two"] and have a blue background &lt;/p&gt;</p>
<p lang="x-klingon">&lt;p lang="x-klingon"&gt; this should match
p[lang¦="x"] and have a grey background &lt;/p&gt;</p>
</div>
<div>
<h3>New to CSS3 Attribute Selectors</h3>
<p class="one-1">&lt;p class="one-1"&gt; this should match
p[class^="one"] and have a yellow background &lt;/p&gt;</p>
<p class="two-2">&lt;p class="two-2"&gt; this should match
p[class$="2"] and have a purple background &lt;/p&gt;</p>
<p class="three-3">&lt;p class="three-3"&gt; this should match
p[class*="ee"] and have a blue background &lt;/p&gt;</p>
</div>
<!--[if lt IE 7]>
<script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE7.js"
type="text/javascript"></script>
<![endif]-->
</body>
</html>
(2)

Support:

  • Very widely supported in browsers in use today, including IE7
  • not supported by IE6

Graceful fallback:
IE6 lack of support can be fixed by using IE7.js [code.google.com] making this usable if we want to.

Practical use:
Could be used to target older legacy code that still contains deprecated attributes e.g. <font color="red"> or it could be used to target only certain external link types so relevant icons can be added as background images e.g. a[href$=".pdf"] to add a PDF image link icon



NOTES
(1): The pipe characters in the above post should not be broken pipes, this BB's formatting breaks them.. so please replace all instances of the broken pipe with a solid pipe character, thanks
(2): This html will not validate, it's intended to show how one can use these selectors ewhen having no control over the html.

swa66

9:26 pm on Jun 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



reserved for future use