Home > Article > Web Front-end > How can I override a conflicting CSS rule from an external stylesheet when using inline styling?
In this scenario, the inline style padding-left: 10px applied to table cells in the rightColoumn ID is being overridden by a conflicting rule from an external stylesheet. The referenced stylesheet contains a rule that sets margin and padding to 0 for all elements within the rightColumn class.
To determine which CSS rule takes precedence, a concept called specificity comes into play. Specificity is a measure of how specific the selector of a CSS rule is. The more specific a selector, the higher its specificity.
CSS 2.1 defines the following rules for calculating specificity:
These four values are concatenated in the form a-b-c-d, where a is the value from the first rule, b from the second rule, and so on. The higher the numerical value, the higher the specificity.
In this case, the inline style has a specificity of 0001 (no ID attributes, no other attributes or pseudo-classes, one element name). The conflicting rule from the external stylesheet has a specificity of 0010 (no ID attributes, one other attribute or pseudo-class, no element names).
Since 0010 is higher than 0001, the rule from the external stylesheet takes precedence and overrides the inline style.
To override the conflicting rule, there are two main options:
The above is the detailed content of How can I override a conflicting CSS rule from an external stylesheet when using inline styling?. For more information, please follow other related articles on the PHP Chinese website!