Blogger Information
Blog 28
fans 0
comment 0
visits 25408
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
css选择器中各个种类,以及常用的选择器使用方法
,多思曩惜,
Original
1233 people have browsed it

选择器

1.简单选择器

1.1种类

选择器 描述 举例
元素选择器 根据元素标签名称进行匹配 div{..}
群组选择器 同时选择多个不同类型的元素 h1,h2.h3{...}
通配选择器 选择全部元素,不区分类型 *{....}
属性选择器 根据元素属性进行匹配 *[]
类选择器 根据元素clss属性进行匹配 *.active{..}
id选择器 根据元素id属性进行匹配 *#top{...}
  • 元素是使用标签和属性进行描述,所以使用标签和属性来选择元素非常自然和直观
  • 以上6种,其实可分为俩类:元素选择器和属性选择器,其中的只是二者的特例
  • 当class,id选择器不限定被修改的元素类型时,星号“*”可以省略
  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>Document</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. height: 300px;
  11. display: grid;
  12. grid-template-columns: repeat(3, 1fr);
  13. gap: 5px;
  14. }
  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. body {
  25. background-color: lightcyan;
  26. }
  27. /* 多个类选择器 */
  28. .item.center {
  29. background-color: lightgreen;
  30. }
  31. /* id选择器,id在页面上只有唯一一次 */
  32. #first {
  33. background-color: lime;
  34. }
  35. /* 类选择器,class权重大于标签
  36. id的权重大于clss 。标签 《 class 《 id属性*/
  37. /* 属性选择器 */
  38. .item[title="hello"] {
  39. background-color: maroon;
  40. }
  41. .item[title] {
  42. background-color: mediumslateblue;
  43. }
  44. /* 简单群选择器 */
  45. *.first,
  46. .item[title="hello"],
  47. .item.center {
  48. background-color: midnightblue;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <div class="container">
  54. <div class="item" id="first">1</div>
  55. <div class="item" title="php">2</div>
  56. <div class="item">3</div>
  57. <div class="item center">4</div>
  58. <div class="item">5</div>
  59. <div class="item" title="hello">6</div>
  60. <div class="item">7</div>
  61. <div class="item">8</div>
  62. <div class="item">9</div>
  63. </div>
  64. </body>
  65. </html>
  • 预览效果

2.上下文选择器

  • html 文档,看上去就像一颗倒置的”树”,所以是有层级结构的
  • 每一个元素, 在文档中, 都有自己的位置,即上下文关系
  • 所以, 完全可以根据元素的上下文关系,来获取到它们

2.1一个元素的四种角色

角色 描述
祖先元素 拥有字元素,孙元素等所有层级的后代元素
父级元素 拥有字元素层级的元素
后代元素 与其它层级元素一起拥有共同共同祖先元素
子元素 与其它同级元素一起拥有共同腹父级元素
  1. <!-- 祖先元素 -->
  2. <div>
  3. <!-- 父级元素 -->
  4. <div>
  5. <!-- 后代元素 -->
  6. <p>
  7. <!-- 子元素 -->
  8. <span></span>
  9. </p>
  10. </div>
  11. </div>

2.2四种上下文选择器

选择器 操作符 描述 举例
后代选择器 空格 选择当前元素的所有后代元素 div p,body *
父子选择器 > 选择当前元素的所有字元素 div>h2
同级相邻选择器 + 选择拥有共同父级且相邻的元素 li.red+li
同级所有选择器 ~ 选择拥有共同父级的后续所有元素 li.red~li
  1. <style>
  2. .container {
  3. width: 300px;
  4. height: 300px;
  5. display: grid;
  6. grid-template-columns: repeat(3, 1fr);
  7. gap: 5px;
  8. }
  9. /* 类选择器 */
  10. .item {
  11. font-size: 2rem;
  12. background-color: lightskyblue;
  13. display: flex;
  14. justify-content: center;
  15. align-items: center;
  16. }
  17. /* 后代选择器 */
  18. .container div {
  19. border: 1px solid coral;
  20. }
  21. /* 父子选择器 */
  22. body>div {
  23. background-color: coral;
  24. border: 3px solid lightpink;
  25. }
  26. /* 使用后代选择器模拟父子选择器 */
  27. body div.container {
  28. border: 5px solid lightseagreen;
  29. }
  30. /* 同级相邻选择器 */
  31. /* 选择相邻的一个,第几个有 + 号决定 */
  32. .item.center+.item {
  33. background-color: limegreen;
  34. }
  35. /* 同级所有选择器 */
  36. .item.center~.item {
  37. background-color: limegreen;
  38. }
  39. </style>
  • 预览效果

3.伪类选择器

  • 学习之前,先分析上下文选择器的局限性,例如选择同一个父级下的第二个子元素,就没那么简单
  • 而伪类就正好弥补了上下文选择器的短板, 所以伪类,大多数是基于文档中元素结构的
  • : 本意是假的,不存在的意思, 这里是特指, 不需要在元素上添加额外的属性来获取元素
  • : 暗指伪类的级别, 仍然是属于”class”级别, 仍然属于属性选择器范畴,级别高于元素选择器

  • 应用场景:

场景 描述
结构伪类 根据子元素的位置特征进行选择
表单伪类 根据表单控件状态特征进行选择

3.1结构伪类

3.1.1不分组匹配

选择器 描述 举例
:first-child 匹配第一个子元素 div:first-child
:last-child 匹配最后一个子元素 div:last-child
:only-child 选择元素的唯一子元素 div:only-child
nth-child(n) 匹配任意位置的子元素 div:nth-child(n)
:nth-last-child(n) 匹配倒数任意位置的子元素 div:nth-last-child(n)
  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. .container {
  9. width: 300px;
  10. height: 300px;
  11. display: grid;
  12. grid-template-columns: repeat(3, 1fr);
  13. gap: 5px;
  14. background-color: rgb(83, 83, 55);
  15. }
  16. .item {
  17. font-size: 2rem;
  18. background-color: lightskyblue;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. /* body,第一个单元格都变了 */
  24. /* 为了防止递归,应该在具体父元素上调用伪类 */
  25. .container> :first-child {
  26. background-color: rgb(240, 24, 24);
  27. }
  28. /* 匹配最后一个 */
  29. .container> :last-child {
  30. background-color: rgb(243, 19, 243);
  31. }
  32. /* 匹配任何一个,索引是从1开始计算 */
  33. .container> :nth-child(4) {
  34. /* :nth-child(n) n:支持表达式 */
  35. background-color: lightgreen;
  36. }
  37. /* 直选前三 */
  38. .container> :nth-child(-n + 1) {
  39. background-color: lightseagreen;
  40. }
  41. /* 选择倒数第二个 */
  42. .container :nth-last-child(2) {
  43. background-color: lime;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div class="container">
  49. <div class="item">1</div>
  50. <div class="item">2</div>
  51. <div class="item">3</div>
  52. <div class="item">4</div>
  53. <div class="item">5</div>
  54. <div class="item">6</div>
  55. <div class="item">7</div>
  56. <div class="item">8</div>
  57. <div class="item">9</div>
  58. </div>
  59. </body>
  60. </html>
  • 预览效果

3.1.1不分组匹配

选择器 描述 举例
:first-of-type 匹配按类型分组后的第一个字元素 div:first-of-type
:last-of-type 匹配按类型分组后的最后一个字元素 div:last-of-type
:only-of-type 匹配按类型分组后的唯一字元素 div:only-of-type
:nth-of-type() 匹配按类型分组后的任意位置的子元素 div:nth-of-type(n)
:nth-last-of-type() 匹配按类型分组后倒数任意位置的子元素 div:nth-last-of-type(n)
  • 允许使用表达式来匹配一组元素,表达式中的”n”是从”0”开始计数,且必须写到前面
  • “-n”表示获取前面一组元素,正数表示从指定位置获取余下元素
  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. .container {
  9. width: 300px;
  10. height: 300px;
  11. display: grid;
  12. grid-template-columns: repeat(3, 1fr);
  13. gap: 5px;
  14. background-color: rgb(83, 83, 55);
  15. }
  16. .item {
  17. font-size: 2rem;
  18. background-color: lightskyblue;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. .container span:first-of-type {
  24. background-color: lime;
  25. }
  26. .container span:last-of-type {
  27. background-color: lime;
  28. }
  29. .container span:nth-of-type(3) {
  30. background-color: lime;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="container">
  36. <div class="item">1</div>
  37. <div class="item">2</div>
  38. <div class="item">3</div>
  39. <div class="item">4</div>
  40. <span class="item">5</span>
  41. <span class="item">6</span>
  42. <span class="item">7</span>
  43. <span class="item">8</span>
  44. <span class="item">9</span>
  45. </div>
  46. </body>
  47. </html>
  • 预览效果

3.3其他伪类

选择器 描述
:autive 向被激活的元素添加样式
:focus 向拥有键盘输入焦点的元素添加样式
:hover 当鼠标悬浮在元素上方时,向元素添加样式
:link 向未被访问的连接添加样式
:visited 向已被访问的链接添加样式
:root 跟元素,通常是html
:empty 选择没有任何字元素的元素(含文本节点)
:not() 排除与选择器参数匹配的元素

总结

  • 了解选择器的种类:元素选择器,群组选择器,通配选择器,属性选择器,类选择器,id选择器。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:伪类用得很熟悉, 伪类用得越多, 你的html代码就越干净, seo就越好
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!