Order by CSS Specificity

后端 未结 1 1720
独厮守ぢ
独厮守ぢ 2021-02-09 03:15

My main goal is to try to reorder a CSS style block based on specificity. I previously had helped from SO and I managed to produce this function. See gist.

Here is an e

相关标签:
1条回答
  • 2021-02-09 03:50

    The problem should go away if you serialize

    html, body, body div{ 
        background: transparent; 
    } 
    

    as separate rules, as if they were written in the following manner:

    html{ 
        background: transparent; 
    } 
    body{ 
        background: transparent; 
    } 
    body div{ 
        background: transparent; 
    } 
    

    The semantics is a bit confusing, but basically you are judging an entire declaration based on independent selectors that have been combined into the same rule set.

    Re your update: specificity is not affected by selector order, i.e. the rules below have the same specificity. The last rule will take precedense rendering in blue, unless you re-arrange the rules rendering in red:

    <style>
        #container span {color:red;}
        div #content {color:blue;}
    </style>
    
    <div id="container"><span id="content">Hello world!</span></div>
    
    0 讨论(0)
提交回复
热议问题