Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

CSS | Specificity

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

When more than one set of CSS rules apply to the same element, the browser will have to decide which specific set will be applied to the element. The rules the browser follows are collectively called Specificity
 

Specificity Rules include:  

  • CSS style applied by referencing external stylesheet has lowest precedence and is overridden by Internal and inline CSS.
  • Internal CSS is overridden by inline CSS.
  • Inline CSS has highest priority and overrides all other selectors.

Example: 

html




<html>
 
<head>
    <link rel="stylesheet" type="text/css" href="external.css">
    <style type="text/css">
        h1 {
            background-color: red;
            color: white;
        }
         
        h2 {
            color: blue;
        }
    </style>
</head>
 
<body>
    <h1>
        Internal CSS overrides external CSS
    </h1>
    <h2 style="color: green;">
        Inline CSS overrides internal CSS
    </h2>
</body>
 
</html>


“external.css” of Example-1: 
 

html




h1{
    background-color: lightgreen;
}
h2{
    color: pink;
}


Output: 
 

inline internal and external css

Specificity Hierarchy :Every element selector has a position in the Hierarchy. 

  1. Inline style: Inline style has highest priority. 
     
  2. Identifiers(ID): ID have the second highest priority. 
     
  3. Classes, pseudo-classes and attributes: Classes, pseudo-classes and attributes are come next. 
     
  4. Elements and pseudo-elements: Elements and pseudo-elements have lowest priority. 
     

Example-2: 

html




<html>
 
<head>
    <style type="text/css">
        h1 {
            background-color: red;
            color: white;
        }
         
        #second {
            background-color: black;
            color: white;
        }
         
        .third {
            background-color: pink;
            color: blue;
        }
         
        #second1 {
            color: blue;
        }
         
        .third1 {
            color: red;
        }
    </style>
</head>
 
<body>
    <h1 id="second" class="third">
        ID has highest priority.
    </h1>
    <h1>
        Element selectors has lowest priority.
    </h1>
    <h1 class="third">
        Classes have higher priority than element selectors.
    </h1>
 
    <h2 style="color: green;" id="second1" class="third1">
        Inline CSS has highest priority.    </h2>
 
</body>
 
</html>


Output: 
 

Specificity Hierarchy gfg

Specificity Hierarchy

Note: 

  • When two or more selectors have equal specificity, the last(latest) one counts.
  • Universal selectors like body and inherited selectors have least specificity.

My Personal Notes arrow_drop_up
Last Updated : 27 Jul, 2021
Like Article
Save Article
Similar Reads
Related Tutorials