1-Position, 2-specificity( element ,class,attribute,id.) 3-type 4-importance
The correct order of cascade specificity, from highest to lowest, is as follows:
Importance: Importance is determined by using the
!important
declaration in CSS. Styles marked as!important
have the highest priority and override all other styles, regardless of their specificity. It is generally recommended to use!important
sparingly and only when necessary.Specificity: Specificity is calculated based on the combination of CSS selectors used to target an element. It determines the priority of styles when there are conflicting styles targeting the same element. The order of specificity, from highest to lowest, is as follows:
Inline styles: Styles defined directly within an HTML element using the "style" attribute.
ID selectors: Styles applied to elements with a specific "id" attribute.
Class selectors: Styles applied to elements with a specific class using the "." notation.
Attribute selectors: Styles applied to elements with a specific attribute.
Type selectors: Styles applied to elements based on their HTML tag name.
Universal selectors: Styles applied to all elements on a page.
If multiple styles have the same specificity, the one that appears later in the CSS file takes precedence.
- Element: If two conflicting styles have the same specificity, the one targeting a more specific element takes precedence. For example, a style targeting
<div>
would take precedence over a style targeting<p>
.
It's important to note that "importance" is not a widely used concept in CSS and should be used with caution. The primary mechanism for resolving style conflicts is through cascade specificity.
Subscribe to my newsletter
Read articles from Vaishnav Srivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by