{"id":11,"date":"2022-09-15T18:26:00","date_gmt":"2022-09-15T18:26:00","guid":{"rendered":"http:\/\/fem.flywheelsites.com\/?p=11"},"modified":"2023-12-17T19:27:17","modified_gmt":"2023-12-17T19:27:17","slug":"css-specificity","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/css-specificity\/","title":{"rendered":"Tackling CSS Specificity"},"content":{"rendered":"\n<p>How often have you been frustrated because you\u2019ve just joined a new project and can\u2019t figure out why your styles aren\u2019t being applied properly? Or maybe you\u2019ve just integrated a new library, and suddenly all of your styles are being overridden. You could slap an&nbsp;<code>!important<\/code>&nbsp;declaration onto your styles and call it a day, but that would throw maintainability out the window. So you\u2019ve finally decided to learn about CSS specificity.<\/p>\n\n\n\n<p>Specificity is one of the foundational concepts of CSS, and understanding it allows you to write maintainable, efficient CSS. So what exactly is specificity?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-css-specificity\">What Is CSS Specificity?<\/h2>\n\n\n\n<p>CSS specificity is the algorithm browsers use to determine which style declarations should be applied to an element. Each selector is assigned a particular weight depending on its specificity and then these weights are measured against each other to determine which style rules win for a particular element. We calculate specificity using three categories: ID, class, and type. Let\u2019s take a deeper look into these weighted selector categories.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"weighted-selectors\">Weighted Selectors<\/h2>\n\n\n\n<p>The&nbsp;<strong>ID<\/strong>&nbsp;category includes ID selectors (i.e.&nbsp;<code>#about<\/code>). For every ID in a selector, add 1-0-0 to the weight.<\/p>\n\n\n\n<p>The&nbsp;<strong>class<\/strong>&nbsp;category includes class selectors (i.e.<code>.about<\/code>), attribute selectors (i.e.&nbsp;<code>[type=\"button\"]<\/code>), and pseudo-classes (i.e.&nbsp;<code>:hover<\/code>,&nbsp;<code>:checked<\/code>). For every class, attribute, and pseudo-class in a selector, add 0-1-0 to the weight.<\/p>\n\n\n\n<p>The&nbsp;<strong>type<\/strong>&nbsp;category includes type selectors (i.e.&nbsp;<code>h3<\/code>,&nbsp;<code>aside<\/code>) and pseudo-elements (i.e.&nbsp;<code>::after<\/code>). For every type and pseudo-element in a selector, add 0-0-1 to the weight.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"678\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/weighted_selectors_1-1024x678.png?resize=1024%2C678\" alt=\"\" class=\"wp-image-99\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/weighted_selectors_1.png?resize=1024%2C678&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/weighted_selectors_1.png?resize=300%2C199&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/weighted_selectors_1.png?resize=768%2C508&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/weighted_selectors_1.png?resize=1536%2C1016&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/weighted_selectors_1.png?w=1632&amp;ssl=1 1632w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"calculating-specificity\">Calculating Specificity<\/h2>\n\n\n\n<p>For each of the categories above, we add one to each respective column when we use a selector of that type. For example, if a selector contains a type followed by an ID, we would add a 1 in the ID column and a 1 in the type column giving us a specificity of 1-0-1.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-comment\">\/* 1-0-1 *\/<\/span>\n<span class=\"hljs-selector-tag\">aside<\/span> <span class=\"hljs-selector-id\">#paragraph<\/span> {\n  ...\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To calculate which selector is more specific, we compare the values found in each column. For example, let\u2019s say we have two selectors, each styling a paragraph element.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.paragraph<\/span> {\n  ...\n}\n\n<span class=\"hljs-selector-class\">.aside<\/span> <span class=\"hljs-selector-tag\">p<\/span> {\n  ...\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The first selector contains only one class, so its value is 0-1-0.<\/p>\n\n\n\n<p>The second selector contains a class selector and a type selector, so its value is 0-1-1.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"534\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_3-1024x534.png?resize=1024%2C534\" alt=\"\" class=\"wp-image-100\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_3.png?resize=1024%2C534&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_3.png?resize=300%2C156&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_3.png?resize=768%2C401&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_3.png?resize=1536%2C801&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_3.png?w=1881&amp;ssl=1 1881w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Now we can compare the two selectors column by column. Starting with the most specific column, the ID selectors column, both selectors have a 0, so we can move on to the next column.<\/p>\n\n\n\n<p>In the classes, pseudo-classes, and attribute selectors column, each selector has a 1. Since these values are equal, we must compare the third column to determine which selector wins. In the final column, type selectors and pseudo-elements, only the second selector has a 1, so it is more specific.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"534\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_4-1024x534.png?resize=1024%2C534\" alt=\"\" class=\"wp-image-101\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_4.png?resize=1024%2C534&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_4.png?resize=300%2C156&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_4.png?resize=768%2C401&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_4.png?resize=1536%2C801&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_4.png?w=1881&amp;ssl=1 1881w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"examples-of-calculating-specificity\">Examples of Calculating Specificity<\/h2>\n\n\n\n<p>Let\u2019s look at a few more examples and see if we\u2019re comfortable before moving on.<\/p>\n\n\n\n<p>In the first example, we have three selectors. These three selectors are all attempting to change the color of a paragraph\u2019s text. The first selector uses two class selectors; the second selector uses a class selector followed by a type selector. The third selector uses two type selectors.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.my-aside<\/span> <span class=\"hljs-selector-class\">.my-paragraph<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: blue;\n}\n\n<span class=\"hljs-selector-class\">.my-aside<\/span> <span class=\"hljs-selector-tag\">p<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: red;\n}\n\n<span class=\"hljs-selector-tag\">aside<\/span> <span class=\"hljs-selector-tag\">p<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: yellow;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Let\u2019s evaluate each of these selectors using the three-column method.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"828\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_3-1024x828.png?resize=1024%2C828\" alt=\"\" class=\"wp-image-103\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_3.png?resize=1024%2C828&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_3.png?resize=300%2C243&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_3.png?resize=768%2C621&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_3.png?resize=1536%2C1242&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_3.png?w=1881&amp;ssl=1 1881w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The first selector&nbsp;<code>.my-aside .my-paragraph<\/code>&nbsp;contains two classes, so it gets a two in the second column.<\/li>\n\n\n\n<li>The second selector&nbsp;<code>.my-aside p<\/code>&nbsp;includes a class and a type selector, so it gets one in the second and third columns.<\/li>\n\n\n\n<li>The last selector&nbsp;<code>aside p<\/code>&nbsp;contains two types, so it gets a two in the third column.<\/li>\n<\/ol>\n\n\n\n<p>Starting from column one, none of the selectors have an ID, so we move on to column two. In column two, we see that the first selector contains two classes while the other selectors contain one and zero, respectively. Thus, selector one is the most specific, and we don\u2019t need to examine column three (assuming all three selectors are styling the same CSS property).<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/static.frontendmasters.com\/assets\/fm\/med\/blog\/2022\/css-specificity\/calc_specificity_ex_3.png?ssl=1\" alt=\"calculating specificity, example 3\"\/><\/figure>\n\n\n\n<p>The first example was pretty clear cut, but what if we have two selectors of equal weight attempting to style the same element?<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-class\">.aside<\/span> <span class=\"hljs-selector-class\">.paragraph<\/span> {\n    <span class=\"hljs-attribute\">color<\/span>: blue;\n  }\n\n  <span class=\"hljs-selector-class\">.my-aside<\/span> <span class=\"hljs-selector-class\">.my-paragraph<\/span> {\n    <span class=\"hljs-attribute\">color<\/span>: red;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">aside<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"aside my-aside\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"paragraph my-paragraph\"<\/span>&gt;<\/span>...<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">aside<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We can see here that both selectors attempt to style the same paragraph element and have a specificity of 0-2-0. So which color will the paragraph text be?<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"536\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_5-1024x536.png?resize=1024%2C536\" alt=\"\" class=\"wp-image-104\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_5.png?resize=1024%2C536&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_5.png?resize=300%2C157&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_5.png?resize=768%2C402&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_5.png?resize=1536%2C804&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/calc_specificity_ex_5.png?w=1881&amp;ssl=1 1881w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>The text will be red because the latest CSS declaration wins.&nbsp;<strong>When two selectors of equal weight attempt to style the same CSS property, the latest, or most recent, declaration wins<\/strong>.<\/p>\n\n\n\n<p>If you want the text to be blue, you\u2019ll have to increase the specificity of the first selector or decrease the specificity of the second selector. You could also change the order of the declarations so the first selector becomes the latest selector.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"unweighted-selectors\">Unweighted Selectors<\/h2>\n\n\n\n<p>In the previous section we took a look at weighted selectors, or selectors that contribute to a selector\u2019s specificity. There are, however, some selectors which are unweighted and do not contribute to a selector\u2019s specificity weight.<\/p>\n\n\n\n<p>These selectors include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The universal selector (*)<\/li>\n\n\n\n<li>The :where() pseudo-class<\/li>\n\n\n\n<li>Combinators (i.e. +, &gt;)<\/li>\n\n\n\n<li>The :is() pseudo-class (the selectors inside the parentheses are weighted)<\/li>\n\n\n\n<li>The :not() pseudo-class (the selectors inside the parentheses are weighted)<\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s take a look at a few examples containing unweighted selectors.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-unweighted-selectors\">Example of Unweighted Selectors<\/h3>\n\n\n\n<p>Here we have three sections that contain paragraphs of text. The second section has a class of&nbsp;<code>highlight<\/code>.<\/p>\n\n\n\n<p>In our CSS, the first selector selects all paragraphs that are direct children of elements containing a class of&nbsp;<code>highlight<\/code>.<\/p>\n\n\n\n<p>The second selector selects all paragraphs that are descendants of section elements that do not contain the class&nbsp;<code>highlight<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-class\">.highlight<\/span> &gt; <span class=\"hljs-selector-tag\">p<\/span> {\n    <span class=\"hljs-attribute\">background<\/span>: yellow;\n  }\n\n  <span class=\"hljs-selector-tag\">section<\/span><span class=\"hljs-selector-pseudo\">:not(.highlight)<\/span> <span class=\"hljs-selector-tag\">p<\/span> {\n    <span class=\"hljs-attribute\">background<\/span>: blue;\n    <span class=\"hljs-attribute\">color<\/span>: white;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus modi \n    quasi in nisi nobis! Accusamus possimus perferendis consectetur libero \n    ut dolorem eveniet officiis aliquam. Accusantium cumque ab ratione\n    exercitationem perspiciatis!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"highlight\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus modi \n    quasi in nisi nobis! Accusamus possimus perferendis consectetur libero \n    ut dolorem eveniet officiis aliquam. Accusantium cumque ab ratione\n    exercitationem perspiciatis!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus modi \n    quasi in nisi nobis! Accusamus possimus perferendis consectetur libero \n    ut dolorem eveniet officiis aliquam. Accusantium cumque ab ratione\n    exercitationem perspiciatis!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The UI for this code looks like this. The first and third paragraphs have a blue background and white text (per selector two) and the middle paragraph, which is contained within a section with a class of highlight, has a background color of yellow per selector one.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"668\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_2.png?resize=750%2C668\" alt=\"\" class=\"wp-image-106\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_2.png?w=750&amp;ssl=1 750w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_2.png?resize=300%2C267&amp;ssl=1 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>If we take a look at the specificities for each selector we see that selector one has a specificity of 0-1-1 and selector two has a specificity of 0-1-2. Notice that the direct child combinator (<code>&gt;<\/code>) and the negation pseudo-class (<code>:not()<\/code>) do not contribute to the weight of the specificity, even though the selector inside of the negation pseudo-class does contribute.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"536\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_3-1024x536.png?resize=1024%2C536\" alt=\"\" class=\"wp-image-107\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_3.png?resize=1024%2C536&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_3.png?resize=300%2C157&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_3.png?resize=768%2C402&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_3.png?resize=1536%2C804&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/unweighted_selectors_ex_3.png?w=1881&amp;ssl=1 1881w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"inline-styles\">Inline Styles<\/h2>\n\n\n\n<p>Inline styles are added to elements via the style attribute (i.e.&nbsp;<code>style=\"color: blue\"<\/code>) and override any author-defined styles. The only styles that override inline styles use the&nbsp;<code>!important<\/code>&nbsp;declaration (which we\u2019ll discuss in the next section).<\/p>\n\n\n\n<p>For code maintainability it\u2019s recommended to encapsulate your styles in external stylesheets (linked with&nbsp;<code>&lt;link rel=\"stylesheet\" href=\"...\" \/&gt;<\/code>) and don\u2019t use inline styles unless absolutely necessary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-inline-styles\">Example of Inline Styles<\/h3>\n\n\n\n<p>In the example below, we\u2019ve declared a style that should turn all elements with a class of&nbsp;<code>paragraph<\/code>&nbsp;blue. However, this style is being overridden on the first paragraph by an inline style declaration which sets the paragraph color to red. The second paragraph text will still be blue.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-class\">.paragraph<\/span> {\n    <span class=\"hljs-attribute\">color<\/span>: blue;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"paragraph\"<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"color: red;\"<\/span>&gt;<\/span>lorem ipsem<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"paragraph\"<\/span>&gt;<\/span>lorem ipsem<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"important\">!important<\/h2>\n\n\n\n<p>Adding <code>!important<\/code> to the end of a style is a way for the code author to give a CSS value more weight than it has by default.<\/p>\n\n\n\n<p>If two conflicting styles contain&nbsp;<code>!important<\/code>&nbsp;declarations, the higher-specificity declaration wins. If both elements have the same weight, the last declaration wins.<\/p>\n\n\n\n<p>Unfortunately, using&nbsp;<code>!important<\/code>&nbsp;has become an \u201ceasy solution\u201d for many developers, leading to specificity chaos and unmaintainable code.&nbsp;<code>!important<\/code>&nbsp;should not be used unless absolutely necessary. The code becomes more difficult to maintain and disrupts the natural flow of style calculations by giving more weight to selectors which don\u2019t necessarily deserve that weight.<\/p>\n\n\n\n<p><code>!important<\/code>&nbsp;is a great tool for testing, aiding accessibility, or temporarily fixing an urgent problem, but it should not be considered a long-term solution.<\/p>\n\n\n\n<p>Let\u2019s take a look at an example of how&nbsp;<code>!important<\/code>&nbsp;impacts style.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-important\">Example of !important<\/h3>\n\n\n\n<p>Here we have an unordered list with three list items, all wrapped in a div. On line two we select all list items that are direct children of an unordered list. On line 6 (<code>div li<\/code>) we select all list items contained within a&nbsp;<code>&lt;div&gt;<\/code>.<\/p>\n\n\n\n<p>Both of these selectors have equal weights (remember the direct descendent combinator, &lt;, is unweighted) of 0-0-2, and are attempting to style the same CSS property. Under normal circumstances the selector&nbsp;<code>div li<\/code>&nbsp;would win and the list item text would be red, however we\u2019ve added an&nbsp;<code>!important<\/code>&nbsp;declaration to the style rule on line 3 (<code>color: blue !important;<\/code>), which overrides the style declared on line 7 (<code>color: red;<\/code>). Thus, the list item text is blue.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-tag\">ul<\/span> &gt; <span class=\"hljs-selector-tag\">li<\/span> {\n    <span class=\"hljs-attribute\">color<\/span>: blue <span class=\"hljs-meta\">!important<\/span>;\n  }\n\n  <span class=\"hljs-selector-tag\">div<\/span> <span class=\"hljs-selector-tag\">li<\/span> {\n    <span class=\"hljs-attribute\">color<\/span>: red;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span>Item 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span>Item 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span>Item 3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"374\" height=\"212\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/inline_styles_ex_3.png?resize=374%2C212\" alt=\"\" class=\"wp-image-108\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/inline_styles_ex_3.png?w=374&amp;ssl=1 374w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/inline_styles_ex_3.png?resize=300%2C170&amp;ssl=1 300w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"specificity-challenges\">Specificity Challenges<\/h2>\n\n\n\n<p>Now that you\u2019ve learned the basics of CSS specificity, it\u2019s time to put it to the test. See if you can determine what the UI will look like based on the following code snippets. Answers are provided at the end of this post.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"specificity-challenge-1\">Specificity Challenge 1<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  * {\n    <span class=\"hljs-attribute\">font-family<\/span>: `Arial`, sans-serif;\n  }\n\n  <span class=\"hljs-selector-tag\">section<\/span> {\n    <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">24px<\/span>;\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">2px<\/span> dotted black;\n    <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">16px<\/span>;\n  }\n\n  <span class=\"hljs-selector-tag\">button<\/span> {\n    <span class=\"hljs-attribute\">background<\/span>: skyblue;\n    <span class=\"hljs-attribute\">border<\/span>: none;\n    <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">12px<\/span>;\n  }\n\n  <span class=\"hljs-selector-tag\">p<\/span> {\n    <span class=\"hljs-attribute\">font-family<\/span>: <span class=\"hljs-string\">'Tahoma'<\/span>;\n  }\n  \n  <span class=\"hljs-selector-class\">.content<\/span> {\n    <span class=\"hljs-attribute\">font-family<\/span>: <span class=\"hljs-string\">'Georgia'<\/span>;\n  }\n  \n  <span class=\"hljs-selector-id\">#section-1-read-more<\/span> {\n    <span class=\"hljs-attribute\">background<\/span>: orange;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"title\"<\/span>&gt;<\/span>Section 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"content\"<\/span>&gt;<\/span>Lorem ipsum dolor sit amet consectetur adipisicing elit. \n    Velit maiores...<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"content\"<\/span>&gt;<\/span>Lorem ipsum dolor sit amet consectetur adipisicing elit. \n    Velit maiores...<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"section-1-read-more\"<\/span>&gt;<\/span>Read more<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"title\"<\/span>&gt;<\/span>Section 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"content\"<\/span>&gt;<\/span>Lorem ipsum dolor sit amet consectetur adipisicing elit. \n    Velit maiores...<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"content\"<\/span>&gt;<\/span>Lorem ipsum dolor sit amet consectetur adipisicing elit. \n    Velit maiores...<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"section-2-read-more\"<\/span>&gt;<\/span>Read more<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"specificity-challenge-1-solution\">Specificity Challenge 1 Solution<\/h3>\n\n\n\n<p>There will be two sections. Each section will have a bottom margin of 24px, a 2px dotted border and 16px padding on all sides.<\/p>\n\n\n\n<p>Each section header will have a font-family of Arial or another sans-serif font as declared by the universal selector on line 1.<\/p>\n\n\n\n<p>Each paragraph will have a font family of Georgia because of the content class selector on line 22&nbsp;<code>.content<\/code>&nbsp;(it overrides the type selector on line 18&nbsp;<code>p<\/code>).<\/p>\n\n\n\n<p>Each button will have a padding of 12px on all sides and will not have a border. Only button two will have a background color of&nbsp;<code>skyblue<\/code>&nbsp;(defined by the type selector on line 12&nbsp;<code>button<\/code>). The first button will have a background color of orange as defined by the ID selector on line 27&nbsp;<code>#section-1-read-more<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  * { <span class=\"hljs-comment\">\/* 0-0-0 *\/<\/span>\n    <span class=\"hljs-attribute\">font-family<\/span>: <span class=\"hljs-string\">'Arial'<\/span>, sans-serif;\n  }\n\n  <span class=\"hljs-selector-tag\">section<\/span> { <span class=\"hljs-comment\">\/* 0-0-1 *\/<\/span> \n    <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">24px<\/span>; \n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">2px<\/span> dotted black; \n    <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">16px<\/span>;\n  }\n\n  <span class=\"hljs-selector-tag\">button<\/span> { <span class=\"hljs-comment\">\/* 0-0-1 *\/<\/span> \n    <span class=\"hljs-attribute\">background<\/span>: skyblue; \n    <span class=\"hljs-attribute\">border<\/span>: none; \n    <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">12px<\/span>;\n  }\n  \n  <span class=\"hljs-selector-tag\">p<\/span> { <span class=\"hljs-comment\">\/* 0-0-1 *\/<\/span>\n    <span class=\"hljs-attribute\">font-family<\/span>: <span class=\"hljs-string\">'Tahoma'<\/span>;\n  }\n  \n  <span class=\"hljs-selector-class\">.content<\/span> { <span class=\"hljs-comment\">\/* 0-1-0 *\/<\/span>\n    <span class=\"hljs-attribute\">font-family<\/span>: <span class=\"hljs-string\">'Georgia'<\/span>;\n  }\n  \n  <span class=\"hljs-selector-id\">#section-1-read-more<\/span> { <span class=\"hljs-comment\">\/* 1-0-0 *\/<\/span>\n    <span class=\"hljs-attribute\">background<\/span>: orange;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n... \n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"372\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_1c-1024x372.png?resize=1024%2C372\" alt=\"\" class=\"wp-image-109\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_1c.png?resize=1024%2C372&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_1c.png?resize=300%2C109&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_1c.png?resize=768%2C279&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_1c.png?resize=1536%2C559&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_1c.png?w=1999&amp;ssl=1 1999w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"specificity-challenge-2\">Specificity Challenge 2<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-tag\">li<\/span> &gt; <span class=\"hljs-selector-tag\">a<\/span> {\n    <span class=\"hljs-attribute\">color<\/span>: pink;\n    <span class=\"hljs-attribute\">text-decoration<\/span>: none;\n  }\n  \n  <span class=\"hljs-selector-tag\">a<\/span><span class=\"hljs-selector-class\">.link<\/span> {\n    <span class=\"hljs-attribute\">color<\/span>: orange;\n  }\n\n  <span class=\"hljs-selector-tag\">a<\/span><span class=\"hljs-selector-attr\">&#91;visited=<span class=\"hljs-string\">\"true\"<\/span>]<\/span> {\n    <span class=\"hljs-attribute\">text-decoration<\/span>: underline;\n  }\n  \n  <span class=\"hljs-selector-tag\">li<\/span><span class=\"hljs-selector-pseudo\">:first-of-type<\/span> <span class=\"hljs-selector-tag\">a<\/span> {\n    <span class=\"hljs-attribute\">color<\/span>: blue;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"link\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Item 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"link\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span> <span class=\"hljs-attr\">visited<\/span>=<span class=\"hljs-string\">\"true\"<\/span>&gt;<\/span>Item 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"link\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Item 3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"specificity-challenge-2-solution\">Specificity Challenge 2 Solution<\/h3>\n\n\n\n<p>There will be an unordered list with three items: item 1, item 2, and item 3. Item 1 will be blue because of the&nbsp;<code>:first-of-type<\/code>&nbsp;pseudo-class selector on line 15. Items 2 and 3 will be orange. Item 2 will be underlined.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-tag\">li<\/span> &gt; <span class=\"hljs-selector-tag\">a<\/span> { <span class=\"hljs-comment\">\/* 0-0-2 *\/<\/span>\n    <span class=\"hljs-attribute\">color<\/span>: pink;\n    <span class=\"hljs-attribute\">text-decoration<\/span>: none;\n  }\n  \n  <span class=\"hljs-selector-tag\">a<\/span><span class=\"hljs-selector-class\">.link<\/span> { <span class=\"hljs-comment\">\/* 0-1-1 *\/<\/span> \n    <span class=\"hljs-attribute\">color<\/span>: orange;\n  }\n  \n  <span class=\"hljs-selector-tag\">a<\/span><span class=\"hljs-selector-attr\">&#91;visited=<span class=\"hljs-string\">\"true\"<\/span>]<\/span> { <span class=\"hljs-comment\">\/* 0-1-1 *\/<\/span>\n    <span class=\"hljs-attribute\">text-decoration<\/span>: underline;\n  }\n  \n  <span class=\"hljs-selector-tag\">li<\/span><span class=\"hljs-selector-pseudo\">:first-of-type<\/span> <span class=\"hljs-selector-tag\">a<\/span> { <span class=\"hljs-comment\">\/* 0-1-2 *\/<\/span>\n    <span class=\"hljs-attribute\">color<\/span>: blue;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"228\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_2c.png?resize=496%2C228\" alt=\"\" class=\"wp-image-110\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_2c.png?w=496&amp;ssl=1 496w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_2c.png?resize=300%2C138&amp;ssl=1 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"specificity-challenge-3\">Specificity Challenge 3<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-tag\">input<\/span><span class=\"hljs-selector-attr\">&#91;type=<span class=\"hljs-string\">\"text\"<\/span>]<\/span> {\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">2px<\/span> solid red;\n  }\n  \n  <span class=\"hljs-selector-tag\">input<\/span> {\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">4px<\/span> solid purple;\n  }\n  \n  <span class=\"hljs-selector-tag\">input<\/span><span class=\"hljs-selector-id\">#first-name<\/span> {\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">6px<\/span> solid orange;\n  }\n  \n  <span class=\"hljs-selector-id\">#first-name<\/span> {\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">8px<\/span> solid black;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\">\"first-name\"<\/span>&gt;<\/span>First name<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"first-name\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"specificity-challenge-3-solution\">Specificity Challenge 3 Solution<\/h3>\n\n\n\n<p>There will be a label (First name) and a text input. The text input will have a&nbsp;<code>6px solid orange<\/code>&nbsp;border due to the type\/ID selector on line 10&nbsp;<code>input#first-name<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-tag\">input<\/span><span class=\"hljs-selector-attr\">&#91;type=<span class=\"hljs-string\">\"text\"<\/span>]<\/span> { <span class=\"hljs-comment\">\/* 0-1-1 *\/<\/span>\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">2px<\/span> solid red;\n  }\n  \n  <span class=\"hljs-selector-tag\">input<\/span> { <span class=\"hljs-comment\">\/* 0-0-1 *\/<\/span>\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">4px<\/span> solid purple;\n  }\n  \n  <span class=\"hljs-selector-tag\">input<\/span><span class=\"hljs-selector-id\">#first-name<\/span> { <span class=\"hljs-comment\">\/* 1-0-1 *\/<\/span>\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">6px<\/span> solid orange;\n  }\n  \n  <span class=\"hljs-selector-id\">#first-name<\/span> { <span class=\"hljs-comment\">\/* 1-0-0 *\/<\/span>\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">8px<\/span> solid black;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\">\"first-name\"<\/span>&gt;<\/span>First name<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"first-name\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"538\" height=\"102\" src=\"https:\/\/i0.wp.com\/rc.frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_3c.png?resize=538%2C102\" alt=\"\" class=\"wp-image-111\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_3c.png?w=538&amp;ssl=1 538w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/challenge_3c.png?resize=300%2C57&amp;ssl=1 300w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"you-learned-specificity\">You Learned Specificity!<\/h2>\n\n\n\n<p>CSS specificity is tricky, but once you get the gist, it will become second nature. As one of the foundational elements of CSS, having a solid grasp of specificity will allow you to work confidently with legacy code bases and third-party libraries.<\/p>\n\n\n\n<p>If you want to go further with CSS specificity, here are a few resources for you to check out!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"more-resources-on-css-specificity\">More Resources on CSS Specificity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.matuzo.at\/blog\/2022\/specificity\/\">CSS Specificity Demo<\/a>: An interactive demo that illustrates how CSS specificity impacts style.<\/li>\n\n\n\n<li><a href=\"https:\/\/specificity.keegan.st\/\">Specificity Calculator<\/a>: Input your selectors to see the specificities.<\/li>\n\n\n\n<li><a href=\"https:\/\/stuffandnonsense.co.uk\/archives\/css_specificity_wars.html\">CSS Specificity Wars<\/a>: Learn CSS specificity through Star Wars.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>How often have you been frustrated because you\u2019ve just joined a new project and can\u2019t figure out why your styles aren\u2019t being applied properly? Or maybe you\u2019ve just integrated a new library, and suddenly all of your styles are being overridden. You could slap an&nbsp;!important&nbsp;declaration onto your styles and call it a day, but that [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":44,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[7,9,8],"class_list":["post-11","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-css","tag-selectors","tag-specificity"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/10\/specificity-featured.png?fit=1000%2C500&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":5,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":277,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions\/277"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/44"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}