<?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: Obika Onyinyechi Viola </title>
    <description>The latest articles on DEV Community by Obika Onyinyechi Viola  (@obikaviola).</description>
    <link>https://dev.to/obikaviola</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%2F1025590%2F100abbb3-a956-41d3-bdcf-66560648f802.jpeg</url>
      <title>DEV Community: Obika Onyinyechi Viola </title>
      <link>https://dev.to/obikaviola</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/obikaviola"/>
    <language>en</language>
    <item>
      <title>Understanding CSS Cascade, Specificity, and Selectors</title>
      <dc:creator>Obika Onyinyechi Viola </dc:creator>
      <pubDate>Mon, 06 May 2024 16:48:10 +0000</pubDate>
      <link>https://dev.to/obikaviola/understanding-css-cascade-specificity-and-selectors-2dm6</link>
      <guid>https://dev.to/obikaviola/understanding-css-cascade-specificity-and-selectors-2dm6</guid>
      <description>&lt;p&gt;Cascading Style Sheets (CSS) plays a vital role when it comes to improving the visual aspect of a website. To have an understanding of CSS, one needs to grasp the following concepts: Cascade, Selector and specificity.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Cascade
&lt;/h2&gt;

&lt;p&gt;CSS cascade has to do with determining the final styles that will be applied to an element. This style can be affected by:&lt;br&gt;
​The styles applied by the author to the document.&lt;/p&gt;

&lt;p&gt;​The default style applied by the browser. The styles applied by the users themselves.&lt;/p&gt;

&lt;p&gt;Understanding CSS cascade will help a web developer know about what style will affect which and it is a first step to understanding specificity and selector.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Selectors
&lt;/h2&gt;

&lt;p&gt;CSS selector are used to select elements you want to style. CSS has are different means of selecting specific element such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Element selectors
Target all instances of a particular HTML 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;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16px&lt;/span&gt;&lt;span class="p"&gt;;&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;Class selectors: Target elements with a specific class attribute.
&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="nc"&gt;.blueText&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="p"&gt;;&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;ID selectors:Target a single element with a specific ID attribute.
&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;#uniqueID&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&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;Attribute selectors: Targets elements with specific attributes.
&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;input&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;"text"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#ccc&lt;/span&gt;&lt;span class="p"&gt;;&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;Pseudo-classes and pseudo-elements: Target elements based on their state or position.
&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;a&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;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;underline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="nd"&gt;::first-letter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;150%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Specificity
&lt;/h2&gt;

&lt;p&gt;CSS specificity has to deal to what particular style is applied and if there are more than one style targeting a particular element, what style takes precedence. Specificity on CSS has its hierarchy which is used to calculate which style should be applied. &lt;/p&gt;

&lt;p&gt;These hierarchy are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inline styles
&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="c"&gt;/* Inline style */&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="nt"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"color: red;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;This&lt;/span&gt; &lt;span class="nt"&gt;text&lt;/span&gt; &lt;span class="nt"&gt;is&lt;/span&gt; &lt;span class="nt"&gt;red&lt;/span&gt;&lt;span class="o"&gt;.&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;ID selector styles
&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="c"&gt;/* ID selector */&lt;/span&gt;
&lt;span class="nf"&gt;#uniqueID&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="p"&gt;;&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;Classes and pseudo-classes selector styles
&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="c"&gt;/* Class selector */&lt;/span&gt;
&lt;span class="nc"&gt;.blueText&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="p"&gt;;&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;Element selector
&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="c"&gt;/* Element selector */&lt;/span&gt;
&lt;span class="nt"&gt;p&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this means is that if the inline styles is present, it will be applied. If it is not, the ID selector styles will be applied and if it is not present then the browser will look out if there is a class selector style is applied. And on and on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding cascade, selector and specificity as a web developer is the key to proficient in the world of CSS. It helps make your code clean, resolve conflict in styles and makes your code maintainable and understandable. To master these concepts is to making your CSS life less a struggle.&lt;/p&gt;

&lt;p&gt;Enjoyed what you read and would like to connect? How about we take the conversation on social. You can connect with me on &lt;a href="https://twitter.com/obika_viola" rel="noopener noreferrer"&gt;X(formerly Twitter&lt;/a&gt;), &lt;a href="https://www.linkedin.com/in/onyinyechi-obika" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://web.facebook.com/viola.obika.1/" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Likes and comments are highly appreciated 😊&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
