{"id":109,"date":"2008-04-30T07:00:06","date_gmt":"2008-04-30T12:00:06","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=109"},"modified":"2008-04-30T07:00:06","modified_gmt":"2008-04-30T12:00:06","slug":"css-attribute-selectors","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/css-attribute-selectors\/","title":{"rendered":"CSS Attribute Selectors: Built-In Classes"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/04\/lillypad-400.jpg\" alt=\"\" title=\"'Lilly Pad Reflections' by MrClean1982. Used under a Creative Commons license.\" width=\"400\" height=\"184\" class=\"alignnone size-full wp-image-110\" \/><\/p>\n<p>If you\u2019ve been a CSS groupie or follower of this website for any length of time, you should have a fairly firm grasp on the concept of using classes in XHTML as \u201chooks\u201d into your pages (and if not, <a href=\"\/when-to-use-css-ids-and-classes\/\" title=\"When to Use IDs and Classes\">this article is a good place to catch up<\/a>). But classes and IDs aren\u2019t the only ways you can describe the elements in your website in order to style them. You can also make use of attribute selectors.<\/p>\n<p>Attributes are the property=\u201dvalue\u201d pairs inside of most XHTML elements that further describe that element. Take the anchor tag, for example:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;a href=&quot;page.html&quot; title=&quot;Click to visit!&quot;&gt;Text&lt;\/a&gt;<\/pre>\n<p>This anchor tag has two attributes: href and title. Those attributes also have very specific values associated with them. And it\u2019s these attributes, and their values, that make attribute selectors work.<\/p>\n<p>By using attribute selectors in your CSS, you\u2019re able to target elements with specific attributes, or even specific values within those attributes. When using attribute selectors, the attribute is contained within [brackets], just like how .classes have a leading period, or #ids have a leading pound sign. There are four ways to use attribute selectors:<\/p>\n<p>The <strong>[attribute]<\/strong> syntax is the least specific of the four. It allows you to target any element with a specific attribute set, regardless of its value. For example:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">a[title] {\n\tfont-weight: bold;\n\tcursor: help;\n}<\/pre>\n<p>This little bit of CSS would target any anchor tag that had a title attribute set, regardless of what the title contained. My CSS here makes the anchor boldfaced, and changes the cursor to a question mark when the user hovers over the link. This could be useful to indicate to the user that more information (i.e., the tool tip) is coming.<\/p>\n<p>The most specific syntax is <strong>[attribute=\u201dvalue\u201d]<\/strong>. Using this selector, you\u2019d be able to apply specific styles to elements with specific values set. So if, for example, you had an anchor that pointed to a specific website you wanted to emphasize:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">a[href=\"http:\/\/cssnewbie.com\/\"] {\n\tfont-size: 120%;\n\tcolor: red; \n}<\/pre>\n<p>This code would add a bit of emphasis to any link going to the front page of CSSnewbie. I would recommend copying and pasting this code into any website you happen to be working on at the moment. :)<\/p>\n<p>The <strong>[attribute~=\u201dvalue\u201d]<\/strong> syntax allows you to look within the attribute\u2019s values and find a specific value within a space-separated list of values. Alternatively, you can use the related <strong>[attribute|=\u201dvalue\u201d]<\/strong> syntax to find parts of a value that are separated by hyphens (instead of spaces). So, for example, if I had an image with an alt tag that read \u201cGallery: Wombats Gone Wild,\u201d I could style this gallery (and all other galleries that followed this same pattern) like so:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">img[alt~=\"Gallery:\"] {\n\tborder: 2px solid #333;\n}<\/pre>\n<p>And suddenly, all of your images that link to galleries have special styling, without having to add any additional classes! And that\u2019s really the beauty of attribute selectors: if used properly, they can be a great help in <a href=\"\/combating-classitis\/\">the fight against Classitis<\/a>. <\/p>\n<p>Of course, attribute selectors don\u2019t work everywhere (why would they? They\u2019re too cool for school). Here\u2019s where attribute selectors earn a big fat FAIL:<\/p>\n<ul>\n<li>IE6 or below on the PC<\/li>\n<li>IE5 or lower on the Mac<\/li>\n<li>Netscape 4 or under (i.e., people in 1997)<\/li>\n<li>Both people still using Opera 3<\/li>\n<\/ul>\n<p>Luckily, very few people are still using Opera 3, Netscape 4, or IE5. And with the recent release of IE7 (and the planned future release of IE8), Internet Explorer version 6 should be on its way out the door, as well. In other words, attribute selectors are fast becoming a reasonable means of styling your websites.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<\/p>\n<p>If you\u2019ve been a CSS groupie or follower of this website for any length of time, you should have a fairly firm grasp on the concept of using classes in XHTML as \u201chooks\u201d into your pages (and if not, this [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/css-attribute-selectors\/\" title=\"Click to read 'CSS Attribute Selectors: Built-In Classes'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[173,182],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}