<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Debjit Sinha</title>
    <description>The latest articles on DEV Community by Debjit Sinha (@debjits1).</description>
    <link>https://dev.to/debjits1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F610446%2F9e045d55-5ed0-46ec-9f79-72b1d63338b8.png</url>
      <title>DEV Community: Debjit Sinha</title>
      <link>https://dev.to/debjits1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/debjits1"/>
    <language>en</language>
    <item>
      <title>Stop using '!important' in css - css specificity explained in shortest possible way</title>
      <dc:creator>Debjit Sinha</dc:creator>
      <pubDate>Sat, 26 Jun 2021 11:38:20 +0000</pubDate>
      <link>https://dev.to/debjits1/stop-using-important-in-css-css-specificity-explained-in-shortest-possible-way-b31</link>
      <guid>https://dev.to/debjits1/stop-using-important-in-css-css-specificity-explained-in-shortest-possible-way-b31</guid>
      <description>&lt;p&gt;Most of the time I have seen developers using &lt;code&gt;!important&lt;/code&gt; in css without any necessity. Using &lt;code&gt;!important&lt;/code&gt; just for a simple override is &lt;em&gt;always a bad practice&lt;/em&gt;. This is because it makes debugging more difficult by breaking the natural cascading in your stylesheets.&lt;/p&gt;

&lt;p&gt;You might be asking, "then how do I avoid it"?&lt;br&gt;
Answer is, you can do it using greater "specificity". &lt;/p&gt;

&lt;p&gt;For the next few minutes I am going to explain CSS specificity in the shortest way possible.&lt;/p&gt;

&lt;p&gt;CSS specificity is the value provided to each selector so that the browser can understand how much priority to be given to a specific selector.&lt;br&gt;
Below are the selector types in decreasing priority order. The top one having the highest priority and the bottom one has the lowest.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;!important&lt;/code&gt; - this one is given the highest priority, overriding everything else, including inline styles (&lt;code&gt;style&lt;/code&gt; attribute). If there are multiple CSS selectors with &lt;code&gt;!important&lt;/code&gt; mentioned for same element, the last declaration found in the CSS is applied to the element.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Inline styles&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"color: red"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello reader&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ID Selector&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nf"&gt;#headline&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Class, pseudo class and attribute selector&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;selector&lt;/span&gt;
&lt;span class="nc"&gt;.headline&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;pseudo&lt;/span&gt; &lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;selector&lt;/span&gt;
&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;attribute&lt;/span&gt; &lt;span class="nt"&gt;selector&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"input"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Type Selectors and pseudo elements&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;type&lt;/span&gt; &lt;span class="nt"&gt;selector&lt;/span&gt;
&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;pseudo&lt;/span&gt; &lt;span class="nt"&gt;element&lt;/span&gt; &lt;span class="nt"&gt;selector&lt;/span&gt;
&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="nd"&gt;:before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other than the above type of selectors there are Universal selectors &lt;code&gt;*&lt;/code&gt;, combinators &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;~&lt;/code&gt;, &lt;code&gt;' '&lt;/code&gt;, &lt;code&gt;||&lt;/code&gt; and negation pseudo-class &lt;code&gt;:not()&lt;/code&gt; which does not have any specificity value.&lt;/p&gt;

&lt;p&gt;Now we need to know how the browser calculates the specificity value and what happens when they are combined. The total value for a given combination of selectors is calculated by the priority of the selectors present and the number of occurrence of that selector. You can visualize it from the below image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftjvgr47n7r9801b5z4bw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftjvgr47n7r9801b5z4bw.jpg" alt="CSS Specificity values map"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0qvvrctrqgk1ghsbx0k7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0qvvrctrqgk1ghsbx0k7.jpg" alt="CSS Specificity values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0oy983cxd015spa2au41.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0oy983cxd015spa2au41.jpg" alt="CSS Specificity values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feml1shnfu6k85ct0d8j4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feml1shnfu6k85ct0d8j4.jpg" alt="CSS Specificity values"&gt;&lt;/a&gt;&lt;br&gt;
Here we have 2 elements &lt;code&gt;body&lt;/code&gt; and &lt;code&gt;div&lt;/code&gt; and 2 classes &lt;code&gt;parentClass&lt;/code&gt; and &lt;code&gt;ChildClass&lt;/code&gt;. That is why we get 2 in each selectors position, because number of occurrences are added along with priority.&lt;/p&gt;

&lt;p&gt;You can check &lt;a href="https://specifishity.com/" rel="noopener noreferrer"&gt;this&lt;/a&gt; for more examples.&lt;br&gt;
For more details, you can refer &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity" rel="noopener noreferrer"&gt;this&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have reached here, I hope you will be able to avoid &lt;code&gt;!important&lt;/code&gt; and use higher specificity instead. &lt;br&gt;
Thank you, happy styling!&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>cssspecificity</category>
      <category>stylesheet</category>
    </item>
  </channel>
</rss>
