文章目录[x]
- 1:1.1、属性选择器🔥
- 2:1.2、结构伪类选择器🔥
- 2.1:①、E:first-child 和E:last-child
- 2.2:②、nth-child(n)
- 2.3:③、E:first-of-type和E:last-of-type
- 2.4:④、区别
- 2.5:⑤、小结
- 3:1.3、伪元素选择器🔥
- 4:2.1、content-box
- 5:2.2、border-box🔥
- 6:3.1、滤镜filter
1、新增选择器🔥
CSS3 给我们新增了选择器,可以更加便捷,更加自由的选择目标元素。
- 属性选择器
- 结构伪类选择器
- 伪元素选择器
1.1、属性选择器🔥
- 属性选择器可以根据元素特定的属性来选择元素,这样就可以不用借助于类或者id选择器
选择符 | 简介 |
---|---|
E[att] | 选择具有att属性的E元素 |
E[att=“val”] | 选择具有att属性且属性值等于val的E元素 |
E[att^=“val”] | 匹配具有att属性且值以val开头的E元素 |
E[att$=“val”] | 匹配具有att属性且值以val结尾的E元素 |
E[att*=“val”] | 匹配具有att属性且值中含有val的E元素 |
1、利用属性选择器就可以不借助于类或者id选择器
<head>
<style>
input[value] {
color: pink;
}
</style>
</head>
<body>
<!-- 1.利用属性选择器可以不借助类或者id选择器 -->
<input type="text" value="请输入用户名">
<input type="text">
</body>
2、属性选择器还可以选择 属性 = 值的某些元素
<head>
<style>
input[type=text] {
color: pink;
}
</style>
</head>
<body>
<!-- 2.属性选择器还可以选择 属性=值的某些元素 -->
<input type="text" name="" id="">
<input type="password" name="" id="">
</body>
3、属性选择器可以选择属性值开头的某些元素
<head> /* 选择首先是div,然后具有class属性,并且是icon开头的值 */
<style>
div[class^=icon] {
color: pink;
}
</style>
</head>
<body>
<!-- 3.属性选择器可以选择属性值开头的某些元素 -->
<div class="icon1">小图标1</div>
<div class="icon2">小图标2</div>
<div class="icon3">小图标3</div>
<div class="icon4">小图标4</div>
</body>
4、属性选择器可以选择属性值结尾的某些元素
<head>
<style>
section[class$=data] {
color: pink;
}
</style>
</head>
<body>
<!-- 4.属性选择器可以选择属性值结尾的某些元素 -->
<section class="icon1-data">1</section>
<section class="icon2-data">2</section>
<section class="icon3-data">3</section>
</body>
注意:类选择器,属性选择器,伪类选择器,权重为10
1.2、结构伪类选择器🔥
- 结构伪类选择器主要根据文档结构来选择元素
- 常用于根据父级选择器选择里面的子元素
选择符 | 简介 |
---|---|
E:first-child | 匹配父元素中的第一个子元素E |
E:last-child | 匹配父元素中最后一个E元素 |
E:nth-child(n) | 匹配父元素中的第n个子元素E |
E:first-of-type | 指定类型E的第一个 |
E:last-of-type | 指定类型E的最后一个 |
E:nth-of-type(n) | 指定类型E的第n个 |
①、E:first-child 和E:last-child
<head>
<style>
/* 1.选择ul里面的第一个孩子 小li */
ul li:first-child {
background-color: pink;
}
/* 2.选择ul里面的最后一个孩子 小li */
ul li:last-child {
background-color: pink;
}
</style>
</head>
<body>
<ul>
<li>我是第1个孩子</li>
<li>我是第2个孩子</li>
<li>我是第3个孩子</li>
<li>我是第4个孩子</li>
<li>我是第5个孩子</li>
<li>我是第6个孩子</li>
<li>我是第7个孩子</li>
<li>我是第8个孩子</li>
</ul>
②、nth-child(n)
nth-child(n)
选择某个父级元素的一个或多个特定的子元素(重点)- n可以是数字,关键字和公式
- n如果是数字,就是选择第n个子元素,里面数字从1开始
- n可以是关键字:even 偶数,odd奇数
- n可以是公式:常见的公式如下(如果n是公式,则从0开始计算,但是第0个元素或者超出了元素的个数会被忽略)
公式 | 取值 |
---|---|
2n | 偶数(等价于even) |
2n+1 | 奇数(等价于odd) |
5n | 5 10 15 …(5的倍数) |
n+5 | 从第5个开始(包含第五个)到最后 |
-n+5 | 前5个(包含第5个) |
<head>
<style>
/* 选择ul里面的第2个孩子 小li */
ul li:nth-child(2) {
background-color: pink;
}
</style>
</head>
<body>
<ul>
<li>我是第1个孩子</li>
<li>我是第2个孩子</li>
<li>我是第3个孩子</li>
<li>我是第4个孩子</li>
<li>我是第5个孩子</li>
<li>我是第6个孩子</li>
<li>我是第7个孩子</li>
<li>我是第8个孩子</li>
</ul>
</body>
<style>
/* 1.把所有的偶数 even的孩子选出来 */
ul li:nth-child(even) {
background-color: #ccc;
}
/* 2.把所有的奇数 odd的孩子选出来 */
ul li:nth-child(odd) {
background-color: gray;
}
/* 3.nth-child(n) 从0开始 每次加1 往后面计算 这里面必须是n 不能是其他的字母 选择了所有的孩子*/
/* ol li:nth-child(n) {
background-color: pink;
} */
/* 4.nth-child(2n)母选择了所有的偶数孩子 等价于 even*/
/* ol li:nth-child(2n) {
background-color: pink;
}
ol li:nth-child(2n+1) {
background-color: skyblue;
} */
/* ol li:nth-child(n+3) {
background-color: pink;
} */
ol li:nth-child(-n+3) {
background-color: pink;
}
</style>
</head>
<body>
<ul>
<li>我是第1个孩子</li>
<li>我是第2个孩子</li>
<li>我是第3个孩子</li>
<li>我是第4个孩子</li>
<li>我是第5个孩子</li>
<li>我是第6个孩子</li>
<li>我是第7个孩子</li>
<li>我是第8个孩子</li>
</ul>
<ol>
<li>我是第1个孩子</li>
<li>我是第2个孩子</li>
<li>我是第3个孩子</li>
<li>我是第4个孩子</li>
<li>我是第5个孩子</li>
<li>我是第6个孩子</li>
<li>我是第7个孩子</li>
<li>我是第8个孩子</li>
</ol>
</body>
③、E:first-of-type和E:last-of-type
E:first-of-type | 指定类型E的第一个 |
---|---|
E:last-of-type | 指定类型E的最后一个 |
<head>
<style>
ul li:first-of-type {
background-color: pink;
}
ul li:last-of-type {
background-color: pink;
}
ul li:nth-last-child(2) {
background-color: pink;
}
</style>
</head>
<body>
<ul>
<li>我是第1个孩子</li>
<li>我是第2个孩子</li>
<li>我是第3个孩子</li>
<li>我是第4个孩子</li>
<li>我是第5个孩子</li>
<li>我是第6个孩子</li>
<li>我是第7个孩子</li>
<li>我是第8个孩子</li>
</ul>
</body>
④、区别
nth-child(n)
和nth-of-type(n)
区别?- nth-child 对父元素里面所有孩子排序选择(序号是固定的),先找到第n个孩子,然后看看是否和E匹配
- nth-of-type 对父元素里面指定子元素进行排序选择,先去匹配E,然后再根据E 找第n个孩子
⑤、小结
- 结构伪类选择器一般用于选择父级里面的第几个孩子
- nth-child 对父元素里面所有孩子排序选择(序号是固定的),先找到第n个孩子,然后看看是否和E匹配
- nth-of-type 对父元素里面指定子元素进行排序选择,先去匹配E,然后再根据E找第n个孩子
- 关于nth-child(n), 我们要知道n是从0开始计算的,要记住常用的公式
- 如果是无序列表,我们肯定用 nth-child 更多
- 类选择器,属性选择器,伪类选择器,权重为10
1.3、伪元素选择器🔥
- 伪元素选择器可以帮我们利用CSS创建新标签元素,而不需要HTML标签,从而简化HTML结构
选择符 | 简介 |
---|---|
::before | 在元素内部的前面插入内容 |
::after | 在元素内部的后面插入内容 |
注意:
- before 和 after 创建一个元素,但是是属于行内元素
- before和after 都是一个盒子,都作为父元素的孩子
- 新创建的这个元素在文档树中是找不到的,所以我们称为伪元素
- 语法:
element::before {
}
- before是放在内容的前面,after是放在了内容的后面
<head>
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
}
/* div::before 权重是2 */
div::before {
/* 这个content是必须要写的 */
content: '我';
}
div::after {
content: '小猪佩奇';
}
</style>
</head>
<body>
<div>
是
</div>
- before 和 after 必须有 content 属性
- before 在父元素内容的前面创建元素 ,after 在父元素内容的后面插入元素
- 伪元素选择器 和 标签选择器 一样,权重为1
2、CSS3盒子模型🔥
- CSS3 中可以通过
box-sizing
来指定盒模型 - 有2个值:这样我们计算盒子大小的方式就发生了改变
content-box
border-box
2.1、content-box
box-sizing: content-box;
- 第一种情况是 CSS 的盒子模型,盒子大小为 width + padding + border
- 此种情况盒子大小为 宽度 + 内边距 + 边框,这也是我们之前写盒子所默认的
2.2、border-box🔥
box-sizing: border-box;
- 第二种情况是 CSS3 的盒子模型,盒子大小为 width
- 此种情况盒子大小为 宽度,不包括内边距和边框,这样 padding 和 border 就不会撑大盒子了(前提是 padding 和 border 不会超过 width 宽度)
- 我们可以在以后的 css 通配符中添加 CSS3 盒子模型
* {
margin: 0;
padding: 0;
box-sizing: border-box;
/* 这样的话padding和border就不会撑大盒子了 */
}
3、C3其他特性
3.1、滤镜filter
filter
: CSS属性将模糊或颜色偏移等图形效果应用于元素(图片变模糊)- 语法:
filter: 函数();
- 模糊处理:
blur
,数值越大越模糊
