Blogger Information
Blog 14
fans 0
comment 2
visits 12670
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS 简单选择器、上下文选择器、结构伪类选择器等使用总结
JKY辉哥
Original
506 people have browsed it

1.简单选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>选择器:简单选择器</title>
  7. <style>
  8. /* 使用九宫格来演示: grid布局实现 */
  9. .container {
  10. width: 300px;
  11. height: 300px;
  12. display: grid;
  13. grid-template-columns: repeat(3, 1fr);
  14. gap: 5px;
  15. }
  16. .item {
  17. font-size: 2rem;
  18. background-color: lightskyblue;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. /* 简单选择器 */
  24. /* 元素选择器:标签选择器 */
  25. body {
  26. background-color: lightseagreen;
  27. }
  28. /* 类选择器: 对应着html标签中的class属性 */
  29. .item {
  30. border: 1px solid #000;
  31. }
  32. /* 多个类复合应用 */
  33. .item.center {
  34. background-color: lightgreen;
  35. }
  36. /* id选择器 */
  37. .item#first {
  38. background-color: lightpink;
  39. }
  40. /* 层叠样式表, 相同元素,后面追加的样式会覆盖前面的样式 */
  41. *#first {
  42. background-color: yellow;
  43. }
  44. #first.item {
  45. background-color: tomato;
  46. }
  47. /* * :属于元素级别
  48. 元素 < class < id */
  49. /* id,class可以添加到任何元素上,所以可以省略 */
  50. /* id 选择器的应用场景目前只有二种场景: 表单元素, 锚点 */
  51. </style>
  52. </head>
  53. <body>
  54. <!-- .container>.item{$}*9 快速生成-->
  55. <div class="container">
  56. <div class="item" id="first">1</div>
  57. <div class="item">2</div>
  58. <div class="item">3</div>
  59. <div class="item center">4</div>
  60. <div class="item">5</div>
  61. <div class="item">6</div>
  62. <div class="item">7</div>
  63. <div class="item">8</div>
  64. <div class="item">9</div>
  65. </div>
  66. </body>
  67. </html>

运行结果:
简单选择器

2.上下文选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>选择器: 上下文选择器</title>
  7. <style>
  8. /* 使用九宫格来演示: grid布局实现 */
  9. .container {
  10. width: 300px;
  11. height: 300px;
  12. display: grid;
  13. grid-template-columns: repeat(3, 1fr);
  14. gap: 5px;
  15. }
  16. .item {
  17. font-size: 2rem;
  18. background-color: lightskyblue;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. /* 1.后代选择器 */
  24. .container div {
  25. border: 1px solid #000;
  26. }
  27. /* 2. 父子选择器 */
  28. body > div {
  29. border: 5px solid coral;
  30. }
  31. /* 3.同级相邻选择器 */
  32. .item.center + .item {
  33. background-color: lightgreen;
  34. }
  35. /* 4.同级所有选择器 */
  36. .item.center ~ .item {
  37. background-color: lightsalmon;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div class="container">
  43. <div class="item">1</div>
  44. <div class="item">2</div>
  45. <div class="item">3</div>
  46. <div class="item">4</div>
  47. <div class="item center">5</div>
  48. <div class="item">6</div>
  49. <div class="item">7</div>
  50. <div class="item">8</div>
  51. <div class="item">9</div>
  52. </div>
  53. </body>
  54. </html>

3.结构伪类选择器 不分组(不区分元素类型)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>结构伪类选择器: 不分组(不区分元素类型)</title>
  7. <style>
  8. /* 使用九宫格来演示: grid布局实现 */
  9. .container {
  10. width: 300px;
  11. height: 300px;
  12. display: grid;
  13. grid-template-columns: repeat(3, 1fr);
  14. gap: 5px;
  15. }
  16. .item {
  17. font-size: 2rem;
  18. background-color: lightskyblue;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. /* 匹配第一个子元素 */
  24. .container > *:first-child {
  25. /* background-color: wheat; */
  26. }
  27. .container > .item:first-child {
  28. /* background-color: blue; */
  29. }
  30. /* 匹配最后一个子元素 */
  31. .container > :last-child {
  32. /* background-color: lightpink; */
  33. }
  34. /* 选第3个:索引是从1开始 */
  35. .container > :nth-child(3) {
  36. /* background-color: lightseagreen; */
  37. }
  38. /* 只选择偶数单元格 */
  39. .container > :nth-child(2n) {
  40. /* background-color: lightyellow; */
  41. }
  42. /* 偶数用even表示 */
  43. .container > :nth-child(even) {
  44. background-color: lightyellow;
  45. }
  46. /* 只选择奇数单元格 */
  47. .container > :nth-child(2n-1) {
  48. /* background-color: lightsalmon; */
  49. }
  50. /* 奇数: odd */
  51. .container > :nth-child(odd) {
  52. /* background-color: lightsalmon; */
  53. }
  54. /* 从指定位置(从第4个开始),选择剩下的所有元素 */
  55. .container > .item:nth-child(n + 4) {
  56. background-color: lightseagreen;
  57. }
  58. /* 只取前三个 */
  59. .container > .item:nth-child(-n + 3) {
  60. background-color: magenta;
  61. }
  62. /* -0 + 3 = 3
  63. -1 + 3 = 2
  64. -2 + 3 = 1 */
  65. /* 只选最后三个 */
  66. .container > .item:nth-last-child(-n + 3) {
  67. background-color: brown;
  68. }
  69. /* 取第8个,用倒数的方式更直观 */
  70. .container > .item:nth-last-child(2) {
  71. background-color: yellow;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <div class="container">
  77. <div class="item">1</div>
  78. <div class="item">2</div>
  79. <div class="item">3</div>
  80. <div class="item">4</div>
  81. <div class="item">5</div>
  82. <div class="item">6</div>
  83. <div class="item">7</div>
  84. <div class="item">8</div>
  85. <div class="item">9</div>
  86. </div>
  87. </body>
  88. </html>

4.结构伪类选择器: 分组(不区分元素类型)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>结构伪类选择器: 分组(不区分元素类型)</title>
  7. <style>
  8. /* 使用九宫格来演示: grid布局实现 */
  9. .container {
  10. width: 300px;
  11. height: 300px;
  12. display: grid;
  13. grid-template-columns: repeat(3, 1fr);
  14. gap: 5px;
  15. }
  16. .item {
  17. font-size: 2rem;
  18. background-color: lightskyblue;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. /* 分组结构伪类分二步:
  24. 1. 元素按类型进行分组
  25. 2. 在分组中根据索引进行选择 */
  26. /*选择最后一个 */
  27. .container div:last-of-type {
  28. background-color: violet;
  29. }
  30. /* 在分组中匹配任何一个,选择倒数第3个 */
  31. .container div:nth-last-of-type(3) {
  32. background-color: turquoise;
  33. }
  34. /* 前三个 */
  35. .container div:nth-of-type(-n + 3) {
  36. background-color: whitesmoke;
  37. }
  38. /* 最后四个 */
  39. .container div:nth-last-of-type(-n + 4) {
  40. background-color: yellowgreen;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div class="container">
  46. <div class="item">1</div>
  47. <div class="item">2</div>
  48. <div class="item">3</div>
  49. <div class="item">4</div>
  50. <div class="item">5</div>
  51. <div class="item">6</div>
  52. <div class="item">7</div>
  53. <div class="item">8</div>
  54. <div class="item">9</div>
  55. </div>
  56. </body>
  57. </html>

运行结果:
结构伪类选择器

5. 伪类与伪元素

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>伪类与伪元素</title>
  7. <style>
  8. /* :target: 必须id配合,实现锚点操作 */
  9. /* 当前#login-form的表单元素被a的锚点激活的时候执行 */
  10. #login-form {
  11. display: none;
  12. }
  13. /* 当前#login-form的表单元素被a的锚点激活的时候线上 */
  14. #login-form:target {
  15. display: block;
  16. }
  17. /* :focus: 当获取焦点的时候 */
  18. input:focus {
  19. background-color: yellow;
  20. }
  21. /* ::selection: 设置选中文本的前景色与背景色 */
  22. input::selection {
  23. color: white;
  24. background-color: red;
  25. }
  26. /* :not(): 用于选择不满足条件元素 */
  27. .list > :not(:nth-of-type(3)) {
  28. color: red;
  29. }
  30. /* ::before */
  31. .list::before {
  32. content: "购物车";
  33. color: blue;
  34. font-size: 1.5rem;
  35. border-bottom: 1px solid #000;
  36. }
  37. /* ::after */
  38. .list::after {
  39. content: "结算中...";
  40. color: red;
  41. font-size: 1.1rem;
  42. }
  43. /* 伪元素前面是双冒号, 伪类前能是单冒号 */
  44. </style>
  45. </head>
  46. <body>
  47. <a href="#login-form">我要登录:</a>
  48. <!-- <button onclick="location='#login-form'">点击登录</button> -->
  49. <form action="" method="post" id="login-form">
  50. <label for="email">邮箱:</label>
  51. <input type="email" name="email" id="email" />
  52. <label for="password">密码:</label>
  53. <input type="password" name="password" id="password" />
  54. <button>登录</button>
  55. </form>
  56. <hr />
  57. <ul class="list">
  58. <li>item1</li>
  59. <li>item2</li>
  60. <li>item3</li>
  61. <li>item4</li>
  62. </ul>
  63. </body>
  64. </html>

6.总结

  • 简单选择器优先级别:tag < class < id
  • :target: 必须 id 配合,实现锚点操作
  • :not(): 用于选择不满足条件元素
  • ::before 在之前
  • ::after 在之后
  • 伪元素前面是双冒号, 伪类前能是单冒号
  • CSS 选择器用法比较多,有点晕,需要多实践
Correcting teacher:WJWJ

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