Specificity and Hierarchy in CSS.


As you learn more about how CSS functions, you start to realize that it is more than just formatting. You must consider each style's significance and superiority over other styles. The CSS attribute of specificity controls how CSS rules are applied to elements. We will examine the specificity of CSS and its significance in web design in this article.
What is specificity in CSS?
Specificity is a value that decides which CSS rule is applied to an element when multiple rules target the same element. The rule with the highest specificity value takes priority when there is a conflict between two or more rules.
CSS specificity is determined by the mix of selectors used in a rule. The greater the specificity value of a selector, the more specific it is. For instance, the specificity number of an ID selector is higher than that of a class selector, which is higher than that of an element selector.
The following table shows the specificity values of different CSS selectors:
Selector | Specificity value |
ID selector | 100 |
Class selector, attribute selector, pseudo-class selector | 10 |
Element selector | 1 |
Universal selector (*) | 0 |
The specificity value of a CSS rule is calculated by adding up the specificity values of its selectors. For example, #main .container p
has a specificity value of 111 (100 for the ID selector, 10 for the class selector, and 1 for the element selector).
Why is specificity important?
Specificity is crucial because it decides which CSS rules are applied to webpage elements. The rule with the highest specificity value gets precedence when numerous rules are directed at the same element. As a result, you must ensure that your new rule has a higher specificity value if you want to replace a previously specified rule.
Effective use of specificity can help you prevent disputes and make sure your CSS rules are implemented properly. By making it simpler to understand which rules are applied to which components, it can also assist you in writing CSS code that is easier to maintain.
There are several ways to increase the specificity of a CSS rule:
Use ID selectors: Using ID selectors can make your rules more specific because they have the greatest specificity value. However, you should be careful when using ID selectors as they can make your code harder to manage and update.
Use multiple selectors: A rule's specificity value can be increased by using multiple selectors. For example,
#main .container p
has a higher specificity value than.container p
.Use "!important": If a CSS rule contains the keyword "
!important
" it will take precedence over all other rules, independent of their specificity. However, using "!important
" should be avoided if possible, as it can make your code more difficult to manage and debug.Use inline styles: The most specific styles are inline styles because they are applied straight to an element. Inline styles should only be used sparingly, though, as they can make it more difficult to manage and update your code.
Now, when it comes to CSS specificity, we've covered a lot of ground. Before we call it a day, let's take a quick peek at Hierarchy in CSS.
What is Hierarchy in CSS?
Hierarchy in CSS refers to the order of precedence that determines how different CSS rules apply to the same element. When an element is subject to two or more rules, the browser first applies the rule with the greatest precedence. This implies that if there is a conflict between two or more rules, the rule with the higher precedence wins.
The user agent level, author level, and user level are the three layers that make up CSS's hierarchy. The rules at each level can take precedence over the rules at lesser levels. Each level has its own set of rules.
User-agent level: The user-agent level includes the default styles that are built into the browser. When no other styles are specified, these are the styles that are automatically applied to HTML elements. In the CSS structure, user-agent styles are given the least importance.
Author level: The styles specified in internal style sheets, external style sheets, and inline styles are all included at the author level. External style sheets are separate files that are linked to HTML documents and contain CSS rules. Internal style sheets are defined in the head section of an HTML document and contain CSS rules. Using the
style
attribute, inline styles are defined directly in the HTML elements.Author-level styles take priority over user-agent styles, which means they take precedence over the browser's default styles.
User level: The styles that the user defines, such as those used by browser addons or user style sheets, are included at the user level. User-level styles take precedence over all other styles because they are given the greatest priority in the CSS hierarchy.
Why is hierarchy important?
Hierarchy in CSS is important because it determines how different CSS rules apply to the same element. You can influence how elements are styled on your webpage by knowing the hierarchy.
When working with CSS rules that conflict, hierarchy is especially crucial. When several principles apply to the same element, conflicts can arise, which may lead to unexpected behaviour or styling. You can write CSS rules that have the right precedence and prevent conflicts by knowing the hierarchy.
How to control hierarchy in CSS:
There are several ways to control hierarchy in CSS. They include;
Use selectors: When we want to target particular HTML components and apply CSS rules to them, we use selectors. Effective selector use allows you to manage the structure of your CSS rules. A selector's priority in the hierarchy increases with its degree of specificity. An ID selector, for instance, takes priority over a class selector.
Use the "!important" keyword: The "!important" keyword is used to override all other styles and give a CSS rule the highest precedence. However, the "!important" keyword should be used sparingly, as it can make it difficult to override styles later on.
Use cascading rules: Rules that are applied to every aspect of a webpage are known as cascading rules. These rules can be used to set default styles or to apply styles to numerous elements at once, but they have lesser precedence than more specific rules.
Use the cascade: The cascade is the method used to apply CSS rules to HTML components. Knowing the cascade will help you create CSS rules with appropriate precedence.
Best practices for using hierarchy in CSS:
Hierarchical structure in CSS is essential for creating organized and maintainable stylesheets. Here are some best practices for using hierarchy in CSS;
Use a consistent naming convention for classes and IDs: For your classes and IDs, use names that are useful and descriptive. This will make it simpler to locate particular elements and comprehend the hierarchy of your stylesheets.
Use nested selectors: To focus on particular components within a hierarchy, use nested selectors. Your CSS will be more effective and simpler to understand as a result.
Use parent selectors: To target components that are contained within other elements, use parent selectors. When you want to apply styles to a collection of components that have a common parent, this is helpful.
Use inheritance: Use inheritance to avoid repeating styles. By applying styles to a parent element, you can inherit those styles to all of its child elements.
Avoid using too many levels of hierarchy: While hierarchy helps organize your CSS, using too many layers of nesting can make it overly complex. To keep your CSS manageable, limit your hierarchy to a few levels.
Use comments to explain your hierarchy: Use comments to describe your stylesheets' hierarchical structure. This will facilitate future code maintenance and assist other writers in comprehending the structure of your styles.
You can create well-structured, maintainable stylesheets that are simple to peruse and comprehend by adhering to these best practices.
I trust you enjoyed reading about how CSS's hierarchy and specificity function. With enough practice, you will get the hang of it in no time!
Subscribe to my newsletter
Read articles from Kelvin Ofili directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Kelvin Ofili
Kelvin Ofili
I am a Software Developer, with my main focus on Frontend Web Development. I am very proficient with JavaScript and its surrounding frameworks and libraries like ReactJs, NodeJs, and TypeScript. My blog here is centred towards beginners. I am writing for people like me at the start of my tech growth. My articles are what would have worked perfectly for me. After much experience, I have decided to try to recreate what I would have loved to see when I was starting out. Hopefully, this works for someone out there.