Blogger Information
Blog 49
fans 0
comment 3
visits 22996
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
自定义样式的来源与优先级及选择器与权重分析示例
P粉479712293
Original
328 people have browsed it

题目一:自定义样式的来源与优先级

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>自定义样式来源与优先级</title>
  8. <!-- *第一种外部导入方式 -->
  9. <!--* <link rel="stylesheet" href="../static/style.css"> -->
  10. <!-- *第二种外部导入方式 -->
  11. <style>
  12. @import url("../static/style.css");
  13. </style>
  14. </head>
  15. <body>
  16. <!-- *1.默认样式继承自html -->
  17. <h1>晚上好</h1>
  18. <!-- *2.自定义的行内样式 覆盖默认样式-->
  19. <h2 style="color: red;">晚上好</h2>
  20. <!-- *3.自定义的行内样式,而在自定义的样式中,如有同名属性中则后面的会覆盖前面的-->
  21. <h2 style="color: red; color: green;">晚上好</h2>
  22. <!-- *4.自定义的文档样式/内部样式 -->
  23. <h3>晚上好</h3>
  24. <style>
  25. /* 分两步
  26. 1.找到它:选择器
  27. 2.设置它;样式声明 */
  28. h3{
  29. color: blue;
  30. }
  31. h3{
  32. color: green;
  33. }
  34. </style>
  35. <!-- *5.有两种导入方式,即<link...>与@import ... -->
  36. <h4>晚上好</h4>
  37. </body>
  38. </html>

外部样式文件:style.css

  1. h4 {
  2. color: green;
  3. }

效果图:

题目二:选择器与权重分析示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="stylesheet" href="../static/4-3.css"
  8. <title>选择器与权重</title>
  9. </head>
  10. <body>
  11. <div><h1 class="title" id="a1">Hello</h1></div>
  12. </body>
  13. </html>

对应的css文件为:

  1. /* *h1.title权重(0,1,1) */
  2. h1.title{
  3. color: red;
  4. }
  5. /* *div h1#a1.title权重(1,1,2) */
  6. div>h1#a1.title{
  7. color: green;
  8. }
  9. /* *忽略权重为最大 */
  10. h1{
  11. color:rosybrown !important;
  12. }
  13. /* *h1权重(0,0,1) */
  14. h1{
  15. color:chartreuse;
  16. }
  17. /* *h1#a1.title权重(1,1,1) */
  18. h1#a1.title{
  19. color: blue;
  20. }

效果图:

1.首先显示的是!important,因它忽略权重为最大:

2.然后删去上面最大的权重,依次显示的分别为:



Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:对比一目了然
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post