Blogger Information
Blog 17
fans 0
comment 0
visits 14774
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS样式表优先级及模块化原理及实现
未来星
Original
929 people have browsed it

>CSS样式表优先级原则

优先级相同时,书写顺序决定优先级
选择器本身优先级大于书写顺序
样式声明优先级: id > class > tag

  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>Document</title>
  8. <!-- 内部样式,仅作用于该文档 -->
  9. <style>
  10. /* 样式声明优先级: id > class > tag */
  11. /* 1,1,0 */
  12. #str.abc {
  13. color: violet;
  14. }
  15. /* 1,0,0 */
  16. #str {
  17. color: gold;
  18. }
  19. /* 0,1,0 */
  20. .act {
  21. color: tomato;
  22. }
  23. /* 选择器本身优先级大于书写顺序 */
  24. /* 类样式 */
  25. /* 0,0,2 */
  26. body h1 {
  27. color: green;
  28. }
  29. /* 优先级相同时,书写顺序决定优先级 */
  30. /* 0,0,1 */
  31. h1 {
  32. color: red;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <h1>HELLO WORLD</h1>
  38. <!-- 行内样式的优先级要高于style标签设置的内部样式 -->
  39. <h1 style="color: blue">HELLO WORLD</h1>
  40. <h1 class="act">HELLO WORLD</h1>
  41. <h1 class="act" id="str">HELLO WORLD</h1>
  42. <h1 class="abc" id="str">HELLO WORLD</h1>
  43. </body>
  44. </html>

>样式表模块化实例

网站大量网页所共用的CSS样式表,推荐采用模块化书写方式,统一单独文件编写存储,在需要的网页文件中链接引用。

  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. <link rel="stylesheet" href="css/index.css" />
  9. </head>
  10. <body>
  11. <header>页头</header>
  12. <main>主体</main>
  13. <footer>页脚</footer>
  14. </body>
  15. </html>

css/index.css文件:

  1. header {
  2. background-color: lightblue;
  3. height: 5em;
  4. color: #fff;
  5. }
  6. footer {
  7. background-color: lightblue;
  8. height: 5em;
  9. color: red;
  10. }
  11. main {
  12. background-color: orange;
  13. height: 10em;
  14. }

>伪类选择器的使用方法

  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. <style>
  9. .idx li {
  10. background-color: lightgreen;
  11. }
  12. /* 结构伪类: 选择子元素, 要有一个父元素做为查询起点 */
  13. /* .idx > :nth-child(3) {
  14. background-color: violet;
  15. } */
  16. /* 匹配任何位置的元素
  17. n = (0,1,2,3,4....) */
  18. /* .idx > li:nth-child(0n + 3) {
  19. background-color: violet;
  20. } */
  21. /* 分组伪类结构选择器,推荐使用 */
  22. .idx li:first-of-type {
  23. background-color: violet;
  24. }
  25. .idx li:last-of-type {
  26. background-color: yellow;
  27. }
  28. .idx > li:nth-of-type(3) {
  29. background-color: cyan;
  30. }
  31. .idx p:last-of-type {
  32. background-color: blue;
  33. color: white;
  34. }
  35. .idx p:nth-last-of-type(3) {
  36. background-color: brown;
  37. color: white;
  38. }
  39. /* 选择任何一个: :nth-of-type(n)
  40. 选择第一个: :first-of-type
  41. 选择最后一个: :last-of-type
  42. 选择倒数某一个: :nth-last-of-type()
  43. 唯一子元素的元素: :only-of-type */
  44. </style>
  45. </head>
  46. <body>
  47. <ul class="idx">
  48. <li>item1</li>
  49. <li>item2</li>
  50. <li>item3</li>
  51. <li>item4</li>
  52. <li>item5</li>
  53. <p>item6</p>
  54. <p>item7</p>
  55. <p>item8</p>
  56. <li>item9</li>
  57. <li>item10</li>
  58. </ul>
  59. </body>
  60. </html>
Correcting teacher:天蓬老师天蓬老师

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