1、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>选择器优先级</title>
  8. </head>
  9. <style>
  10. #box {
  11. font-size: large;
  12. } /* 增加一级选择器,优先级高于没有的后面的覆盖前面的样式 ,将忽略书写顺序*/
  13. #box .box1 .box2 {
  14. color: crimson;
  15. }
  16. #box .box1 {
  17. color: chartreuse;
  18. }
  19. .box1 {
  20. color: blue;
  21. }
  22. /* 同一级的class选择器后面的覆盖前面的样式 */
  23. .box1 {
  24. color: blueviolet;
  25. }
  26. </style>
  27. <body>
  28. <dic id="box">
  29. <!-- 行内样式 -->
  30. <div style="color: aqua">选择器的优先级</div>
  31. <!-- class选择器 -->
  32. <div class="box1">
  33. 选择器的优先级
  34. <div class="box2">选择器的优先级</div>
  35. </div>
  36. </dic>
  37. </body>
  38. </html>

选择器的归纳:

  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. </head>
  9. <style>
  10. #box {
  11. font-size: large;
  12. } /* 增加一级选择器,优先级高于没有的后面的覆盖前面的样式 ,将忽略书写顺序*/
  13. #box .box1 .box2 {
  14. color: crimson;
  15. }
  16. #box .box1 {
  17. color: chartreuse;
  18. }
  19. .box1 {
  20. color: blue;
  21. }
  22. /* 同一级的class选择器后面的覆盖前面的样式 */
  23. .box1 {
  24. color: blueviolet;
  25. }
  26. table{
  27. border-collapse: collapse;
  28. widtH:60vw;
  29. }
  30. th,td{
  31. border: 2px solid blue;
  32. }
  33. </style>
  34. <body>
  35. <dic id="box">
  36. <!-- 行内样式 -->
  37. <div style="color: aqua">选择器的优先级</div>
  38. <!-- class选择器 -->
  39. <div class="box1">
  40. 选择器的优先级
  41. <div class="box2">选择器的优先级</div>
  42. </div>
  43. </dic>
  44. <table style="border:1px solid blue ;">
  45. <tr >
  46. <th>选择器名</th>
  47. <th>表达式</th>
  48. <th>定义</th>
  49. <th>优先级</th>
  50. </tr>
  51. <tr>
  52. <td>标签名选择器</td>
  53. <td>div { color:Red;}</td>
  54. <td>即页面中的各个标签名的css样式</td>
  55. <td>优先 第一级</td>
  56. </tr>
  57. <tr>
  58. <td>类选择器</td>
  59. <td>.divClass {color:Red;}</td>
  60. <td>/即定义的每个标签的class 中的css样式</td>
  61. <td>优先第二级</td>
  62. </tr>
  63. <tr>
  64. <td>ID选择器 </td>
  65. <td> #myDiv {color:Red;} </td>
  66. <td>即页面中的标签的id</td>
  67. <td>优先第三级</td>
  68. </tr>
  69. <tr>
  70. <td>后代选择器(类选择器的后代选择器)</td>
  71. <td>divClass span { color:Red;} </td>
  72. <td>即多个选择器以逗号的格式分隔 命名找到准确的标签</td>
  73. <td>优先第四级</td>
  74. </tr>
  75. <tr>
  76. <td>群组选择器</td>
  77. <td>div,span,img {color:Red}</td>
  78. <td>即具有相同样式的标签分组显示</td>
  79. <td>优先第五级</td>
  80. </tr>
  81. <tr>
  82. <td colspan="4">选择器的优先级:
  83. </br>
  84. 1.最高优先级是 (直接在标签中的设置样式,假设级别为1000)&lt div style="color:Red;"&gt &lt /div&gt;
  85. 2.次优先级是(ID选择器 ,假设级别为100) #myDiv{color:Red;}</td>
  86. <!-- <td></td>
  87. <td></td>
  88. <td>优先第五级</td> -->
  89. </tr>
  90. </table>
  91. </body>
  92. </html>
  93. ```![](https://img.php.cn/upload/image/227/893/639/1616671045590969.png)
  94. |选择器名|表达式|定义|优先级|
  95. |---|---|---|---|
  96. |权重 !important|div { color:Red; color:lime !important;}|一般调试用|级别最高级|
  97. |标签名选择器|div { color:Red;}|即页面中的各个标签名的css样式|优先 第一级|
  98. |类选择器|divClass {color:Red;}|即定义的每个标签的class 中的css样式|优先第二级|
  99. |ID选择器|#myDiv {color:Red;}|即页面中的标签的id|优先第三级|
  100. |后代选择器(类选择器的后代选择器)|divClass span { color:Red;}|即多个选择器以逗号的格式分隔 命名找到准确的标签|优先第四级|
  101. |群组选择器|div,span,img {color:Red}|即具有相同样式的标签分组显示|优先第五级|
  102. ###选择器的优先级:
  103. 1.最高优先级是 (直接在标签中的设置样式,假设级别为1000)<div style="color:Red;"> </div>;
  104. 2.次优先级是(ID选择器 ,假设级别为100) #myDiv{color:Red;}</td>;

2. 实例演示前端组件样式模块化的原理与实现;

html页面

  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/public.css" />
  9. </head>
  10. <body>
  11. <header class="head">页眉</header>
  12. <main class="subject">主体</main>
  13. <footer class="foot">页脚</footer>
  14. </body>
  15. </html>

css组件

1、public(公共)组件

  1. @import url(header.css);
  2. @import url(main.css);
  3. @import url(footer.css);
  4. * {
  5. margin: 0;
  6. padding: 0;
  7. box-sizing: border-box;
  8. }
  9. a {
  10. text-decoration: none;
  11. color: #000;
  12. }

2、header(头部)组件

  1. head {
  2. height: 4rem;
  3. background-color: #eee;
  4. }

3、 main(主体)组件

  1. .subject {
  2. height: 15rem;
  3. background-color: antiquewhite;
  4. }
  1. .foot {
  2. height: 5rem;
  3. background-color: #888;
  4. }

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. <title>伪类选择器</title>
  8. <!-- <link rel="stylesheet" href="/css/public.css" /> -->
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. a {
  16. text-decoration: none;
  17. color: rgb(74, 240, 124);
  18. }
  19. a:hover {
  20. color: #c81623;
  21. }
  22. ul,
  23. li {
  24. list-style: none;
  25. }
  26. /* 根据元素的位置或坐标选择 */
  27. ul li {
  28. background-color: beige;
  29. }
  30. /* 下面的LI全部选种 */
  31. /* ul > li {
  32. background-color: beige;
  33. } */
  34. /* 只选种子元素,不选种孙子元素 */
  35. /* body > ul > li {
  36. background-color: blue;
  37. } */
  38. /* 同级相邻选择器 + */
  39. /* .last + li {
  40. background-color: blueviolet;
  41. } */
  42. /* 同级相邻所有以下元素用 ~ */
  43. /* .last ~ li {
  44. background-color: blueviolet;
  45. } */
  46. /* 伪类选择器 */
  47. /* 结构伪类选择子元素的 */
  48. /* (0n + 3) 0为1时为单数,2为偶籹;当前面为0时3就是所要选择的指定位置*/
  49. /* .list > :nth-of-type(0n + 3) {
  50. background-color: crimson;
  51. } */
  52. /* 选择第1个方法 */
  53. .list > :nth-of-type(1) {
  54. background-color: crimson;
  55. }
  56. .list>:first-of-type: {
  57. background-color: darkblue;
  58. }
  59. /* 最后一个 */
  60. .lisr > :nth-of-type(5) {
  61. background-color: darkcyan;
  62. }
  63. .lisr > :last-of-type {
  64. background-color: darkgoldenrod;
  65. }
  66. /* 倒数每几个 */
  67. .list > li:nth-last-of-type(3) {
  68. background-color: antiquewhite;
  69. }
  70. /* 最后一个 */
  71. .lisr > p:nth-of-type(4) {
  72. background-color: darkcyan;
  73. }
  74. .lisr > p:last-of-type {
  75. background-color: darkgoldenrod;
  76. }
  77. /* 倒数第几个 */
  78. .list > p:nth-last-of-type(3) {
  79. background-color: darkseagreen;
  80. }
  81. /* 只有一个子元素的 */
  82. ul li:only-of-type {
  83. background-color: rgb(231, 231, 13);
  84. }
  85. </style>
  86. </head>
  87. <body>
  88. <ul class="list">
  89. <li>导航1<a href="">&nbsp页眉</a></li>
  90. <li>导航2<a href="">&nbsp左侧边栏</a></li>
  91. <li class="last">导航3<a href="">&nbsp主体</a></li>
  92. <li>
  93. <!-- <ul>
  94. <li>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp页面1</li>
  95. <li>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp页面2</li>
  96. <li>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp页面3</li>
  97. <li>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp页面4</li>
  98. <li>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp页面5</li>
  99. </ul> -->
  100. 导航4<a href="">&nbsp右侧边栏</a>
  101. </li>
  102. <p>内容1</p>
  103. <p>内容2</p>
  104. <p>内容3</p>
  105. <p>内容4</p>
  106. <li>导航5<a href="">&nbsp页脚</a></li>
  107. </ul>
  108. <ul>
  109. <li>只有一个的</li>
  110. </ul>
  111. </body>
  112. </html>

更多相关文章

  1. CSS样式表优先级及模块化原理及实现
  2. CSS:选择器优先级、前端组件样式模块化原理实现、常用伪类选择器
  3. css选择器优先级、伪类选择器的使用和模块化样式
  4. 代码中的Thread.sleep(0) 有什么意义?是写错了吗?
  5. 210322 CSS 样式与选择器
  6. 【CSS入门】学习CSS盒模型及常用样式属性详解(整理)
  7. 10 个最佳实践来改良你的 CSS
  8. CSS学习(一)
  9. CSS选择器讲解

随机推荐

  1. 小米随身WiFi,Linux下AP热点驱动(开源)
  2. 走近Linux世界:Linux与Windows特性比较
  3. linux系统时间和硬件时钟问题(date和hwcl
  4. Linux 下串口编程之三 termios结构体介绍
  5. Linux上配置使用iSCSI详细说明
  6. pc与arm板间的文件传送
  7. SUSE LINUX虚拟机设置静态IP
  8. tty、串口、console等的区别
  9. linux 文件系统的管理 (硬盘) 工作原理
  10. ASP.NET vs. PHP,哪个更快?