Skip to main content

Specificity In CSS


Introduction
In this article, we shall learn what Specificity is in CSS. We shall also learn how Specificity is calculated with some examples.

Let's get started now.
Specificity in CSS
Specificity defines the preference of classes out of several CSS classes which will be applied to a particular element. This case happens when more than one class points to a particular element in a document. Specificity is a numeric value shows which selector is pointing more specifically to that element.
In this case, a CSS class overrides to another CSS according to their higher specificity value.
Creating HTML file
To understand it let’s create a simple HTML file.
  1. <html>  
  2.     <head>  
  3.         <title>Learning about specificity in CSS</title>  
  4.     </head>  
  5.     <body>  
  6.         <p id="specificityBlog" class="blog">  
  7.           This is CSS blog and we are learning about CSS.  
  8.         </p>  
  9.     </body>  
  10. </html>  
Creating CSS class
To point out the above single paragraph we have created four CSS classes which are element, id and class selector as shown below.
  1. <style type="text/css">  
  2.     body{  
  3.       color : red;  
  4.       background-color: green;  
  5.     }  
  6.     p{  
  7.       color : blue;  
  8.       background-color: pink;  
  9.     }  
  10.     #specificityBlog{  
  11.       color : orange;  
  12.       background-color: yellow;  
  13.     }  
  14.     .blog{  
  15.       color : LightCoral;  
  16.       background-color: green;  
  17.     }  
  18. </style>  
Now, we shall save the file and open it into the browser. Now, inspect it and see the effect. It will be shown as below.
Specificity in CSS
 
Explanation
As we can see after inspecting and pointing to the paragraph tag, in the right side of the inspected window some of the CSS has been streaked i.e., it has less specificity and hence has been deleted. We can also see the CSS definition with the ID selector has been only applied because it has more specificity value.
To know more about CSS Specificity visit here.
Specificity Calculator
To know and calculate the specificity value you can visit the link here.
 
Specificity in CSS
 
Specificity in CSS
Here, we have calculated specificity value of class and ID. As we can see the specified value of the class is 10 while the specificity value of ID selector is 100. In this case, CSS with class selector will be overtraded by the CSS of ID selector.

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

First, FirstOrDefault, Single, SingleOrDefault In C#

For people who are new to LINQ, it is difficult to understand the difference between First, FirstOrDefault, Single, SingleOrDefault. In this blog, I will explain what to use and when.     I will take a simple example to make you understand practically how these methods work.   Consider a class Employee with properties as Id, Name, and Department. class  Employee {    public   int  Id {  get ;  set ; }    public   string  Name {  get ;  set ; }    public   string  Department{  get ;  set ; } } I have a list of Employees: List<Employee> employeeList =  new  List<Employee>(){    new  Employee() { Id = 1, Name =  "Sunny" , Department =  "Technical"  },    new  Employee() { Id=2, Name= "Pinki" , Department = "HR" },    new  Employee() { Id=3, Name= "Tensy" , De...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...