{"id":2937,"date":"2011-11-10T10:59:33","date_gmt":"2011-11-10T15:59:33","guid":{"rendered":"http:\/\/www.ericfeminella.com\/blog\/?p=2937"},"modified":"2016-03-30T14:57:47","modified_gmt":"2016-03-30T18:57:47","slug":"css3-attribute-selectors","status":"publish","type":"post","link":"https:\/\/www.ericfeminella.com\/blog\/2011\/11\/10\/css3-attribute-selectors\/","title":{"rendered":"CSS3 Attribute Selectors"},"content":{"rendered":"<p>The power of <a href=\"http:\/\/www.w3.org\/TR\/selectors\/\" title=\"CSS3 Selectors Module\" target=\"_blank\">CSS Selectors<\/a> can not be understated; for, without them, there would be no simple means by which developers could target specific elements for styling in a manner abstracted from, or external to, the actual markup to which the styles will bind.<\/p>\n<p>In addition to some of the more common <a href=\"http:\/\/www.w3.org\/TR\/selectors\/#simple-selectors\" title=\"CSS3 Simple Selectors\" target=\"_blank\">Simple Selectors<\/a>, such as <a href=\"http:\/\/www.w3.org\/TR\/selectors\/#type-selectors\" title=\"CSS3 Type Selectors\" target=\"_blank\">Type Selectors<\/a>, <a href=\"http:\/\/www.w3.org\/TR\/selectors\/#class-html\" title=\"CSS3 Class Selectors\" target=\"_blank\">Class Selectors<\/a> and <a href=\"http:\/\/www.w3.org\/TR\/selectors\/#id-selectors\" title=\"CSS3 Id Selectors\" target=\"_blank\">Id Selectors<\/a>, we have have <a href=\"http:\/\/www.w3.org\/TR\/selectors\/#attribute-selectors\" title=\"CSS3 Attribute Selectors\" target=\"_blank\">Attribute Selectors<\/a>, which, as the name implies, allow us to match elements based on their <a href=\"http:\/\/dev.w3.org\/html5\/spec\/Overview.html#attributes\" title=\"HTML5 Element Attributes\" target=\"_blank\">attributes<\/a>.<\/p>\n<section id=\"attribute-presence-value-selectors\">\n<h2>Attribute Presence and Value Selectors<\/h2>\n<p>CSS2 introduced four Attribute Selectors; referred to as <em>Attribute Presence and Value Selectors<\/em>, which allow for coarse grained matching of specific elements based on their attributes and \/ or attribute values. These include the following:<\/p>\n<div class=\"post-datalist\">\n<dl>\n<dt>e[attr]<\/dt>\n<dd>Where <em>e<\/em> is an element and <code>[attr]<\/code> is an attribute of element <em>e<\/em>. For example, <code>p[title]<\/code> would match all <code>p<\/code> tags with a <code>title<\/code>, regardless of the value of the <code>title<\/code>.<\/p>\n<pre lang=\"javascript\">\r\n\/* \r\n Matches all <p> tags with a title and changes their \r\n background color to red with white text \r\n *\/\r\np[title]{\r\n    background-color: red;\r\n    color: white;\r\n}\r\n<\/pre>\n<\/dd>\n<dt>e[attr=val]<\/dt>\n<dd>Where <em>e<\/em> is an element and <code>[attr=val]<\/code> represent an attribute of element <em>e<\/em> which contains the exact value of <code>val<\/code>. For example, <code>p[title=\"Example 1\"]<\/code> would match all <code>p<\/code> tags with a <code>title<\/code> which equals &#8220;Example 1&#8221; exactly.<\/p>\n<pre lang=\"javascript\">\r\n\/* \r\n Matches all <p> tags with a title equal to \"Example 1\" \r\n and changes their background color to green and text \r\n color to white \r\n *\/\r\np[title=\"Example 1\"]{\r\n    background-color: green;\r\n    color: white;\r\n} \r\n<\/pre>\n<\/dd>\n<dt>e[attr~=val]<\/dt>\n<dd>Where <em>e<\/em> is an element and <code>[attr~=val]<\/code> is an attribute of element <em>e<\/em> which has a value containing a whitespace-separated list of words, one of which equals <code>val<\/code> exactly. For example, <code>p[title~=\"Example-1a\"]<\/code> would match all <code>p<\/code> tags with a <code>title<\/code> containing the word &#8220;Example-1a&#8221; in a list of whitespace delimited words.<\/p>\n<pre lang=\"javascript\">\r\n\/* \r\n Matches all <p> tags with a title containing the exact \r\n word to \"Example-1a\" and changes their background color \r\n to black and text color to red \r\n *\/\r\np[title~=\"Example-1a\"]{\r\n    background-color: black;\r\n    color: red;\r\n}\r\n<\/pre>\n<\/dd>\n<dt>e[attr|=val]<\/dt>\n<dd>Where <em>e<\/em> is an element and <code>[attr|=val]<\/code> is an attribute of element <em>e<\/em> that has a value of <code>val<\/code> exactly, or begins with <code>val<\/code> immediately followed by a hyphen &#8220;-&#8220;. For example, <code>p[title!=\"Example\"]<\/code> would match all <code>p<\/code> tags with a <code>title<\/code> containing the word &#8220;Example-&#8220;, followed by any other value, such as &#8220;Example-1&#8221;, &#8220;Example-A&#8221;, etc..<\/p>\n<pre lang=\"javascript\">\r\n\/* \r\n Matches all <p> tags with a title containing the word \r\n to \"Example-\" and changes their background color to blue \r\n and text color to white \r\n *\/\r\np[title|=\"Example\"]{\r\n    background-color: blue;\r\n    color: white;\r\n}\r\n<\/pre>\n<\/dd>\n<\/dl>\n<p><a href=\"http:\/\/code.ericfeminella.com\/articles\/examples\/css3\/selectors\/attribute-presence.html\" target=\"_blank\" title=\"Attribute Presence and Value Selectors Example\">View Example<\/a>\n<\/div>\n<\/section>\n<section id=\"attribute-substring-matching-selectors\">\n<h2>Substring Matching Attribute Selectors<\/h2>\n<p>In addition to the above <em>Attribute Presence and Value Selectors<\/em>, CSS3 expands on this by defining three additional <a href=\"http:\/\/www.w3.org\/TR\/selectors\/#attribute-substrings\" target=\"_blank\">Attribute Selectors<\/a>; referred to as <em>Substring Matching Attribute Selectors<\/em>. These additions allow for fine grained matching of specific elements based on their attribute values. <\/p>\n<p>In simplest terms, the new Attribute Selectors in CSS3 can be used to match an element with a given attribute whose value begins, ends or contains a certain value. The following is a basic description and example of each new Attribute Selector:<\/p>\n<div class=\"post-datalist\">\n<dl>\n<dt>e[attr^=val]<\/dt>\n<dd>Where <em>e<\/em> is an element and <code>[attr^=val]<\/code> is an attribute of element <em>e<\/em> which contains a value that begins with <em>val<\/em>. <\/p>\n<pre lang=\"javascript\">\r\n\/* Matches all linked resources sent over https *\/\r\na[href^=\"https\"]{\r\n    color: red;\r\n}\r\n<\/pre>\n<\/dd>\n<dt>e[attr$=val]<\/dt>\n<dd>Where <em>e<\/em> is an element and <code>[attr$=val]<\/code> represent an attribute of element <em>e<\/em> which contains a value that ends with <code>val<\/code>.<\/p>\n<pre lang=\"javascript\">\r\n\/* Matches all anchor tags to .html documents *\/\r\na[href$=\".html\"]{\r\n    color: green;\r\n}\r\n<\/pre>\n<\/dd>\n<dt>e[attr*=val]<\/dt>\n<dd>Where <em>e<\/em> is an element and <code>[attr*=val]<\/code> is an attribute of element <em>e<\/em> which has a value that contains <code>val<\/code>.<\/p>\n<pre lang=\"javascript\">\r\n\/* Matches all anchor tags which contain a query string *\/\r\na[href*=\"?\"]{\r\n    color: blue;\r\n}\r\n<\/pre>\n<\/dd>\n<\/dl>\n<p><a href=\"http:\/\/code.ericfeminella.com\/articles\/examples\/css3\/selectors\/substring-matching.html\" target=\"_blank\" title=\"Substring Matching Attribute Selectors Example\">View Example<\/a>\n<\/div>\n<\/section>\n<p>To summarize, there are a total of seven Attribute Selectors in CSS3, three of which are new. Whether used for general matches, such as global Attributes; e.g. <code>*[hreflang|=en]<\/code> or more specific matches, such as chaining; e.g, <code>a[href^=\"https\"][target=\"_blank\"]<\/code>, Attribute Selectors provide a powerful mechanism for selecting both general and specific content within a page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The power of CSS Selectors can not be understated; for, without them, there would be no simple means by which developers could target specific elements for styling in a manner abstracted from, or external to, the actual markup to which the styles will bind. In addition to some of the more common Simple Selectors, such as Type Selectors, Class Selectors&#8230; <a class=\"read-more\" href=\"https:\/\/www.ericfeminella.com\/blog\/2011\/11\/10\/css3-attribute-selectors\/\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[133,62,59,78],"tags":[162,66,163,161,65,125,126],"class_list":["post-2937","post","type-post","status-publish","format-standard","hentry","category-browsers","category-css3","category-html5","category-javascript-2","tag-css","tag-css3-2","tag-css3-attribute-selectors","tag-css3-selectors","tag-html5-2","tag-html5-elements-2","tag-html5-semantics-2"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/2937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/comments?post=2937"}],"version-history":[{"count":0,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/2937\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/media?parent=2937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/categories?post=2937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/tags?post=2937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}