Nov 27, 2024
Hello everyone, welcome to WebDevPlusPlus – your one-stop destination for all things web development! So far, we’ve explored CSS Selectors, CSS Combinators, and CSS Inheritance.
In this post, we’re diving into CSS Specificity – a tricky but essential topic in CSS. It’s something that many courses and tutorials don’t cover, but it’s crucial if you want to become a great frontend developer. Consider yourself lucky to have found WebDevParadise!
Let’s get started and master CSS Specificity together!
Specificity determines how CSS rules are applied and which rule takes priority when multiple rules target the same HTML element. Simply put, every CSS rule has a weight, called specificity, that tells the browser which rule to apply. The browser uses this information to decide which styles should be applied. It might sound tricky, but it will make sense as you read through this blog post. Let’s dive in and learn more!
Now, Before moving further, let's take a look at how our code files for this article looks like.
In your machine, create a folder named specificity. Within that folder create the following two files.
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1 class="heading" id="header">Main Heading</h1>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
The above code is straightforward. It contains a basic HTML boilerplate with an h1
tag inside the body. The h1
tag has a class name (heading) and an id (header). The CSS file is already linked to the HTML file.
When you save the HTML file and open it in your browser, it will look like this:
Now, let’s dive into the core topic—CSS Specificity.
Before moving into Specificity, Lets see the default CSS cascading behavior. In your CSS file add the below style.
cssh1 {
color: blueviolet;
}
1
2
3
Now save the files and reload the browser.
As you can see, the styles were applied successfully.
Now add one more style as below to the same h1 element with color: green.
cssh1 {
color: blueviolet;
}
h1 {
color: green;
}
1
2
3
4
5
6
7
Now save the file and reload the browser
As you can see, the first style with color: blueviolet
is ignored, and color: green
is applied to the h1
tag. This happens because both CSS rules have the same specificity (element-level).
This is the default behavior in CSS. The browser scans the CSS file from top to bottom and applies the style that appears last.
In this case, the browser replaced blueviolet
with green
because the most recent style for color
was green
. However, the real challenge arises when we target the same element with rules of different specificity weights. Let’s explore that scenario now.
When we apply multiple styles to a single HTML element using different CSS rules with varying specificity weights, a "collision" or "conflict" between these rules occurs.
To see this in action, add the following style to the top of your CSS file, above all other rules:
css.heading {
color: orange;
}
1
2
3
Now your CSS file will look like this:
css.heading {
color: orange;
}
h1 {
color: blueviolet;
}
h1 {
color: green;
}
1
2
3
4
5
6
7
8
9
10
11
If you save the files and reload the browser. You will see some thing like this:
Woah! Wait, what? Why is the color from the top rule (color: orange
) being applied instead of the green color at the bottom of the file?
Welcome to the concept of Specificity in CSS! This behavior happens due to the specificity of the CSS rules. In this case, we are using a class selector, and the specificity of a class selector is higher than that of an element selector.
As a result, the CSS rule with the class selector (.heading {}
) overrides the rule with the element selector (h1 {}
). That’s why the color orange is applied instead of green.
The specificity priorities of different selectors in CSS are illustrated below:
As shown in the chart, the priority order is:
!important
> ID Selector > Class Selector > Element Selector.
For now, ignore !important
. We'll discuss it at the end of this article.
Now, let's create a Specificity Chart for the styles we've written so far:
As you can see:
.heading {}
, we have: 0 !important
, 0 IDs, 1 class, 0 elements.h1 {}
, we have: 0 !important
, 0 IDs, 0 classes, 1 element.To determine which style wins, we analyze the specificity from left to right in the table:
!important
: Both selectors have 0 !important
declarations (we're ignoring !important
for now)..heading {}
has 1 class, while h1 {}
has 0 classes. Therefore, .heading {}
wins since class selectors have higher specificity than element selectors.This is why the orange color defined in .heading {}
is applied instead of the green color in h1 {}
.
Now that you understand specificity priorities, let's add a new CSS rule with an ID selector at the top of the styles we've written so far and see how it behaves.
Add the following style to your CSS file:
css#header {
color: red;
}
1
2
3
Now your CSS file will look like this:
css#header {
color: red;
}
.heading {
color: orange;
}
h1 {
color: blueviolet;
}
h1 {
color: green;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Save the file and reload the browser.
Now, If we plot the Specificity chart, It will look like this:
As you can see:
#header {}
, we have: 0 !important
, 1 ID, 0 classes, 0 elements..heading {}
, we have: 0 !important
, 0 IDs, 1 class, 0 elements.h1 {}
, we have: 0 !important
, 0 IDs, 0 classes, 1 element.!important
: All selectors have 0 !important
declarations, so we move to the next column.#header {}
has 1 ID, while the other selectors have 0 IDs. Since IDs have the highest specificity after !important
, the #header {}
rule wins.This is why the color red from #header {}
is applied, as shown in the example below:
Now, let's add another CSS rule at the very top of our CSS file above all the previous rules. Add the following style:
css.heading#header {
color: brown;
}
1
2
3
Now, Our CSS file will look like this:
css.heading#header {
color: brown;
}
#header {
color: red;
}
.heading {
color: orange;
}
h1 {
color: blueviolet;
}
h1 {
color: green;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Now let’s update the specificity chart:
The browser first looks for the style with the most IDs.
#header {}
and .heading#header {}
.Since both styles have the same number of IDs, the browser moves to the next column in the specificity chart: classes.
.heading#header {}
has 1 class, while #header {}
has 0 classes.
.heading#header {}
wins the specificity fight, and the color brown is applied, as shown below:When calculating specificity, always move from left to right in the specificity chart:
Even if a CSS rule contains a very large number of classes, a single ID will always have higher specificity. That’s the power of IDs in CSS specificity.
In the HTML file, add two more classes (heading-main
and heading-top
) to the <h1>
element. Your updated HTML will look like this:
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1 class="heading heading-main heading-top" id="header">Main Heading</h1>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
Now add the following style at the top of our CSS file as shown below:
css.heading-main.heading-top#header {
color: blue;
}
1
2
3
Now our CSS file will look like this:
css.heading-main.heading-top#header {
color: blue;
}
.heading#header {
color: brown;
}
#header {
color: red;
}
.heading {
color: orange;
}
h1 {
color: blueviolet;
}
h1 {
color: green;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
First, the browser checks the ID count for all the CSS rules. In this case, three CSS rules have 1 ID:
Since all three rules have the same number of IDs, the browser then checks for the highest number of classes among these rules.
As shown above, .heading-main.heading-top#header {}
has 2 classes, which is the highest. Therefore, the browser applies the styles from .heading-main.heading-top#header {}
. The resulting color is blue, as demonstrated below:
Inline styles always take precedence over internal and external styles unless a style is marked with the !important
value. To demonstrate this, let’s add an inline style to the <h1>
tag in our index.html
file as shown below:
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1
class="heading heading-main heading-top"
id="header"
style="color: yellow"
>
Main Heading
</h1>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Now save the html file and reload the browser.
The !important
declaration has the highest priority in CSS, even overriding inline styles. When a CSS rule includes !important
, it will take precedence regardless of the number of IDs, classes, or elements in competing rules. Let’s explore this behavior.
Update the style h1 { color: blueviolet; }
in your CSS file to include !important
, as shown below:
cssh1 {
color: blueviolet !important;
}
1
2
3
Now, Your CSS file will look like this:
css.heading-main.heading-top#header {
color: blue;
}
.heading#header {
color: brown;
}
#header {
color: red;
}
.heading {
color: orange;
}
h1 {
color: blueviolet !important;
}
h1 {
color: green;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Now lets plot the Specificity chart.
As you can see for h1 selector we have !important value with one, which is greater than all other selectors in our example. And browser will ignores all other selectors and apply this selector with !important as shown below:
That's it for this post, everyone! I hope you found it helpful and enjoyed learning about CSS specificity.
!important
, unless absolutely necessary, as they can make your CSS harder to maintain and debug.Thank you for reading! Stay tuned to WebDev Paradise for more informative posts like this. Until next time, happy coding! 🚀
Welcome, Learn essential CSS concepts and stay up-to-date with the latest trends and techniques in web design. Join us to enhance your CSS skills today.
Read moreIntroduction to CSS. What is CSS and How to use it to design your web layouts and components to become a better frontend developer.
Read moreDiscover how to connect CSS with HTML using inline, internal, and external stylesheets. This beginner-friendly guide covers methods and examples to style in css
Read moreMastering CSS Selectors: Guide for Targeting HTML Elements using HTML Element Selector, ID Selector, Class Selector , Attribute Selector, Universal Selector
Read moreCSS Combinators: Selectors, Descendant Combinations, Child Combinations, Sibling Combinations, and More
Read more