Home Web Front-end CSS Tutorial CSS FundamentalsSpecificity and Inheritance

CSS FundamentalsSpecificity and Inheritance

Nov 21, 2024 am 08:21 AM

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:

Fundamentos de CSSEspecificidad y Herencia

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.

Fundamentos de CSSEspecificidad y Herencia

- Specificity Calculator

Just place your selector and visually, you will be able to know how much its specificity is equivalent to.

Fundamentos de CSSEspecificidad y Herencia

  • 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.

Fundamentos de CSSEspecificidad y Herencia

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:

Fundamentos de CSSEspecificidad y Herencia

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 used

Fundamentos de CSSEspecificidad y Herencia

In cases where we want an element to ignore an inherited property, we can reset it to its initial value using initial:

Fundamentos de CSSEspecificidad y Herencia

Good Practices and Considerations in Specificity and Inheritance

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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 .

  7. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

See all articles