What is the order of precedence for CSS?

前端 未结 8 1744
攒了一身酷
攒了一身酷 2020-11-22 09:43

I\'m trying to figure out why one of my css classes seems to override the other (and not the other way around)

Here I have two css classes

.smallbo         


        
8条回答
  •  悲&欢浪女
    2020-11-22 10:30

    What we are looking at here is called specificity as stated by Mozilla:

    Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Specificity is based on the matching rules which are composed of different sorts of CSS selectors.

    Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector. When multiple declarations have equal specificity, the last declaration found in the CSS is applied to the element. Specificity only applies when the same element is targeted by multiple declarations. As per CSS rules, directly targeted elements will always take precedence over rules which an element inherits from its ancestor.

    I like the 0-0-0 explanation at https://specifishity.com:

    Quite descriptive the picture of the !important directive! But sometimes it's the only way to override the inline style attribute. So it's a best practice trying to avoid both.

提交回复
热议问题