Does embedded css always override external css?

前端 未结 4 1842
南方客
南方客 2020-12-11 02:05

I had studied earlier that embedded CSS always overrides external css. But I found that whichever comes last in the code, those styles prevail.

Please see the follo

4条回答
  •  一个人的身影
    2020-12-11 02:23

    Which CSS rules are applied depends on the specificity of the CSS rule, where that rule is placed, and the presence of !important. If two contradictory rules are placed, the rule declared later will overwrite the previous rule. If two contradictory rules are declared with selectors of varying specificity, the more specific styles will win, regardless of placement. If a rule is marked as !important e.g.

    h1 {
      color: green !important;
    }
    

    the !important rule will always win.

    For reference the list of specificity of CSS selectors goes like this (from most specific to least):

    1. Style attributes
    2. ID
    3. Class, pseudo class, attribute
    4. Elements

提交回复
热议问题