{"id":14204,"date":"2011-09-06T20:07:18","date_gmt":"2011-09-07T03:07:18","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=14204"},"modified":"2013-04-03T13:57:43","modified_gmt":"2013-04-03T20:57:43","slug":"id","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/i\/id\/","title":{"rendered":"ID"},"content":{"rendered":"
The ID attribute values should be unique. HTML with two or more identical ID selectors are extremely powerful<\/em>. They have a very high specificity<\/a>, generally written as (0, 1, 0, 0). Styles applies with them override other selectors that only use tags or classes. To demonstrate:<\/p>\n A paragraph with both an ID and class attribute is being given contradicting CSS rules; even though the class selector ( High specificity and uniqueness mean using That being said, ID attributes have several valuable uses outside of CSS:<\/p>\n The #id selector allows you to target an element by referencing the id HTML attribute. Similar to how class attributes are denoted in CSS with a \u201cperiod\u201d (.) before the class name, ID attributes are prefixed with an \u201coctothorpe\u201d (#), more commonly known as a \u201chash\u201d or \u201cpound sign\u201d. <header id=”site-header”><\/header> #header { \/* this […]<\/p>\n","protected":false},"author":1036,"featured_media":0,"parent":14202,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page-almanac-single.php","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":""},"tags":[],"acf":[],"jetpack-related-posts":[{"id":14152,"url":"https:\/\/css-tricks.com\/almanac\/selectors\/a\/attribute\/","url_meta":{"origin":14204,"position":0},"title":"[attribute]","date":"September 6, 2011","format":false,"excerpt":"There are lots of ways you can select elements in CSS. The most basic selection is by tag name, like p { }. Almost anything more specific than a tag selector uses attributes \u2014 class and ID both select on those attributes on HTML elements. But class and ID aren't\u2026","rel":"","context":"With 26 comments","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4129,"url":"https:\/\/css-tricks.com\/snippets\/jquery\/working-with-attributes\/","url_meta":{"origin":14204,"position":1},"title":"Working with Attributes","date":"September 18, 2009","format":false,"excerpt":"Setting Single Attribute $(\"img\").attr(\"src\", \"\/images\/banner.jpg\"); Setting Single Attribute (with function) $(\"div\").attr(\"id\", function (arr) { return \"div-id\" + arr; }) Setting Multiple Attributes $(\"img\").attr({ src: \"\/images\/banner.jpg\", title: \"banner\", alt: \"banner\" }); Getting Attribute var $source = $(\"img\").attr(\"src\");","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6241,"url":"https:\/\/css-tricks.com\/snippets\/jquery\/click-input-when-label-clicked\/","url_meta":{"origin":14204,"position":2},"title":"Trigger Click on Input when Label is Clicked","date":"April 22, 2010","format":false,"excerpt":"Labels should have \"for\" attributes that match the ID of the input they are labeling. This means we can snag that attribute and use it in a selector to trigger a click on the input itself. Assuming of course you have some reason to watch for clicks on inputs. var\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":14164,"url":"https:\/\/css-tricks.com\/almanac\/selectors\/c\/class\/","url_meta":{"origin":14204,"position":3},"title":"Class","date":"September 6, 2011","format":false,"excerpt":"A class selector in CSS starts with a dot (.), like this: .class { } A class selector selects all elements with a matching class attribute. For example, this element: #id<\/code> selector allows you to target an element by referencing the
id<\/code> HTML attribute. Similar to how class attributes are denoted in CSS with a \u201cperiod\u201d (
.<\/code>) before the class name, ID attributes are prefixed with an \u201coctothorpe\u201d (
#<\/code>), more commonly known as a \u201chash\u201d or \u201cpound sign\u201d.<\/p>\n
<header id=\"site-header\"><\/header><\/code><\/pre>\n
#header { \/* this is the ID selector *\/\r\n background: #eee;\r\n}<\/code><\/pre>\n
id<\/code>s does not validate, and will produce unpredictable results. If there are two of the same, CSS will still match and style both. JavaScript however, when querying for an ID, will find the first and stop.<\/p>\n
<\/code>Check out this Pen!<\/a><\/pre>\n
.reusable<\/code>) is below the ID selector (
#unique<\/code>) in the CSS (it would generally overrides styles above it in the \u201ccascade\u201d<\/a>), the paragraph stays red because
#unique<\/code> overwhelms the blue color being set by
.reusable<\/code>. An infinite amount of classes can never beat the specificity of the ID (although there was a bug at one time where 256 classes would beat an ID).<\/p>\n
#id<\/code> is a CSS \u201cnuclear option\u201d: over-powered, inflexible and disproportionately effective. Avoiding the
#id<\/code> selector in CSS is considered<\/a> a best<\/a> practice<\/a>: it is preferable<\/a> to use a class in nearly<\/a> every case.<\/p>\n
\n
id<\/code> attributes can be targeted by anchor tags, by setting the
href<\/code> attribute to the
id<\/code> value, prefixed by a
#<\/code> symbol. Clicking that anchor link will re-focus the current page on the element with the matching
id<\/code>. This is called a \u201cfragment identifier\u201d<\/a>.<\/li>\n
label<\/code>s and
input<\/code>s.<\/li>\n<\/ul>\n
Points of Interest<\/h3>\n
\n
#id<\/code> cannot start with a number and must be at least one character long. A large part of the Unicode are valid<\/a> characters in an
id<\/code>.<\/li>\n
id<\/code> attributes and selectors are case-sensitive, and must exactly match across HTML, CSS and JavaScript<\/li>\n<\/ul>\n
Other Resources<\/h3>\n
\n
Browser Support<\/h3>\n
\n\n
\n Chrome<\/span><\/th>\n Safari<\/span><\/th>\n Firefox<\/span><\/th>\n Opera<\/span><\/th>\n IE<\/span><\/th>\n Android<\/span><\/th>\n iOS<\/span><\/th>\n<\/tr>\n<\/thead>\n\n \n Any<\/td>\n Any<\/td>\n Any<\/td>\n Any<\/td>\n Any<\/td>\n Any<\/td>\n Any<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"