博主信息
博文 17
粉丝 0
评论 0
访问量 19710
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS样式表优先级及模块化原理及实现
未来星
原创
1183人浏览过

>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>
批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学