Home >Web Front-end >CSS Tutorial >Why Are My Inline Styles Being Overridden by My Stylesheet?
CSS Precedence: Why Inline Styles Are Overridden
In CSS, styles are applied to elements based on the precedence of their rules. When multiple rules target the same element, the one with the highest precedence will take effect.
In the provided example, you have an inline style that sets padding-left: 10px for td elements within a table with the rightcolumn ID. However, a style from a referenced stylesheet sets margin and padding to 0 for all elements within the .rightColumn class. The issue is that the styles from the referenced stylesheet have higher precedence, causing the inline styles to be overridden.
Calculating Specificity
CSS precedence is determined by the specificity of rules. Specificity is calculated based on the following criteria:
For example, a rule with the selector .rightColumn * has a specificity of 0010 (a = 0, b = 0, c = 1, d = 0), while a rule with the selector td has a specificity of 0001 (a = 0, b = 0, c = 0, d = 1). Since 0010 is greater than 0001, the rule from the referenced stylesheet has higher precedence.
Resolving the Issue
To resolve this issue and apply the inline styles, you have two options:
The above is the detailed content of Why Are My Inline Styles Being Overridden by My Stylesheet?. For more information, please follow other related articles on the PHP Chinese website!