博客列表 >前端选择器之常规选择器、伪类选择器与伪元素

前端选择器之常规选择器、伪类选择器与伪元素

new
new原创
2020年06月18日 18:35:26695浏览

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>Document</title>
  7. <style>
  8. div {
  9. background-color: blue;
  10. height: 50px;
  11. width: 500px;
  12. border: 5px solid coral;
  13. }
  14. /* 元素选择器:标签选择器 */
  15. .box {
  16. color: red;
  17. background-color: black;
  18. border: 5px solid cyan;
  19. }
  20. /* 类选择器:前面为 . ,对应着HTML标签中的class属性 */
  21. #box1 {
  22. background-color: chartreuse;
  23. }
  24. /* id选择器:前面为# */
  25. </style>
  26. </head>
  27. <body>
  28. <div>1</div>
  29. <div class="container">
  30. <div class="box" id="box1">1</div>
  31. <div class="box">2</div>
  32. <div class="box">3</div>
  33. <div class="box">4</div>
  34. <div class="box">5</div>
  35. <div class="box">6</div>
  36. </div>
  37. </body>
  38. </html>

演示效果


常规选择器有标签选择器、类选择器、ID选择器等
ID选择器现在很少用,大部分用在表单元素和锚点上,所以尽可能用类选择器
从效果图中可以得知
优先级:标签选择器 < 类选择器 < ID选择器

2.上下文选择器

  • 后代选择器使用类+空格+标签组成

    1. /* 后代选择器: 空格 */
    2. body div {
    3. border: 5px solid coral;
    4. }
  • 父子选择器使用 类+>+标签组成

    1. /* 父子选择器: > */
    2. body > div {
    3. border: 5px solid coral;
    4. }
  • 相邻兄弟选择器可选择紧接在另一元素后的元素,且二者有相同父元素

  1. /* 同级相邻选择器 */
  2. .box.box1 + .box {
  3. background-color: lightgreen;
  4. }
  • 同级所有选择器是选取指定元素下同级的所有元素。
  1. /* 同级所有选择器 */
  2. .box.box1 ~ .box {
  3. background-color: lightsalmon;
  4. }

2.举例演示结构伪类选择器(不分组)

  • :first-child 匹配第一个元素

    1. <style>
    2. /* :first-child 匹配第一个元素(第1个li标签字体红色) */
    3. li:first-child{
    4. color: red;
    5. }
    6. </style>
    7. <div class="box">
    8. <ul>
    9. <li class="item">1</li>
    10. <li class="item">2</li>
    11. <li class="item">3</li>
    12. <li class="item">4</li>
    13. <li class="item">5</li>
    14. </ul>
    15. </div>
  • :first-child 匹配最后一个元素

    1. <style>
    2. /* :last-child 匹配最后一个元素(第5个li标签字体红色) */
    3. li:last-child{
    4. color: red;
    5. }
    6. </style>
    7. <div class="box">
    8. <ul>
    9. <li class="item">1</li>
    10. <li class="item">2</li>
    11. <li class="item">3</li>
    12. <li class="item">4</li>
    13. <li class="item">5</li>
    14. </ul>
    15. </div>
  • :nth-child(索引号) 匹配指定元素
    1. <style>
    2. /* :nth-child(3) 匹配指定元素-索引方式:从1开始(比如:第3个li标签字体红色) */
    3. li:nth-child(3){
    4. color: red;
    5. }
    6. </style>
    7. <div class="box">
    8. <ul>
    9. <li class="item">1</li>
    10. <li class="item">2</li>
    11. <li class="item">3</li>
    12. <li class="item">4</li>
    13. <li class="item">5</li>
    14. </ul>
    15. </div>
  • :nth-child(2n) 或 :nth-child(even) 匹配偶数元素
    1. <style>
    2. /* :nth-child(2n)和:nth-child(even) 匹配偶数元素(第2/4个li标签字体红色) */
    3. li:nth-child(2n){
    4. color: red;
    5. }
    6. /* li:nth-child(even){
    7. color: red;
    8. } */
    9. </style>
    10. <div class="box">
    11. <ul>
    12. <li class="item">1</li>
    13. <li class="item">2</li>
    14. <li class="item">3</li>
    15. <li class="item">4</li>
    16. <li class="item">5</li>
    17. </ul>
    18. </div>
  • :nth-child(2n-1) 或 :nth-child(odd) 匹配奇数元素
    1. <style>
    2. /* :nth-child(2n-1)和:nth-child(odd) 匹配奇数元素(第1/3/5个li标签字体红色) */
    3. /* li:nth-child(2n-1){
    4. color: red;
    5. } */
    6. li:nth-child(odd){
    7. color: red;
    8. }
    9. </style>
    10. <div class="box">
    11. <ul>
    12. <li class="item">1</li>
    13. <li class="item">2</li>
    14. <li class="item">3</li>
    15. <li class="item">4</li>
    16. <li class="item">5</li>
    17. </ul>
    18. </div>
  • 其他
    :nth-child(n + 索引号) 指定位置剩下的所有元素
    :nth-child(-n + 索引号) 只取前几个
    :nth-last-child(-n + 索引号) 只取最后几个
    :nth-last-child(索引号) 倒数的方式匹配

3.结构伪类选择器(分组)

  • :last-of-type伪类名+标签定义 例子如下:
    1. <style>
    2. /* 匹配span标签最后1个=3 */
    3. .box span:last-of-type {
    4. color: red;
    5. }
    6. /* 匹配p标签最后1个=6 */
    7. .box p:last-of-type {
    8. color: red;
    9. }
    10. </style>
    11. <div class="box">
    12. <span class="item">1</span>
    13. <span class="item">2</span>
    14. <span class="item">3</span>
    15. <p class="item">4</p>
    16. <p class="item">5</p>
    17. <p class="item">6</p>
    18. </div>

4.伪类与伪元素

  • 举例演示:target, :not, :before, :after, :focus
    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. #login-form {
    9. display: none;
    10. }
    11. /* :target: 必须id配合,实现锚点操作 */
    12. /* 当前#login-form的表单元素被a的锚点激活的时候执行 */
    13. #login-form:target {
    14. display: block;
    15. }
    16. /* :focus: 当获取焦点的时候执行,改变样式 */
    17. input:focus {
    18. background-color: yellow;
    19. }
    20. /* ::selection: 设置选中文本的前景色与背景色 */
    21. input::selection {
    22. color: white;
    23. background-color: blue;
    24. }
    25. /* :not(): 用于选择不满足条件元素 */
    26. .list > :not(:nth-of-type(2)) {
    27. color: red;
    28. }
    29. /* ::before 这里在内容就没有出现在HTML中,而是由浏览器加载样式后生成的*/
    30. .list::before {
    31. content: "我的地址管理";
    32. color: blue;
    33. font-size: 1.5rem;
    34. border-bottom: 1px solid #000;
    35. }
    36. /* ::after 这里在内容就没有出现在HTML中,而是由浏览器加载样式后生成的*/
    37. .list::after {
    38. content: "验证码发送中...";
    39. color: green;
    40. font-size: 1.1rem;
    41. }
    42. /* 伪元素前面是双冒号, 伪类前能是单冒号 */
    43. </style>
    44. </head>
    45. <body>
    46. <!-- <a href="#login-form">我要登录:</a> -->
    47. <button onclick="location='#login-form'">点击登录</button>
    48. <!-- 把按钮变成锚点,通过点击事件跳转绑定 -->
    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>地址1</li>
    59. <li>地址2</li>
    60. <li>地址3</li>
    61. <li>地址4</li>
    62. <li>地址5</li>
    63. </ul>
    64. </body>
    65. </html>

    总结

  • 伪类(前面是单冒号)
伪类 描述
:target 必须id配合,实现锚点操作
:focus 当获取焦点的时候
:not() 用于选择不满足条件元素
  • 伪元素(前面是双冒号)
伪元素 描述
::selection 一般用于设置选中文本的前后背景色
::before 在元素前添加内容(与content一起使用)
::after 伪元素,可用于页脚等

伪类选择器灵活应用,可以简化代码,减少class的使用,提高效率。

上一条:导航下拉菜单下一条:精典表格写法
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议