![]() |
VOOZH | about |
CSS specificity is the set of rules browsers use to determine which CSS style is applied when multiple rules target the same element. The rule with the higher specificity value takes priority.
Note:
- The Specificity is mainly determined by the Selectors themselves, rather than whether the styles are defined internally within a <style> tag or externally in a separate CSS file, in the case of specificity for external or internal CSS rules.
- If the selectors used in External and Internal CSS contain the same components, then their specificity will be the same, and it will not be affected by the location of the CSS rule.
- If any conflict exists for the styles of the same element, then the styles will be implemented based on the order of inclusion in the HTML document.
- When two or more selectors have equal specificity, then the last(latest) one counts.
- Universal selectors (like body and inherited selectors) have the least specificity.
Every Selector has a position in the Hierarchy, which is described below:
| Priority | Description |
|---|---|
| Inline style | Highest priority, directly applied using the style attribute. |
| ID selectors | Second highest priority, identified by the unique id attribute of an element. |
| Classes, pseudo-classes, | Medium level of specificity, targeted using class names, pseudo-classes like :hover, and |
| attributes | attributes like [type="text"]. |
| Elements and pseudo-elements | Lowest priority, applies to HTML elements and pseudo-elements such as ::before and ::after. |
Example: