CSS specificity hierarchy
CSS specificity hierarchy listed from highest to lowest priority:
Inline Styles: These are styles applied directly to an element using the
style
attribute.- Example:
<div style="color: red;">
- Example:
ID Selectors: These are styles applied using the ID of an element, which is unique within a page.
- Example:
#header { color: blue; }
- Example:
Class Selectors, Attribute Selectors, and Pseudo-classes: These include styles applied using class names, attributes, and pseudo-classes.
- Examples:
- Class:
.main { color: green; }
- Attribute:
[type="text"] { color: yellow; }
- Pseudo-class:
:hover { color: purple; }
- Class:
- Examples:
Element Selectors and Pseudo-elements: These are styles applied directly to HTML elements and pseudo-elements.
- Examples:
- Element:
p { color: black; }
- Pseudo-element:
::after { content: ""; }
- Element:
- Examples:
Universal Selector and Inherited Styles: These styles have the lowest specificity and include the universal selector
*
and inherited styles from parent elements.- Examples:
- Universal:
* { color: gray; }
- Inherited styles are those passed from parent elements to their children.
- Universal:
- Examples:
Understanding this hierarchy helps in determining which styles will be applied when there are conflicting rules.
Comments
Post a Comment
What is your thought about this?