CSS Specificity Formula:
From: | To: |
CSS specificity determines which CSS rule is applied by browsers when multiple rules could apply to the same element. It's a weight that determines which style declarations take precedence.
The calculator uses the specificity formula:
Where:
Explanation: The formula creates a score where higher values indicate higher specificity. Inline styles have the highest weight, followed by IDs, then classes/attributes, then elements.
Details: Understanding specificity helps prevent CSS conflicts, makes styling more predictable, and reduces the need for !important declarations. It's crucial for maintaining large stylesheets.
Tips: Enter counts for each selector type in your CSS rule. The calculator will compute the specificity score which you can compare with other rules.
Q1: What's the highest possible specificity?
A: Theoretically unlimited, but practical maximum is 1,0,0,0 for inline styles. For non-inline, it's 0,∞,∞,∞ but browsers limit ID counts.
Q2: How does !important affect specificity?
A: !important doesn't change specificity but overrides normal declarations. An !important rule with lower specificity beats a normal rule with higher specificity.
Q3: Do universal selectors (*) count?
A: No, universal selectors have specificity 0,0,0,0 and don't affect the calculation.
Q4: How do nested selectors affect specificity?
A: Each component in a selector chain contributes to the total count. For example, "ul li.active" would count as 0,0,1,2.
Q5: Does order in the stylesheet matter?
A: Only when specificity is equal - the last rule defined takes precedence when specificity is the same.