Detailed explanation of CSS weight value (cascading) examples

Detailed explanation of CSS weight value (cascading) examples

•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
◦ At the same time, you or your friends wrote some poor inline styles

• The second

box p { color: blue; } p.awesome { color: red; }

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!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

<<:  How to configure the pdflatex environment in docker

>>:  Web page color matching example analysis: Green color matching web page analysis

Recommend

Application of anchor points in HTML

Set Anchor Point <a name="top"><...

Vue+Element realizes paging effect

This article example shares the specific code of ...

WeChat applet implements the snake game

This article shares the specific code of the WeCh...

Nodejs plug-in and usage summary

The operating environment of this tutorial: Windo...

HTML is the central foundation for the development of WEB standards

HTML-centric front-end development is almost what ...

How to install MySQL under Linux (yum and source code compilation)

Here are two ways to install MySQL under Linux: y...

In-depth explanation of Vue multi-select list component

A Multi-Select is a UI element that lists all opt...

The role of MySQL 8's new feature window functions

New features in MySQL 8.0 include: Full out-of-th...

Detailed tutorial on installing mysql 8.0.20 on CentOS7.8

1. Install MySQL software Download and install My...

Navicat Premium operates MySQL database (executes sql statements)

1. Introduction to Navicat 1. What is Navicat? Na...

Solution to MySQL server login error ERROR 1820 (HY000)

Fault site: Log in to the MySQL server and get th...