| Version |
|---|
| CSS3 |
| IE7 | FF1+ | Saf1.3+ | Op9.5+ |
|---|---|---|---|
| None | Buggy | Full | Full |
Syntax
Description
CSS3 defines three more attribute selector variations. These new selectors give us the ability to make partial matches to attribute values—we can match strings at the start, end, or anywhere within an attribute value.
We can use the ^= operator to cause an attribute selector to match elements that have an attribute containing a value that starts with the specified value:
a[href^="http:"] {
⋮ declarations
}
This example matches a elements that have an
href attribute value which starts with the
characters "http:".
Using the $= operator, an attribute selector can match elements that have an attribute which contains a value ending with the specified value:
img[src$=".png"] {
⋮ declarations
}
This example matches img elements with a
src attribute value that ends with the characters
".png".
Finally, we can use the *= operator to make an attribute selector match elements that have an attribute which contains the specified value:
div[id*="foo"] {
⋮ declarations
}
This example matches div elements whose
id attribute value contains the characters
"foo".
Example
This example will match
a elements with an href
attribute that contains the string
"example.com":
a[href*="example.com"] {
⋮ declarations
}
Compatibility
| Internet Explorer | Firefox | Safari | Opera | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 5.5 | 6.0 | 7.0 | 1.0 | 1.5 | 2.0 | 1.3 | 2.0 | 3.0 | 9.2 | 9.5 |
| None | None | None | Buggy | Buggy | Buggy | Full | Full | Full | Buggy | Full |
Internet Explorer 7 doesn’t support the partial matching of attribute values.
Safari versions up to and including 3 will always ignore the case of HTML attribute values, even for attributes that are, in fact, case sensitive.
Firefox versions up to and including
2, will ignore the case of some attributes that should be compared in a
case-sensitive manner: for example, the id and
for attributes.
User-contributed notes
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.