<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Priyobroto Kar's blog]]></title><description><![CDATA[Priyobroto Kar's blog]]></description><link>https://blog.priyobroto.xyz</link><generator>RSS for Node</generator><lastBuildDate>Sun, 03 May 2026 02:24:28 GMT</lastBuildDate><atom:link href="https://blog.priyobroto.xyz/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[CSS Specificity Algorithm: Why your styles do not always apply?]]></title><description><![CDATA[As Frontend Developers, we have faced the issue in some projects or maybe in every project where we want to apply a style to an element that NEVER applies. No matter how many times we inspect the element or tweak the CSS, the styles refuse to take ef...]]></description><link>https://blog.priyobroto.xyz/css-specificity-algorithm-why-your-styles-do-not-always-apply</link><guid isPermaLink="true">https://blog.priyobroto.xyz/css-specificity-algorithm-why-your-styles-do-not-always-apply</guid><category><![CDATA[CSS]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Priyobroto Kar]]></dc:creator><pubDate>Fri, 28 Feb 2025 07:47:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1738639600276/f2264aac-9578-4053-888a-252a61d79db7.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As Frontend Developers, we have faced the issue in some projects or maybe in every project where we want to apply a style to an element that NEVER applies. No matter how many times we inspect the element or tweak the CSS, the styles refuse to take effect.</p>
<p>Most of the time, the reason behind this frustrating issue is the <strong>Specificity Algorithm of CSS</strong>, where a more specific selector containing the same styles will take precedence and overrule what you are attempting to apply.</p>
<h3 id="heading-a-simple-example">A Simple Example</h3>
<p>Suppose you are working with the following HTML and CSS:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"button"</span>&gt;</span> Learn CSS Specificity <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.button</span> {
    <span class="hljs-attribute">background-color</span>: red;
}

<span class="hljs-selector-tag">button</span> {
    <span class="hljs-attribute">background-color</span>: blue;
}
</code></pre>
<p>There are two competing styles applied to the same element. One with a <code>background-color: red;</code> and another one with <code>background-color: blue;</code>. The question is which one of them gets applied?</p>
<p>At first glance, you might assume that the button's background will be <strong>blue</strong> maybe because you thought that since CSS applies styles in a top-down approach and as <code>background-color: blue;</code> appears later, it will override the previous one <code>background-color: red;</code>.</p>
<p>Well, let’s see how it looks in the browser.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/PriyoBROTO04/pen/yyBmOBw">https://codepen.io/PriyoBROTO04/pen/yyBmOBw</a></div>
<p> </p>
<p>Ah, the actual result is <strong>red.</strong></p>
<h3 id="heading-why-is-this-happening">Why is this happening?</h3>
<p>This behaviour may seem confusing at first, but it all comes down to CSS specificity. In this blog, we will be demystifying why this strange behaviour occurs by breaking down how the <strong>Specificity Algorithm of CSS works</strong> and why certain styles take precedence over others.</p>
<h2 id="heading-what-is-specificity">What is specificity?</h2>
<p>According to MDN, <strong>Specificity</strong> is the algorithm used by browsers to determine the CSS declaration that is the most relevant to an element, which in turn, determines the property value to apply to the element.</p>
<p>In the above example, since the class selector has more weight than the tag selector due to specificity, the property declared in the class selector got higher preference by the browser.</p>
<p>Now, if I comment out the styles applied using the class selector, you will see that the styles in the tag selector will be applied.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/PriyoBROTO04/pen/pvzmRGV">https://codepen.io/PriyoBROTO04/pen/pvzmRGV</a></div>
<p> </p>
<p>So whenever there will be clashing styles in one element, <strong>always the styles of the selector with a higher specificity score will be applied to that element.</strong></p>
<h2 id="heading-how-specificity-score-is-calculated">How Specificity score is calculated?</h2>
<p>The specificity score value is a four-column value of four different selector weights. It’s written like INLINE-ID-CLASS-TYPE, each representing four different types of CSS Selectors, with the default scores of 0-0-0-0. As you add more selector components of a specific category, the score of that column increases by 1 each time.</p>
<p>Once the specificity scores have been calculated, they are <strong>compared column by column from left to right</strong> and the selector with a <strong>greater value in the first differing column</strong> take precedence!</p>
<h3 id="heading-lets-understand-this-using-some-examples">Let’s understand this using some examples.</h3>
<p>Suppose the HTML file looks like this:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"btn"</span>&gt;</span> Learn CSS Specificity <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<div data-node-type="callout">
<div data-node-type="callout-emoji">❗</div>
<div data-node-type="callout-text"><em>This HTML file will be the same for all the examples below.</em></div>
</div>

<ol>
<li><p><strong>TYPE Column</strong></p>
<p> Whenever you add an <strong>element selector</strong> like <code>h1</code>, <code>div</code>, <code>p</code>, etc or <strong>pseudo-elements</strong> like <code>::before</code>, <code>::after</code>, <code>::placeholder</code>, etc, the score of the TYPE column increases by one (0-0-0-1).</p>
<pre><code class="lang-css"> <span class="hljs-selector-tag">button</span> {                    <span class="hljs-comment">/* 0-0-0-1 */</span>
     <span class="hljs-attribute">background-color</span>: blue;
 }

 <span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">button</span> {                <span class="hljs-comment">/* 0-0-0-2 (WINS!) */</span>
     <span class="hljs-attribute">background-color</span>: yellow;
 }
</code></pre>
</li>
<li><p><strong>CLASS Column</strong></p>
<p> Whenever you add a <strong>class selector</strong> like <code>.btn</code> or <strong>attribute selectors</strong> like <code>[type="email"]</code> or <strong>pseudo-classes</strong> like <code>:hover</code>, <code>:active</code>, the score of the CLASS column increases by one (0-0-1-0).</p>
<pre><code class="lang-css"> <span class="hljs-selector-tag">button</span><span class="hljs-selector-class">.btn</span> {                    <span class="hljs-comment">/* 0-0-1-1 */</span>
     <span class="hljs-attribute">background-color</span>: blue;
 }

 <span class="hljs-selector-tag">button</span><span class="hljs-selector-class">.btn</span><span class="hljs-selector-pseudo">:hover</span> {                <span class="hljs-comment">/* 0-0-2-1 (WINS!) */</span>
     <span class="hljs-attribute">background-color</span>: yellow;
 }
</code></pre>
<p> <strong>Do you notice something here?</strong> Before understanding specificity, I used to think pseudo-classes like <code>:hover</code> had some built-in priority that automatically overrode styles. But now, it's clear that when <code>:hover</code> is applied, the browser dynamically applies the <code>:hover</code> pseudo-class when the user hovers over the element and its specificity score increases, allowing it to override previous styles.</p>
</li>
<li><p><strong>ID Column</strong></p>
<p> Whenever you add an <strong>ID selector</strong> like <code>#submitBtn</code>, the score of the ID column increases by one (0-1-0-0).</p>
<pre><code class="lang-css"> <span class="hljs-selector-id">#container</span> <span class="hljs-selector-tag">button</span><span class="hljs-selector-class">.btn</span> {                    <span class="hljs-comment">/* 0-1-1-1 */</span>
     <span class="hljs-attribute">background-color</span>: blue;
 }

 <span class="hljs-selector-id">#container</span> <span class="hljs-selector-tag">button</span><span class="hljs-selector-class">.btn</span><span class="hljs-selector-id">#submitBtn</span> {                <span class="hljs-comment">/* 0-2-1-1 (WINS!) */</span>
     <span class="hljs-attribute">background-color</span>: yellow;
 }
</code></pre>
</li>
<li><p><strong>INLINE Column</strong></p>
<p> Whenever you add some inline style like <code>style=”background-color: black”</code>, the score the of Inline column increases by one (1-0-0-0) and <strong>overrides all the styles</strong> which was applied in the stylesheet.</p>
<pre><code class="lang-xml"> <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"btn"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"background-color: black;"</span>&gt;</span> Learn CSS Specificity <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
</li>
</ol>
<h3 id="heading-selectors-with-matching-specificity">Selectors with matching specificity</h3>
<p>In all the above cases, we have discussed how the specificity algorithm works for different specificity score, but what if there are two or more selectors with the same specificity score. For these cases, it follows the basic <strong>cascade order rule</strong> of CSS, where the <strong>styles declared later will override earlier ones</strong> if their specificity is equal.</p>
<p>This rule applies not only within the same stylesheet, but also <strong>across multiple stylesheets</strong>. If two stylesheets define rules with the same specificity, the styles from the <strong>stylesheet loaded later</strong> in the HTML <code>&lt;head&gt;</code> will take precedence. This is because browsers process stylesheets in the order they are linked.</p>
<h2 id="heading-the-important-keyword">The !important keyword</h2>
<p>By now, we are quite familiar with the Specificity Algorithm of CSS and how it works. But one can <strong>bypass this Specificity system</strong> and <strong>force a style to take precedence over all other selectors</strong>—including more specific selectors and even inline styles, by using a special keyword <code>!important</code> beside a style declaration.</p>
<p>Since this keyword comes with so much power, as it alters the default Specificity Algorithm of CSS, and <strong><em>with great power there comes great responsibility</em></strong> and hence it should be used very carefully and sparingly, as it can make debugging difficult. Whenever you are using the <code>!important</code> keyword, make sure to add comment in that line explaining why it is necessary to ensure maintainability and clarity.</p>
<h3 id="heading-challenge">Challenge</h3>
<p>What will be the <code>background-color</code> of the <code>&lt;button&gt;</code> when <strong>hovered over</strong>?</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"btn"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"color:white; background-color:black"</span>&gt;</span>Button<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> <span class="hljs-selector-id">#btn</span> {
  <span class="hljs-attribute">background-color</span>: blue;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-class">.btn</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">background-color</span>: red;
}
</code></pre>
<details><summary>Solution</summary><div data-type="detailsContent">The <code>background-color</code> of the <code>&lt;button&gt;</code> when <strong>hovered over </strong>will stay <strong>black</strong>. Let’s break it down: The inline style has a score of 1000, the selector <code>.container #btn</code> has a score of 0110 and the <code>.container .btn:hover</code> selector has a score of 0030 and hence the <strong>inline style wins </strong>and the background color of the button stays <strong>black</strong>.</div></details>

<h3 id="heading-summary">Summary</h3>
<p>I hope you now have a clear understanding of the <strong>CSS Specificity Algorithm</strong>. Learning this will definitely help you debug styles that do not apply as intended and will also enable you to write more maintainable and robust styles.</p>
]]></content:encoded></item><item><title><![CDATA[From Browsers to Servers: The Journey of your Data]]></title><description><![CDATA[We’ve all played online multiplayer games at some point in our lives. Imagine this: two friends, Ranjit and Rahul, are playing Valorant, an online tactical FPS game similar to CS:GO. Ranjit shoots Rahul, landing a perfect headshot, and Rahul’s in-gam...]]></description><link>https://blog.priyobroto.xyz/from-browsers-to-servers-the-journey-of-your-data</link><guid isPermaLink="true">https://blog.priyobroto.xyz/from-browsers-to-servers-the-journey-of-your-data</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[internet]]></category><category><![CDATA[software development]]></category><category><![CDATA[networking]]></category><dc:creator><![CDATA[Priyobroto Kar]]></dc:creator><pubDate>Wed, 22 Jan 2025 21:29:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737581240747/38467760-a1a8-46fb-af17-9b32613cf1d3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We’ve all played online multiplayer games at some point in our lives. Imagine this: two friends, Ranjit and Rahul, are playing <em>Valorant</em>, an online tactical FPS game similar to <em>CS:GO</em>. Ranjit shoots Rahul, landing a perfect headshot, and Rahul’s in-game character dies instantly. Have you ever wondered what happens behind the scenes? How does a mouse click on Ranjit’s computer in one part of the world translate into an immediate response on Rahul’s computer, located far away?</p>
<p>If you’re thinking it’s all thanks to the internet, you’re absolutely right! But how does the information actually travel between the two computers? If no then don’t worry we will be discussing this in this blog.</p>
<h3 id="heading-a-brief-history-of-the-internet"><strong>A Brief History of the Internet</strong></h3>
<p>Before we get into the nitty-gritty of how data travels, let’s take a moment to appreciate the history of the internet — the backbone of modern communication and gaming.</p>
<p>The internet as we know it began in the late 1960s with ARPANET, a project funded by the U.S. Department of Defense. ARPANET’s primary goal was to enable computers at different universities and research institutions to communicate with each other. In 1983, the adoption of the TCP/IP protocol laid the foundation for today’s internet, standardizing how data is sent and received. Over the years, advancements in technology and infrastructure transformed the internet into a global network connecting billions of devices, revolutionizing the way we share information, work, and play.</p>
<h3 id="heading-why-the-internet-matters"><strong>Why the Internet Matters</strong></h3>
<p>The internet has become an integral part of our lives, enabling us to:</p>
<ul>
<li><p>Share data across the globe in seconds.</p>
</li>
<li><p>Access a vast wealth of knowledge and resources.</p>
</li>
<li><p>Stay entertained through gaming, streaming, and social media platforms.</p>
</li>
<li><p>Connect with people, no matter where they are.</p>
</li>
</ul>
<p>It’s no exaggeration to say that the internet powers much of our modern world, including the online multiplayer games we love.</p>
<h3 id="heading-from-browsers-to-servers-the-journey-of-your-data"><strong>From Browsers to Servers: The Journey of Your Data</strong></h3>
<p>Now comes the most exciting part: how the information about Ranjit’s headshot reaches Rahul’s computer.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737211288010/bfdd02f1-307f-4dd4-b60e-4e700621e352.png" alt class="image--center mx-auto" /></p>
<h4 id="heading-step-1-breaking-down-the-information"><strong>Step 1: Breaking Down the Information</strong></h4>
<p>When Ranjit clicks his mouse to shoot, his computer breaks down this action into smaller chunks of data called <em>packets</em>. Each packet contains part of the information needed to process the shot, such as the coordinates of the bullet and the outcome (a headshot).</p>
<h4 id="heading-step-2-the-first-hops"><strong>Step 2: The First Hops</strong></h4>
<p>The packets first travel from Ranjit’s computer to his router via a LAN cable or Wi-Fi. From the router, they move to the modem and leave Ranjit’s home network (known as a Local Area Network or LAN) to enter the wider internet (Wide Area Network or WAN).</p>
<h4 id="heading-step-3-traveling-through-the-internet"><strong>Step 3: Traveling Through the Internet</strong></h4>
<p>Once on the internet, the packets take the most efficient route to reach the <em>Valorant</em> server. Along the way, they pass through multiple routers managed by Ranjit’s Internet Service Provider (ISP). A critical part of this journey involves the <strong>DNS lookup process</strong>, where the domain name (e.g., <em>valorant.com</em>) is translated into an IP address, helping the packets find the correct destination.</p>
<h4 id="heading-step-4-reaching-the-valorant-server"><strong>Step 4: Reaching the Valorant Server</strong></h4>
<p>When the packets reach the <em>Valorant</em> server, the server processes the information and confirms the headshot. At this point, only Ranjit’s computer and the server know what happened. But for Rahul to experience the event, the server must relay this information to his computer.</p>
<h4 id="heading-step-5-the-reverse-journey"><strong>Step 5: The Reverse Journey</strong></h4>
<p>The server sends the data about Ranjit’s shot (and Rahul’s unfortunate demise) back through the internet, following the same process in reverse. The packets travel through Rahul’s ISP and routers before finally reaching his computer.</p>
<h4 id="heading-step-6-reassembly-and-display"><strong>Step 6: Reassembly and Display</strong></h4>
<p>Once the packets arrive at Rahul’s computer, they are reassembled into a complete message. The game processes the data, and Rahul’s screen displays the result: his character is dead, and Ranjit’s headshot is confirmed.</p>
<h3 id="heading-the-role-of-efficiency-and-latency"><strong>The Role of Efficiency and Latency</strong></h3>
<p>In fast-paced games like <em>Valorant</em>, latency (the time it takes for data to travel between computers) is critical. Game developers optimize servers and networks to ensure minimal lag so that actions feel instantaneous. Efficient packet routing and high-speed connections are key to delivering smooth gameplay.</p>
<h3 id="heading-conclusion"><strong>Conclusion</strong></h3>
<p>From Ranjit’s mouse click to Rahul’s reaction, the internet orchestrates a complex yet seamless process that makes multiplayer gaming possible. Understanding the journey of data not only deepens our appreciation for the technology but also highlights the marvel of human innovation that allows us to connect and compete with friends across the globe.</p>
<p>Next time you play an online game, take a moment to think about the intricate systems working tirelessly in the background to make your experience enjoyable. The internet truly is a game-changer!</p>
]]></content:encoded></item></channel></rss>