{"id":38879,"date":"2016-07-31T09:37:23","date_gmt":"2016-07-31T04:07:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=38879"},"modified":"2016-08-01T17:29:41","modified_gmt":"2016-08-01T11:59:41","slug":"mastering-css-specificity","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/mastering-css-specificity\/","title":{"rendered":"Mastering CSS Specificity"},"content":{"rendered":"<p>CSS Specificity is one of the most confusing concept in Cascading Style Sheets and that&#8217;s the main reason, many <a title=\"Front End Developers\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">Front End Developers<\/a> avoid this for as long as possible.<\/p>\n<blockquote>\n<p class=\"p1\"><strong><em>Specificity is a weight that is applied to a given CSS\u00a0declaration and is determined by the number of each selector type in the matching selector.<\/em><\/strong><\/p>\n<\/blockquote>\n<p class=\"p1\">Basically, it&#8217;s a mechanism within the CSS cascade, which helps browsers in conflicts resolution. Suppose, you have two (or more) conflicting CSS rules that are being applied to the same element, then there are going to be some basic rules which a browser will follow to determine which one is most <b>specific<\/b> and that particular CSS\u00a0declaration will win.<\/p>\n<p class=\"p1\">Let&#8217;s start with some basic examples.<\/p>\n<p class=\"p1\"><strong>Example 1<\/strong><\/p>\n<p class=\"p1\"><code>p{ color: red;}<br \/>\np{color:blue;}<\/code><\/p>\n<p class=\"p1\">Here, the color of paragraph text will be blue as it came last in the style sheet and will override the above<span class=\"Apple-converted-space\">\u00a0 <\/span><strong>p <\/strong>declaration.<\/p>\n<p class=\"p1\"><strong>Example 2<\/strong><\/p>\n<p class=\"p1\"><code>&lt;p class=\"text-red\"&gt;text here&lt;\/p&gt;<br \/>\n.text-red{color: red;}<br \/>\np{ color: blue }<\/code><\/p>\n<p class=\"p1\">In this example, it might seem that <strong>\u00a0<em>paragraph text<\/em><\/strong><span class=\"Apple-converted-space\">\u00a0\u00a0<\/span>would be colored <em><strong>blue<\/strong><\/em>, as the declaration to color p text blue comes last, but \u00a0<strong><em>paragraph text<\/em>\u00a0<\/strong>would actually be colored <em><strong>red<\/strong><\/em> due to the <em><b>specificity<\/b><\/em> of the first selector. Here, the specificity weight of first selector is <em><strong>10,<\/strong><\/em> while specificity weight of second selector <strong>1<\/strong><em>.<\/em> Hence, the preference will be given to first selector.<\/p>\n<p class=\"p1\"><strong>Specificity Ranking.<\/strong><\/p>\n<p class=\"p1\">Every selector has been given a particular rank. Inline style lies on top, whereas elements and pseudo elements lies in bottom.<\/p>\n<p class=\"p3\">Inline styles &gt; IDs &gt; Classes and pseudo-classes &gt; Elements and pseudo-elements<\/p>\n<p class=\"p1\"><strong>How to calculate specificity ?<\/strong><\/p>\n<p class=\"p4\">To find the actual specificity of a group of nested selectors takes some calculation. Basically, every <em><strong>inline style<\/strong><\/em> has a specificity of 1000, every <em><b>ID selector<\/b><\/em>\u00a0has a value of <b>100<\/b>, every <b><em>class selector<\/em>\u00a0<\/b>has a value of <b>10<\/b> and every <em><strong>elements and pseudo elements<\/strong><\/em> \u00a0has a value of <b>1<\/b>. Finally, we add them up to get the specificity weight.<\/p>\n<p class=\"p4\">Examples: &#8211;<\/p>\n<ul class=\"ul1\">\n<li class=\"li1\">a has a specificity of <b>1<\/b> (1 element\u00a0selector)<\/li>\n<li class=\"li1\">div p has a specificity of <b>2<\/b> (2 element\u00a0selectors, 1+1)<\/li>\n<li class=\"li1\">.active has a specificity of 10(1 class selector)<\/li>\n<li class=\"li1\">#nav has a specificity of 100 (1 id selector)<\/li>\n<li class=\"li1\">#nav li.active a has a specificity of 112 (1 id selector + 1 class selector + 2 elements selector)<\/li>\n<\/ul>\n<p class=\"p1\"><strong>Some Key Points<\/strong><\/p>\n<p class=\"p1\">a) In case of conflicting styles declaration, the declaration with higher specificity will win regardless of order.<br \/>\nb) Always try to use\u00a0<em><b>IDs to increase the specificity<\/b><\/em> as its specificity is 100.<br \/>\nc) The universal selector (*) has no specificity value.<br \/>\nd) Pseudo-classes has specificity of 10 while pseudo-elements (e.g. :first-line) has a specificity of 1.<br \/>\ne) The pseudo-class :not() adds no specificity by itself, only elements inside its parenthesis will add to specificity weight.<br \/>\ng) When an !important rule is used on a style declaration, it will override any other declarations.<br \/>\nh) The only way an !important value can be overridden is with another !important rule declared later in the CSS and with equal or great specificity value .<\/p>\n<p class=\"p1\">Happy coding \u00a0\ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSS Specificity is one of the most confusing concept in Cascading Style Sheets and that&#8217;s the main reason, many Front End Developers avoid this for as long as possible. Specificity is a weight that is applied to a given CSS\u00a0declaration and is determined by the number of each selector type in the matching selector. Basically, [&hellip;]<\/p>\n","protected":false},"author":905,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[3429],"tags":[4842],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38879"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/905"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=38879"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38879\/revisions"}],"predecessor-version":[{"id":52876,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38879\/revisions\/52876"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=38879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=38879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=38879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}