a:2:{i:0;a:1:{s:4:"data";a:1:{s:7:"entries";a:1:{i:0;a:7:{s:2:"id";s:3:"289";s:5:"title";s:33:"CSS Attribute Selectors Explained";s:11:"displayDate";s:10:"04.14.2007";s:13:"attributeDate";s:10:"2007-04-14";s:4:"body";s:5428:"
So, I had played around with CSS attribute selectors a little bit before I went to An Event Apart Boston, but I have a much better grasp of them now. I gave a brief overview in my An Event Apart Boston Summary.
Currently, you really have to do some digging to figure out exactly how to use them, but I hope this article explains them all better.
Basically, attribute selectors allow you to target elements based on their attributes (i.e. alt, href, title, etc.). In the table below, you can see all the different options for attribute selectors.
Ok, so hopefully the general information makes them a little clearer, and I hope the following examples make them very easy to use.
Below, I will give an example of each attribute selector and how it can be used.
Ok, so say maybe you want to style anchors that have the href attribute set to something. This may help differentiate JavaScript anchors.
The CSS you want to add is like so:
a { text-decoration: underline; }
a[href] {
border-bottom: 1px dotted #999;
text-decoration: none;
}
That will remove the underline, and add a grey bottom border to anything that has the href attribute set.
Ok, so maybe you have added a green border to all input elements, but then you don’t want them on radio buttons. So you add the following CSS:
input { border: 1px solid green; }
input[type="radio"] { border: none; }
That will add a green border to all input elements except for radio buttons.
Use the same example that you want to add a green border to all input elements except for radio buttons, checkboxes, and submit buttons. The CSS you need is:
input { border: 1px solid green; }
input[type~="radio checkbox submit"] { border: none; }
So you get a green border on all input elements except radio buttons, checkboxes, and submit buttons.
Ok, so maybe you want to add a Google favicon to every link to Google. Add the following (pretending you have a Google favicon):
a[href*="google"] {
background: url(/images/googleIcon.gif) no-repeat 100% 50%;
padding-right: 25px;
}
So now any link to Google, http://google.com/analytics, http://google.com/sitemaps, etc. will have the Google favicon.
Let’s add an external link to all links that start with http
a[href^="http"] {
background: url(/images/external.gif) no-repeat 100% 50%;
padding-right: 25px;
}
Now every link that starts with http will have an external link icon.
Ok, now we should display an icon for any file types that we are linking to: pdf, doc, xls.
a[href$=".pdf"], a[href$=".doc"], a[href$=".xls"] {
background-position: 100% 50%;
background-repeat: no-repeat;
padding-right: 19px;
}
a[href$=".pdf"] { background-image: url(/images/pdf.gif); }
a[href$=".doc"] { background-image: url(/images/doc.gif); }
a[href$=".xls"] { background-image: url(/images/xls.gif); }
This is one of the coolest things for me. No more adding extraneous markup in order to add an icon that shows the file type when linking to a file.
I must say, that this works in all browsers, except IE 6 of course. But if you use the Dean Edwards IE7 Script and include the CSS 3 selectors, it will work. I will eventually give a rundown of how to use that script, because I find the documentation a little hard to read. So I hope everyone enjoys attribute selectors as much as I do.
";s:13:"numberOfLikes";N;s:10:"bodyBlocks";a:0:{}}}}}i:1;O:25:"yii\caching\TagDependency":3:{s:4:"tags";a:4:{i:0;s:7:"element";i:1;s:29:"element::craft\elements\Entry";i:2;s:40:"element::craft\elements\Entry::section:4";i:3;s:7:"graphql";}s:4:"data";a:4:{s:40:"CraftCMSce35088bdfe0816226cd17fd051a4803";s:21:"0.46162700 1707493692";s:40:"CraftCMS2743a789e8993267a348eee1ee7e4450";s:21:"0.34758900 1706282441";s:40:"CraftCMS2ac34726f299f7e18e449d8e536c67f8";s:21:"0.22771600 1711953912";s:40:"CraftCMS3817d4a648fcfac939af46605323feb0";s:21:"0.87777500 1713035607";}s:8:"reusable";b:0;}}