•There are many selectors in CSS. What will happen if multiple selectors are applied to the same element? Code <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> p{ color:red; font-size: 10px; } #wrap{ color: deeppink; font-size: 30px; } .box{ color:yellow; font-size: 50px; } </style> </head> <body> <p class='box' id="wrap"> Guess what color I am</p> </body> </html> • Execution can produce results •Summary: This effect occurs because the browser determines which CSS style to use based on the weight value. The higher the weight value, the higher its priority will be, and that CSS style will be presented. The weight value of the id selector is 100> class selector 10> tag selector 1, so the final result is the style set by the id selector • Example 2 code!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{ color: cyan; } div { color: yellow; } </style> </head> <body> <div> <p class='box' id="wrap"> Guess what color I am</p> </div> </body> </html> • Execution results •Summary: Inherited elements have no weight value, so the final result is the style set by the universal selector • Example 3 code<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> p{ color: yellow; } *{ color: cyan; } </style> </head> <body> <div> <p class='box' id="wrap"> Guess what color I am</p> </div> </body> </html> • Execution results • Summary: The weight of the tag selector is 1, but it is still greater than the universal selector, so the final result is the style set by the tag selector • Example 4 code<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> div p{ color: yellow; } div>p{ color: cyan; } p{ color: red; } </style> </head> <body> <div> <p class='box' id="wrap"> Guess what color I am</p> </div> </body> </html> • Execution results • Example 5 Code<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> div>p{ color: cyan; } div p{ color: yellow; } p{ color: red; } </style> </head> <body> <div> <p class='box' id="wrap"> Guess what color I am</p> </div> </body> </html> • Execution results Calculation of weight value <!--There is a structure like this: --> <div class='wrap1' id='box1'> <div class="wrap2" id="box2"> <p class='active'>MJJ</p> </div> </div> <!--Give a few examples to see their weight values: --> p{color:gray;} <!--The weight is 1--> div div p{color:yellow;} <!--The weight is 1+1+1=3--> .active{color:red;} <!--Weight 10--> div .active{color:black;} <!--The weight is 11--> div div .active{color:blue;} <!--The weight is 12--> .wrap #box2 .active{color:purple;} <!--Weight is 120--> #box1 #box2 .active{color:green;} <!--Weight is 210--> <!--From this we can see that, in fact, for the calculation of weight value, first of all, it is not carried forward. For the selectors used, we are just counting, counting the number of selectors (in order, first id, then class, then element), for example: --> #box1 .wrap2 div{ color:red; } <!--The weight is 1 1 1--> Note: Inherited attributes also have weight values, but their weight values are very low, which can be understood as the lowest inherited weight value. !important Example of increasing weight value<!--When we are doing web page code, there are some special cases where we need to set the highest weight value for certain styles. What should we do? For example, if we know that the weight value of inline style is 1000, which is relatively large, then we can use !important to solve it. --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Use of !important</title> <style type="text/css"> div{ color:green !important; } </style> </head> <body> <div id="box" style="color:red;"> <span>MJJ</span> </div> </body> </html> • Execution results •Summary: Using !important is a bad habit and should be avoided as much as possible, because it seriously breaks the inherent weight comparison rules in style sheets, making it more difficult to debug bugs. When two conflicting rules with !important apply to the same tag, the one with the highest priority will be used. When can !important be used? • The first ◦Your website has a CSS file that designs the entire site style • The second How can I change the color of the text to red? In this case, if !important is not applied, the first rule always takes precedence over the second rule. Summarize: Summarize The above is a detailed explanation of the CSS weight value (cascading) example introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! |
<<: How to configure the pdflatex environment in docker
>>: Web page color matching example analysis: Green color matching web page analysis
Beautiful code is the foundation of a beautiful we...
This method uses the drop-shadow filter in CSS3 t...
The outermost boxF rotates 120 degrees, the secon...
1. Brief Introduction Vue.js allows you to define...
Previously, we knew several attributes of backgro...
Mapping the mouse position or implementing drag e...
Table of contents 1. Understand the basics 2. Con...
Table of contents Preface Using websocket Constru...
Index merging is an intelligent algorithm provide...
This article shares the specific code of JS+AJAX ...
This article example shares the specific code of ...
Subquery Classification Classification by returne...
1. What is Docker? (1) Docker is an open source t...
The effect is as follows: Example 1 Example 2: Ta...
Preface It is said that if the people doing opera...