博客列表 >css选择器实战运用笔记

css选择器实战运用笔记

Dong.
Dong.原创
2020年06月16日 17:22:56753浏览

一、简单选择器

1.元素选择器:标签选择器
*: 属于元素级别

  1. body {
  2. background-color: red;
  3. }

浏览器显示样式:
标签选择器

2.类选择器:前面为 . ,对应着HTML标签中的class属性

  1. <head>
  2. <style>
  3. .box{
  4. width:200px;
  5. height:200px;
  6. background:green;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <div class="box"></div>
  12. </body>

浏览器显示样式:[类选择器]

3.id选择器:前面为#

  1. <head>
  2. <style>
  3. .box#box{
  4. width:200px;
  5. height:200px;
  6. background:red;
  7. }
  8. #box{
  9. background:blue;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div class="box" id="box"></div>
  15. </body>

浏览器显示样式:
id选择器
注意:因为.box#box的优先级要高于#box所以显示为红色。而#box.box的优先级要比.box#box的优先级更高,了解复合类样式的优先级规则。id现在很少用,大部分用在表单元素和锚点上,所以尽可能用class选择器
ps:id,class可以添加上任何元素。
优先级:元素 < class < id


二、上下文选择器

拥有一个起点或者参照物
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. .container {
  9. width: 625px;
  10. height: 625px;
  11. padding: 5px;
  12. background: #ffffff;
  13. display: grid;/*网格布局*/
  14. grid-template-columns: repeat(3, 1fr);
  15. /*网格的列为3个宽度相同的列*/
  16. grid-gap: 5px;/*网格间距为5像素*/
  17. }
  18. .item {
  19. font-size: 4rem;rem/*相对大小*/
  20. display: flex;/*弹性布局*/
  21. justify-content: center;
  22. align-items: center;/*居中*/
  23. background: #f5bb1e;/*垂直居中*/
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="container">
  29. <div class="item">1</div>
  30. <div class="item">2</div>
  31. <div class="item">3</div>
  32. <div class="item">4</div>
  33. <div class="item center">5</div>
  34. <div class="item">6</div>
  35. <div class="item">7</div>
  36. <div class="item">8</div>
  37. <div class="item">9</div>
  38. </div>
  39. </body>
  40. </html>

网页显示样式:
后代选择器
接下来用后代选择器的方式给九宫格加上边框

  1. <style>
  2. .container .item{border:2px solid #000;}
  3. </style>

显示样式:
后代边框
给所有的具有.item选择器的元素都加上了边框

2.父子选择器: >

  1. <style>
  2. body > div {border:5px solid orange;}
  3. </style>

显示结果:
父子
如果用后代选择器的话:

  1. <style>
  2. body div {border:5px solid blue;}
  3. </style>

显示结果:

注意:后代选择器表示的是不仅是父子关系,它会获得所有后代元素,用空格来连接;父子选择器表示的仅仅是父子关系,用>来连接。

3.同级相邻选择器: +

  1. <style>
  2. .item.center + .item{background:greenyellow;}
  3. </style>

显示样式:
同级
注意:同级相邻选择器只能选择同级别后面最近的一个元素

4.同级所有标签: ~

  1. <style>
  2. .item.center ~ .item{background:red;}
  3. </style>

显示结果:
同级标签

三、结果伪类选择器:不分组(不区分元素)

1.匹配第一个子元素:
示例:九宫格

  1. <style>
  2. .container :first-child{background:red;}
  3. </style>

运行结果:
第一个子元素
注意::first-child没有父元素的话会从根元素开始匹配每个父元素下的第一个元素,一定要在:first-child前面加上父元素并加空格

2.最后一个子元素

  1. <style>
  2. .container :last-child{background:red;}
  3. </style>

显示样式:
最后一个

3.选择第任意一个元素: .container>:nth-child(需要索引的元素)
示例1:选择第4个元素

  1. <style>
  2. .container :nth-child(4){background:red;}
  3. </style>

显示样式:
任意元素

示例2:利用奇偶数选择单元格

  1. <style>
  2. .container :nth-child(2n){background:red;}
  3. /*偶数为红色*/
  4. .container :nth-child(2n-1){background:green;}
  5. /*奇数为绿色*/
  6. </style>
  7. 或者用even表示偶数,odd表示奇数
  8. .container :nth-child(even){background:red;}
  9. /*偶数为红色*/
  10. .container :nth-child(odd){background:green;}
  11. /*奇数为绿色*/

显示样式:
奇偶数
注意:偶数可用even表示,奇数可用odd表示。

示例3:从指定位置开始,选择剩下的元素(从第5个到第8个)

  1. <style>
  2. .container :nth-child(n + 5):nth-child(-n + 8){background:green;}
  3. </style>

结果:

示例4:只取前三个值

  1. <style>
  2. .container :nth-child(-n + 3){background:blue;}
  3. </style>

结果:

示例5:选取倒数三个元素

  1. <style>
  2. .container :nth-last-child(-n + 3){background:blue;}
  3. </style>

结果:

注意:n的索引是从0开始的,所以2n就是偶数,2n+1/2n-1就是奇数,选择从第几个开始是加几,选择从第一个到第几个是-n加几,选择n是从第5个到第8个是 :nth-chlid(n+5):nth-child(-n+8),两者可以结合使用


四、伪类分组选择器

元素按类型进行分组,在分组中根据索引进行选择

1.获取第一个:(首先把九宫格格式改成<span class="item"></span>)

  1. <div class="item">1</div>
  2. <div class="item">2</div>
  3. <div class="item">3</div>
  4. <span class="item">4</span>
  5. <span class="item">5</span>
  6. <span class="item">6</span>
  7. <span class="item">7</span>
  8. <span class="item">8</span>
  9. <span class="item">9</span>

示例1:

  1. <style>
  2. .container :last-of-type{background:pink;}
  3. </style>

结果:

分组结构伪类分两步:1.元素按类型进行分组;2.在分组中根据索引进行选择

2.在分组中匹配任意一个

实例1:匹配span分组中的第三个元素

  1. <style>
  2. .container span:nth-of-type(3){background:pink;}
  3. </style>

结果:

实例2:匹配span分组中的前三个元素

  1. <style>
  2. .container span:nth-of-type(-n + 3){background:pink;}
  3. </style>

结果:

实例3:匹配span分组中的倒数2个元素

  1. <style>
  2. .container span:nth-last-of-type(-n + 2){background:pink;}
  3. </style>

结果:


五、伪类与伪元素

1. :target必须与id配合,实现锚点操作

  1. <head>
  2. <style>
  3. #login-form {display: none;}
  4. #login-form:target {display: block;}
  5. /*当前#login-form的表单元素被a的锚点激活时执行*/
  6. </style>
  7. </head>
  8. <body>
  9. <a href="#login-form">我要登录:</a>
  10. <form action="" method="post" id="login-form">
  11. <label for="email">邮箱:</label>
  12. <!--<label> 标签为 input 元素定义标注(标记)-->
  13. <input type="email" name="email" id="email" />
  14. <label for="password">密码:</label>
  15. <input type="password" name="password" id="password" />
  16. <button>登录</button>
  17. </form>
  18. </body>

结果:

点击后:

2. :focus 当获取焦点的时候
示例:

  1. <style>
  2. input:focus{background:yellow;}
  3. </style>

结果:

3. ::selection设置选中文本的前景色和背景色
示例:

  1. <style>
  2. input::selection{color:red;background-color:greenyellow;}
  3. </style>

双冒号
结果:

4. :not() 用于选择不满足条件元素
示例:

  1. <style>
  2. .list > *:not(:nth-of-style(2)){color:red;}
  3. </style>
  4. <body>
  5. <ul class="list">
  6. <li>1</li>
  7. <li>2</li>
  8. <li>3</li>
  9. <li>4</li>
  10. </ul>
  11. </body>

结果:

5. ::before在元素之前,::after 在元素之后
示例:

  1. <style>
  2. .list::before{content:"购物车";font-size:2rem;}
  3. .list::after{content:"结算中...";color:red;font-size:2rem;}
  4. </style>
  5. <body>
  6. <ul class="list">
  7. <li>1</li>
  8. <li>2</li>
  9. <li>3</li>
  10. <li>4</li>
  11. </ul>
  12. </body>

结果:

双冒号

备注:伪元素前面为双冒号,伪类前面为单冒号。

学习总结:

1.知道了选择器的作用
2。伪类与伪元素的区别

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