InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS selectors

    Simple Selectors

    • type selectors
    • universal selector (*)
    • attribute presence and value selectors
    • attribute substring selectors
    • class selectors
    • two classes (.c1.c2)
    • id selectors

    Pseudo-classes

    • :link
    • :visited
    • :hover
    • :active
    • :focus
    • :target
    • :lang
    • :enabled and :disabled
    • :checked
    • :root
    • :nth-child
    • :nth-of-type
    • :nth-child vs :nth-of-type
    • :nth-last-child
    • :nth-last-of-type
    • :first-child
    • :first-of-type
    • :last-child
    • :last-of-type
    • :only-child
    • :only-of-type
    • :empty
    • :not

    Pseudo-elements

    • ::first-line
    • ::first-letter
    • ::before and ::after
    • ::placeholder

    Combinators

    • child vs descendant selector
    • adjacent sibling selector (E1 + E2)
    • general sibling selector (E1 ~ E2)

    Specificity

    • Selector specificity
     
    • Home
    • > Tutorials
    • > CSS
    • > CSS selectors

    CSS selector specificity

    on Apr 10, 2016

    When two selectors have same style, then one with higher specificity wins. In case they have same specificity, the one which come later, wins.

    Specificity rules

    Generate a number of the form a-b-c-d (in a number system with a large base). The value of a, b, c and d will be calculated using these rules:

    a= count 1 for css in style attribute (otherwise 0). When it is 1, then b=c=d=0
    b= count the number of ID selectors
    c= count the number of class selectors, attributes selectors, and pseudo-classes
    d= count the number of element names and pseudo-elements in the selector

    Few points to note:

    • Universal selector can be ignored.
    • Repeated occurrances of the same simple selector are allowed and do increase specificity.

    Specificity examples

     *             {}   /* a=0 b=0 c=0 d=0 -> specificity = 0,0,0,0 */
     li            {}   /* a=0 b=0 c=0 d=1 -> specificity = 0,0,0,1 */
     li::first-line {}  /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */
     ul li         {}   /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */
     ul ol+li      {}   /* a=0 b=0 c=0 d=3 -> specificity = 0,0,0,3 */
     h1 + *[rel=up]{}   /* a=0 b=0 c=1 d=1 -> specificity = 0,0,1,1 */
     ul ol li.red  {}   /* a=0 b=0 c=1 d=3 -> specificity = 0,0,1,3 */
     li.red.level  {}   /* a=0 b=0 c=2 d=1 -> specificity = 0,0,2,1 */
     #x34y         {}   /* a=0 b=1 c=0 d=0 -> specificity = 0,1,0,0 */
     style="color:red"  /* a=1 b=0 c=0 d=0 -> specificity = 1,0,0,0 */
    

    Example css code

    Specificity order in this example
    attribute style > id > class > element

    <style>
    div {
      width: 50px; height: 50px; background-color:orange;
    }
    .box {
      background-color:lightgreen;
    }
    #id1 {
      background-color:lightblue;
    }
    </style>
    
    <div>
    </div>
    <div class="box">
    </div>
    <div id="id1" class="box">
    </div>
    <div style="background-color:lightgray" class="box">
    </div>
    refresh done
    try it online

    Specification

    • W3C CSS 3 selector specificity specification
    • W3C CSS 2.1 selector specificity specification

    Suggested posts:

    1. CSS ::before and ::after examples
    2. CSS class selectors – define style by class name
    3. CSS type selectors – define style by element name
    4. CSS pseudo class :visited – style for visited links
    5. PHP – print array in one line
    6. CSS animation-play-state
    7. CSS :empty – define style for empty element
    8. AngularJS format date using date filter
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS Pseudo Elements, CSS selectors, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress