CSS Specificity
Depending where CSS rules are defined they can have different consequences on your pages.
There are examples on the W3C Schools website and information relating to how Specificity is calculated and how you use it within your code.
I won't attempt to re-write or try to explain this any more than other websites. This is just a page relating to issues that I have come across that influenced my designs. For example, the higher precidence of ID selectors have a higher specificity than attribute selectors.
W3C say:
Specificity Hierarchy
The W3C website has a table that maps this out. However, they miss the Universal Selector. I have added this to the table.
Priority | Example | Description |
---|---|---|
Inline style | <h1 style="color:pink"> | Highest priority, directly applied with the style attribute |
Id selectors | #navbar | Second highest priority, identified by the unique id attribute of an element |
Classes and pseudo-classes | .test, :hover | Third highest priority, targeted using class names |
Attributes | [type="text"] | Low priority, applies to attributes |
Elements and pseudo-elements | h1, ::before, ::after | Lowest priority, applies to HTML elements and pseudo-elements |
Universal Selectors and combinators | * | (e.g., :not()) have the lowest specificity and are not counted in the specificity calculation. |