CSS Selector Priority


Browsers determine which attribute values ​​are most relevant to an element by judging the CSS priority, and then apply it to the element. The reasonable composition rules of CSS selectors determine the priority. We often use selector priority to reasonably control elements to achieve our ideal display state. Let's take a closer look at CSS selector priority and weight.

How are CSS selectors calculated?

CSS Precedence Order

Here is a list of selectors in increasing order of precedence:

Multiple Styles

Multiple styles are when external, internal, and inline styles are applied to the same element at the same time.

Generally, the priority is: (External style) External style sheet < (Internal style) Internal style sheet < (Inline style) Inline style

There is an exception, that is, if the external style is placed after the internal style, the external style will cover the internal style.

The following is an example:

  1. <head>< /li>
  2. <style type="text/css"> ;
  3. h3{color:green;}
  4. </style>
  5. < span class="pln"> <!-- External style style.css -->
  6. < span class="pln"> <link rel="stylesheet"type= "text/css"href="style.css"/>
  7. < ;!-- Settings: h3{color:blue} -->
  8. </head>< /li>
  9. <body>
  10. <h3>welcome sojson.com</h3>
  11. < li class="L0"></body>

Selector precedence explained

  1. Inline style sheets have a priority of up to 1000.

  2. ID selectors have a priority of 100.

  3. Class class selectors have a priority of 10.

  4. HTML tag (type) selectors have a priority of 1.

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. #redP p {
  5. color:#F00;
  6. }
  7. #redP .red em {
  8. color:#00F;
  9. }
  10. #redP p span em {
  11. color:#FF0;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <divid="redP">
  17. <pclass="red">red
  18. <span><em>em red</em></span>
  19. </p>
  20. <p>red</p>
  21. </div>
  22. </body>
  23. </html>

CSS precedence rules:

  1. Selectors have a weight, the larger the weight, the higher the priority.

  2. When the weights are equal, the style sheet settings that appear later will take precedence over the style sheet settings that appear earlier.

  3. Author's rules take precedence over browser's: that is, the CSS styles set by the web page author have a higher priority than the styles set by the browser.

  4. Inherited CSS styles are inferior to CSS styles specified later

  5. Rules marked with "!important" have the highest priority in the same set of property settings.

CSS !important rule example:
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. #redP p{
  5. color:#00f !important;
  6. color:#f00;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <divid="redP">
  12. <p>color</p>
  13. <p>color</p>
  14. </div>
  15. </body>
  16. </html>

Use scripts to add styles:

When you use a JavaScript script to insert an internal style after connecting to an external style (i.e. the internal style is created using a script), the IE browser behaves differently. The code is as follows:

  1.  
  2. <html>
  3. <head>
  4. <title> demo </title>
  5. <metaname="Author"< /span>content="xugang" />
  6. <!-- Add external CSS styles-->
  7. <linkrel="stylesheet"href ="styles.css"type= "text/css" />
  8. <!-- In the external styles.css file, the code is as follows:
  9. h3 {color:blue}
  10. -->
  11. <!-- Use javascript to create internal CSS Styles -->
  12. <script type="text/javascript">
  13. <!--
  14. (function(){
  15. var agent = window.navigator.userAgent.toLowerCase();
  16. var is_op = (agent.indexOf("opera") != -1);
  17. var is_ie = (agent.indexOf("msie") != -1) && document.all && !is_op;
  18. var is_ch = (agent.indexOf("chrome") != -1);
  19.  
  20. var cssStr="h3 {color:green;}";
  21. var s=document.createElement("style");
  22. var head=document.getElementsByTagName("head").item(0);
  23. var link=document.getElementsByTagName("link");
  24. link=link.item(0);
  25.  
  26. if(is_ie)
  27. {
  28. if(link)
  29. head.insertBefore(s,link);
  30. else
  31. head.appendChild(s);
  32. document.styleSheets.item(document.styleSheets.length-1).cssText=cssStr;
  33. }
  34. else if(is_ch)
  35. {
  36. var t=document.createTextNode();
  37. t.nodeValue=cssStr;
  38. s.appendChild(t);
  39. head.insertBefore(s,link);
  40. }
  41. else
  42. {
  43. s.innerHTML=cssStr;
  44. head.insertBefore(s,link);
  45. }
  46. })();
  47. //-->
  48. </script>
  49. </head>
  50. <body>
  51. <h3>在IE中我是綠色,非IE瀏覽器下我是藍色!</h3>
  52. </body>
  53. </html>

The order of CSS rendering in IE browser may be as follows

  1. IE downloads from top to bottom;

  2. The execution of JavaScript functions will block the download of IE;

  3. IE also renders from top to bottom;

  4. IE downloads and renders at the same time;

  5. When rendering to a certain part of the page, all the parts above it have been downloaded (but it does not mean that all related elements have been downloaded.)

  6. During the download process, if a tag is embedded in a file, and the file is semantically interpreted (for example: JS script, CSS style), then IE's download process will enable a separate connection for downloading. And after downloading, it will be parsed. If there is a redefinition in JS or CSS, the function defined later will overwrite the function defined earlier.

  7. During the parsing process, stop downloading all the elements below the page. The style sheet file is special. After it is downloaded, it will be parsed together with all the previously downloaded style sheets. After the parsing is completed, all the previous elements (including those that have been rendered before) will be re-rendered. And the rendering will continue in this way until the entire page is rendered.

  8. Firefox handles downloading and rendering in much the same order, with some minor differences, such as the rendering of iframes.