What is specificity in CSS

Specificity in CSS is a set of rules that determine which styles are applied to an HTML element when multiple styles could apply. It is calculated based on the types of selectors used in the CSS rule. The higher the specificity of a selector, the more precedence it has over other selectors when applying styles to an element.

Specificity is typically calculated as follows: - Inline styles (added directly to an element) have the highest specificity. - IDs have a higher specificity than classes, attributes, and pseudo-classes. - Classes, attributes, and pseudo-classes have a higher specificity than elements and pseudo-elements. - The universal selector (*) and inherited styles have the lowest specificity.

Here is a basic example to illustrate specificity in action:

This text will be red due to ID specificity.


Specificity CSS specificity CSS styles selector specificity web design.