In this article, we will explore two key CSS3 concepts that every layout designer and front-end developer should master: specificity and inheritance. These fundamentals are essential to understand how styles are applied and how to control their behavior in different elements of our page.
Specificity
Specificity determines how "specific" a selector is in deciding which style to apply. This is calculated with a numerical value based on the type of selectors we are using:
- Tags and pseudo-elements: value of 001
- Classes, attributes and pseudoclasses: value of 010
- IDs: value of 100
- Online styles: value of 1000
- !important: overwrites everything, so it is recommended to avoid it to avoid breaking the specificity.
Example of specificity:
In this case, the specificity calculation is 111, obtained by adding:
h1 (label) = 001
.title (class) = 010
#title (ID) = 100
Total = 111
The higher the number, the greater the specificity, and this allows one selector to have more weight than another regardless of the order in the style sheet (or cascade).
Note: The cascade will work as long as the specificity of the selectors on the same element is the same. This is why it is recommended to use classes to keep specificity manageable and avoid conflicts.
There are tools that help us analyze the specificity in our code, for example:
- CSS Specificity Graph Generator
If we see high spikes in the graphs of this tool, it is a sign that specificity could be being poorly managed.
- Specificity Calculator
Just place your selector and visually, you will be able to know how much its specificity is equivalent to.
- Visual Studio Code also allows you to know the specificity of your selectors. You just need to position yourself over the selector you want to review, and the tool will show you how specific it is.
Inheritance
Inheritance in CSS is the ability of certain elements to "inherit" properties from their containing elements. This means that some styles applied to a container element are automatically passed to its descendants.
For example, in the following code, the element within inherits the styles from the h1 tag, while the outside h1 does not inherit them:
Commonly inherited properties:
- color
- All font-related properties (such as font-family, font-size, etc.)
Note: Links () do not automatically inherit color styles from their parent elements, as they usually have a distinctive style by default. To apply inherited styles on them, we can use a specific class or the value inherit.
To force inheritance on properties that normally do not have it, the value inherit:
is usedIn cases where we want an element to ignore an inherited property, we can reset it to its initial value using initial:
Good Practices and Considerations in Specificity and Inheritance
Avoid excessive use of IDs in selectors: Although IDs have high specificity, overusing them can make CSS difficult to maintain and overwrite. It is preferable to use classes to keep the code more flexible.
Use classes for reusability and scalability: Classes allow you to apply styles consistently to multiple elements. This approach is especially useful in large projects as it improves maintainability.
Avoid using !important whenever possible: !important overrides all specificity, but can complicate code maintenance and cause conflicts. Use it only in very specific situations and when absolutely necessary.
Understand the cascade and flow of styles: The cascade (the order in which styles are applied) is still important. If two selectors have the same specificity, the style closest to the end of the style sheet will be applied. This behavior can be leveraged in global and specific styles.
Consider the impact of inherited layout on components: When applying inheritance, make sure descendant elements get the desired styles. Inheritance is not always obvious, and a change to a container can affect multiple elements unexpectedly.
Use well-structured selectors to improve readability: When using nesting or descending selectors, try to maintain a clear structure and avoid excessive nesting, which can make the code more difficult to read and overwrite .
Optimize with specificity analysis tools: There are tools and extensions that help you visualize and analyze the specificity of your selectors, which can be useful in complex projects. This also helps identify specificity peaks that may require refactoring.
These fundamentals will allow you to control how styles are applied to your page, achieving a cleaner and more professional design. In the next article, we will delve into other important properties of CSS3 to further improve our understanding and handling of styles in web projects.
The above is the detailed content of CSS FundamentalsSpecificity and Inheritance. For more information, please follow other related articles on the PHP Chinese website!

The fact that anchor positioning eschews HTML source order is so CSS-y because it's another separation of concerns between content and presentation.

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
