Blogger Information
Blog 20
fans 0
comment 0
visits 11509
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
选择器的优先级,id,class,tag。前端组件样式模块化的原理与实现。常用伪类选择器的使用方式及伪类来模块化元素组件(表单、表格)。
愿情的博客
Original
644 people have browsed it

一.选择器的优先级,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. <style>
  9. /* 1,1,3 */
  10. html body h1#first.active {
  11. color: palevioletred;
  12. }
  13. /* id选择器优先级大于class */
  14. /* 1,0,3 */
  15. html body h1#first {
  16. color: lightblue;
  17. }
  18. /* 1,0,2 */
  19. body h1#first {
  20. color: beige;
  21. }
  22. /* 选择器本身优先级大于书写顺序 */
  23. /* 样式类 */
  24. /* 0,1,2 */
  25. body h1.active {
  26. color: blue;
  27. }
  28. /* 0,1,1 */
  29. h1.active {
  30. color: yellow;
  31. }
  32. /* 标签选 */
  33. /* 0,0,3 */
  34. html body h1 {
  35. color: green;
  36. }
  37. /* 优先级相同时,书写顺序决定优先级 */
  38. /* 0,0,3 */
  39. html body h1 {
  40. color: red;
  41. }
  42. /* 样式类
  43. .active {
  44. color: blue;
  45. }*/
  46. /* #first {
  47. color: lightblue;
  48. }*/
  49. </style>
  50. </head>
  51. <body>
  52. <h1 class="active" id="first">Hello Woreld</h1>
  53. </body>
  54. </html>
  55. <!-- 公式:id >class >tag -->

二.前端组件样式模块化的原理与实现

mokuai

  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. <!-- header { 1em=16px,3em=48px min-height: 3em; background-color: #ddd; }
  10. main { 1em=16px,3em=48px min-height: 20em; background-color: lightcyan; }
  11. footer { 1em=16px,3em=48px min-height: 3em; background-color: #999; }
  12. @import url(css/header.css); @import url(css/mian.css); @import
  13. url(css/footer.css);
  14. <style>
  15. @import url(css/index.css);
  16. </style> -->
  17. </head>
  18. <body>
  19. <header>页眉</header>
  20. <main>主体</main>
  21. <footer>页脚</footer>
  22. </body>
  23. </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. <style>
  9. /* 结构伪类:选择子元素,只要有一个父元素作为查询起点 */
  10. /* .list > :nth-child(3) {
  11. background-color: violet;
  12. } */
  13. /* 匹配任何位置元素
  14. n = (0,1,2,3,4.....) */
  15. /* .list > li:nth-child(0n + 3) {
  16. background-color: violet;
  17. } */
  18. /* 分组伪类结构选择器,推荐使用 */
  19. .list > li:nth-of-type(3) {
  20. background-color: cyan;
  21. }
  22. /* 选择中第一个p /li */
  23. .list > p:nth-of-type(1) {
  24. background-color: lightgreen;
  25. }
  26. .list > li:nth-of-type(1) {
  27. background-color: lightgreen;
  28. }
  29. .list > li:nth-of-type(7) {
  30. background-color: green;
  31. }
  32. /* 最后一个p */
  33. .list > p:nth-of-type(3) {
  34. background-color: red;
  35. }
  36. .list p:last-of-type {
  37. background-color: blue;
  38. }
  39. .list p:first-of-type {
  40. background-color: red;
  41. }
  42. /* 选择倒数第三个 */
  43. .list > li:nth-last-of-type(3) {
  44. background-color: yellow;
  45. }
  46. ul > li:only-of-type {
  47. background-color: turquoise;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <ul class="list">
  53. <li>item1</li>
  54. <li>item2</li>
  55. <li>item3</li>
  56. <li>item4</li>
  57. <li>item5</li>
  58. <li>item6</li>
  59. <p>item7</p>
  60. <p>item8</p>
  61. <li>item9</li>
  62. <p>item10</p>
  63. </ul>
  64. <ul>
  65. <li>item</li>
  66. </ul>
  67. </body>
  68. <!-- 选择任何一个:nth-of-type(n) 选择第一个:first-of-type
  69. 选择最后一个:last-of-type 选择倒数某一个:nth-last-of-type(n)
  70. 选择唯一子元素得元素:only-of-type-->
  71. </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>组件化编程思想t</title>
  8. <style>
  9. /* 只有获取到页面中得某个元素组件得入口,
  10. 再根据子元素得位置,使用伪类就可以选择任何一个元素了 */
  11. /* 选择首页 */
  12. /* .index {
  13. background-color: yellow;
  14. } */
  15. /* menu是入口 */
  16. /* .menu :first-of-type {
  17. background-color: tomato;
  18. }
  19. .menu :last-of-type {
  20. background-color: turquoise;
  21. }
  22. .menu :nth-last-of-type(2) {
  23. background-color: yellow;
  24. }
  25. .menu :nth-last-of-type(3) {
  26. background-color: turquoise;
  27. } */
  28. @import url(css/menu.css);
  29. @import url(css/login.css);
  30. /* 只要获取表单得入口,使用伪类可以获取表单中任何一个控件 */
  31. /* 获取到提交按钮 */
  32. /* .login button {
  33. background-color: turquoise;
  34. color: black;
  35. } */
  36. /* .login :only-of-type {
  37. background-color: green;
  38. color: black;
  39. }
  40. .login :first-of-type {
  41. background-color: burlywood;
  42. color: black;
  43. }
  44. .login :nth-last-of-type(2) {
  45. background-color: blanchedalmond;
  46. color: black;
  47. }
  48. .login :nth-of-type(3) {
  49. background-color: red;
  50. color: black;
  51. } */
  52. </style>
  53. </head>
  54. <body>
  55. <nav class="menu">
  56. <a href="">首页</a>
  57. <a href="">视频</a>
  58. <a href="">下载</a>
  59. <a href="">注册/登录</a>
  60. </nav>
  61. <hr />
  62. <form action="" style="display: grid; gap: 1rem" class="login">
  63. <input type="text" placeholder="username" />
  64. <input type="password" placeholder="password" />
  65. <input type="email" placeholder="demo@email" />
  66. <button>提交</button>
  67. </form>
  68. </body>
  69. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!