search
HomeWeb Front-endHTML TutorialExplanation of stacking and weighting in css

If the same tag is selected by multiple selectors, and each selector is set to the same style, who listens to this style when loading in the browser?

#For the same style set by different selectors, only one will be selected for loading and will not be superimposed.

In order to solve the problem of who to listen to, the concept of cascading is introduced.

CSS style cascading weight value

According to the CSS specification, the more specific the style rule, the higher the weight value. The basis for calculating the weight value is not "class is 10, tag is 1, ID is 100" as described in many articles - although this can get the correct result in most cases.

First let’s look at an easy-to-remember sequence of "important>inline>ID>class>label|pseudo-class|property selection>pseudo-object>inheritance>wildcard"

Calculation of selector weight value

A: If the rule is written in the style attribute of the tag (inline style), then A=1, otherwise, A=0. For inline style, since there is no selector , so the values ​​of B, C, and D are all 0, that is, A=1, B=0, C=0, D=0 (abbreviated as 1,0,0,0, the same below).

B: Count the number of IDs in the selector. If there is, B=1, if not, B=0 (for example, a selector like #header is calculated as 0, 1, 0, 0).

C: Calculate the number of pseudo-classes and other attributes in the selector (including class, attribute selectors, etc., excluding pseudo-elements). (For example, a selector such as .logo[id='site-logo'] is calculated as 0, 0, 2, 0) (Why it is 0,0,2,0 will be further explained later).

D: Count the number of pseudo-elements and tags in the selector. (For example, a selector like p:first-letter evaluates to 0, 0, 0, 2).



Stackability: Multiple selectors select the same tag and set the same style in the browser When loading, not all attribute values ​​will be loaded, only one of them will be loaded, and one value will cascade/cover the other values.

To achieve cascading or covering involves comparison, the following is the comparison selection There are two situations in weight comparison: 1. The selector selects the tag; 2. The selector does not select the tag

## First, let’s talk about the weight of the selector: id>class>label>* (wildcard);

##①The selector selects the label :

First: If both tags are selected, compare the selector weights.

Selectors have weights, and those with greater weights will cascade with those with smaller weights.

Calculate weight: The larger the range selected by the selector, the smaller the weight. id>class>label>*

Method: count the number of selectors, first compare the number of ids → then compare the number of classes → finally compare the number of tags.

##Annotation order in the picture (number of ids, number of classes, number of tags)

Style displayed on the page:

Console display:

Second: If the selector weight Same, compare the writing order of the code in css.

The css code has a loading order, loading from top to bottom, and the later loading will overwrite the previous loading.

 #box1 .box2 .box3 p{      (1,2,1)

    color: red;

  }

 .box1 #box2 .box3 p{    (1,2,1)

    color: green;

 }

 .box1 .box2 #box3 p{    (1,2,1) 书写顺序最后,层叠前面的样式    color: blue;

 }

② None of the selectors select the tag : Some styles can be inherited. Who inherits it?

First: Compare the distance between the elements selected by each selector and the target element p in HTML, and the ones that are closer are stacked and those that are farther away. Referred to as the proximity principle.

 #box1{    

   color: red;

 }

  .box1 .box2{

    color: green;

  } 

  .box3{           选中的标签距离p最近,继承他的

    color: blue;

  }

Secondly: If the distances are equally close, compare the weights, and the cascade with greater weight will have a smaller weight.

 #box1 .box2 #box3{               (2,1,0)

    color: red;

  }

  .box1 #box2.box2 #box3{   (2,2,0)    color: green;

  }

  .box1 .box2 #box3.box3{     (1,3,0)

    color: blue;

  }

Again: If the distance is the same, the selector weight is the same, depending on the writing order.

 #box1 .box2 #box3.box3{

    color: red;

  }

  .box1 #box2.box2 #box3{

    color: green;

  }

  #box1.box1 #box2 .box3{

    color: blue;

  }

Specially, during the weight comparison process, there is a word important that can increase the weight of a certain style attribute to the maximum.

Based on the principle of proximity, important has no effect on inheritance.

 #box1 .box2 #box3.box3{

    color: red;

  }

  #box3{

    color: green !important;    将这条属性的权重提升的最大,与选择器权重无关

  }

  #box1.box1 #box2 .box3{

    color: blue;

  }

## In summary:

Finally:

Move up comparisons are based on CSS inline as an example, in the css inline, inline In the external link type, the weight is: inline > inline = external link. As the name suggests, no matter how heavy the weight of the inline or external link is, it cannot equal the weight of an inline type!

The above is the detailed content of Explanation of stacking and weighting in css. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools