11.选择器、盒子模型、模糊、动画过度
大约 10 分钟学习笔记前端基础CSS
一、选择器
1. 属性选择器
属性选择器:可以根据元素特定的属性来选择元素
注意
类选择器、属性选择器、伪类选择器,他们的权重都是 10
语法:
元素[属性] {
    样式
}| 选择符 | 说明 | 
|---|---|
| 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 元素 | 
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>CSS3新增属性选择器</title>
		<style>
			/* 必须是input 但是同时具有 value这个属性 选择这个元素  [] */
			/* input[value] {
            color:pink;
        } */
			/* 只选择 type =text 文本框的input 选取出来 */
			input[type="text"] {
				color: pink;
			}
			/* 选择首先是div 然后 具有class属性 并且属性值 必须是 icon开头的这些元素 */
			div[class^="icon"] {
				color: red;
			}
			section[class$="data"] {
				color: blue;
			}
			div.icon1 {
				color: skyblue;
			}
			/* 类选择器和属性选择器 伪类选择器 权重都是 10 */
		</style>
	</head>
	<body>
		<!-- 1. 利用属性选择器就可以不用借助于类或者id选择器 -->
		<!-- <input type="text" value="请输入用户名">
    <input type="text"> -->
		<!-- 2. 属性选择器还可以选择属性=值的某些元素 重点务必掌握的 -->
		<input type="text" name="" id="" />
		<input type="password" name="" id="" />
		<!-- 3. 属性选择器可以选择属性值开头的某些元素 -->
		<div class="icon1">小图标1</div>
		<div class="icon2">小图标2</div>
		<div class="icon3">小图标3</div>
		<div class="icon4">小图标4</div>
		<div>我是打酱油的</div>
		<!-- 4. 属性选择器可以选择属性值结尾的某些元素 -->
		<section class="icon1-data">我是安其拉</section>
		<section class="icon2-data">我是哥斯拉</section>
		<section class="icon3-ico">哪我是谁</section>
	</body>
</html>2. 结构伪类选择器
结构伪类选择器: 根据 文档结构 来选择元素,常用于根据父级选择里面的子元素
| 选择符 | 简介 | 
|---|---|
| E:first-child | 匹配父元素中的第一个子元素 E | 
| E:last-child | 匹配父元素中最后一个 E 元素 | 
| E:nth-child(n) | 匹配父元素中的 第 n 个子元素 E | 
| E:fist-of-type | 指定类型 E 的第一个 | 
| E:last-of-type | 指定类型 E 的最后一个 | 
| E:nth-of-type | 指定类型 E 的第 N 个 | 
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>CSS3新增结构伪类选择器</title>
		<style>
			/* 1. 选择ul里面的第一个孩子 小li */
			ul li:first-child {
				background-color: pink;
			}
			/* 2. 选择ul里面的最后一个孩子 小li */
			ul li:last-child {
				background-color: pink;
			}
			/* 3. 选择ul里面的第2个孩子 小li */
			ul li:nth-child(2) {
				background-color: skyblue;
			}
			ul li:nth-child(6) {
				background-color: skyblue;
			}
		</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>
</html>nth-child 选择器
nth-child(n)选择某个父元素的一个或多个特定的子元素n: 可以是数字、关键字、公式
- n 如果是数字:就是选择器第 N 个子元素
- n 如果是关键字:even(偶数)、odd(奇数)
- n 如果是公式:则从 0 开始计算,往后每次 +1,0 和 超出 的部分会自动忽略
nth-child(n),表示选择全部数据
nth-child(2n),等价于 even,选择所有的偶数
详情
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>CSS3新增结构伪类选择器-nth-child</title>
		<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>
</html>nth-child 和 nth-of-type 的区别
相同点:
- 他们的用法基本一致,都是对元素进行排序,然后筛选;
- 他们的权重都是 10;
不同点:
- nth-child(n):先对所有元素进行排序,然后根据 n 进行筛选,如果结果与条件不一致,则忽略
- nth-of-type(n):先对条件中的元素进行排序,然后根据 n 进行筛选;
详情
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
		<style>
			* {
				padding: 0;
				margin: 0;
			}
			.box {
				margin: 20px auto;
				width: 600px;
				height: 300px;
				border: 1px solid #222;
				text-align: center;
			}
			.box tr td {
				border: 1px solid #222;
				line-height: 30px;
			}
			.tr2 .td1 div:nth-child(1) {
				background-color: goldenrod;
			}
			.tr2 .td2 div:nth-of-type(1) {
				background-color: goldenrod;
			}
		</style>
	</head>
	<body>
		<table class="box" cellspacing="0" cellpadding="0">
			<tr class="tr1">
				<td>nth-child(1)</td>
				<td>nth-of-type(1)</td>
			</tr>
			<tr>
				<td>排序后的第一个元素是 p ,与 div 不符,所以忽略</td>
				<td>先对 div 元素进行排序,然后选择第一个元素改变其背景颜色</td>
			</tr>
			<tr class="tr2">
				<td class="td1">
					<p>光头强</p>
					<div>熊大</div>
					<div>熊二</div>
				</td>
				<td class="td2">
					<p>光头强</p>
					<div>熊大</div>
					<div>熊二</div>
				</td>
			</tr>
		</table>
	</body>
</html>3. 伪元素选择器
伪元素选择器: 利用 css 创建新标签元素,而不需要 HTML 标签,从而简化 HTML 结构
语法:
父元素::before()| 选择符 | 简介 | 
|---|---|
| ::before | 在元素内部的前面插入内容 | 
| ::after | 在元素内部的后面插入内容 | 
注意:
- before 和 after 创建的元素属于行内元素
- 创建的元素在文档树中无法找到;
- 必须要 content属性
- 伪元素选择器 和 标签选择器 一样,权重为 1;
详情
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @font-face {
            font-family: 'iconfont';
            /* Project id 2303857 */
            src: url('//at.alicdn.com/t/font_2303857_vw08kfmp6uq.woff2?t=1638161279592') format('woff2'),
                url('//at.alicdn.com/t/font_2303857_vw08kfmp6uq.woff?t=1638161279592') format('woff'),
                url('//at.alicdn.com/t/font_2303857_vw08kfmp6uq.ttf?t=1638161279592') format('truetype');
        }
        .box {
            position: relative;
            width: 200px;
            height: 35px;
            border: 1px solid #222;
            margin: 30px auto;
        }
        .box::after {
            position: absolute;
            top: 10px;
            right: 10px;
            font-family: 'iconfont';
            content: '\e665';
            color: #222;
            font-size: 18px;
        }
        /* 鼠标滑过显示动画 */
        .tudou {
            position: relative;
            width: 300px;
            height: 200px;
            margin: 30px auto;
        }
        .tudou img {
            width: 100%;
            height: 100%;
        }
        .tudou::before {
            content: "";
            display: none;
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0, .4) url(https://img.pupper.cn/img/202111241329939.png) no-repeat center;
        }
        .tudou:hover::before {
            display: block;
        }
    </style>
</head>
<body>
        <div class="box"></div>
        <div class="tudou">
            <img src="https://img.pupper.cn/img/202111241153035.jpg" alt="">
        </div>
</body>
</html>4. 伪元素清除浮动
额外标签法升级版
.clearfix:after {
	/* 单冒号是为了兼容低版本浏览器 */
	content: "";
	display: block; /* 行内元素转为块级元素 */
	height: 0;
	visibility: hidden;
	clear: both;
}闭合元素
.clearfix:before,
.clearfix:after {
	content: "";
	display: table;
}
.clearfix:after {
	clear: both;
}二、 盒子模型 (box-sizing: border-box)
css3 中通过
box-sizing来指定盒子模型
注
- box-sizing: content-box: 盒子大小为 width + padding + border (以前默认的计算方式)- padding 和 border 的设置 会 把盒子撑开
 
- box-sizing: border-box: 盒子大小为 windth- padding 和 border 的设置 不会 把盒子撑开
 
详情
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 200px;
            height: 200px;
            padding: 20px;
            border: 5px solid coral;
            margin-bottom: 30px;
            background-color: aquamarine;
        }
        .box2 {
            box-sizing: border-box;
            width: 200px;
            height: 200px;
            padding: 20px;
            border: 5px solid coral;
            background-color: aquamarine;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
</html>三、 图片模糊处理 (filter)
filter: 将模糊或颜色偏移等图像效果应用于元素
语法:
filter: 函数()
如: filter: blur(5px);	/* blur 是模糊处理 数值越大越模糊 */详情
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 img {
            filter: blur(5px);
        }
        .box1 img:hover {
            filter: blur(0px);
        }
    </style>
</head>
<body>
    <div class="box1">
        <img src="https://img.pupper.cn/img/202111291344063.jpg" alt="">
    </div>
</body>
</html>四、 宽度计算 (calc 函数)
calc(): 可以对 css 属性值 进行一定的计算
calc(): 可以进行 + - * / 四则运算
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 300px;
            height: 200px;
            border: 1px solid #222;
            text-align: center;
            line-height:200px;
        }
        .box1 img {
            width: calc(100% - 20px);
            height: calc(100% - 20px);
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div class="box1">
        <img src="https://img.pupper.cn/img/202111291344063.jpg" alt="">
    </div>
</body>
</html>五、 动画过度 (transition)
transition: 从一个样式过度到另一个样式如果想要写多个属性,需要用
,逗号隔开
语法:
transition: 要过度的属性 花费的时间 运动曲线 何时开始;说明:
- 属性 : 想要变化的 css 属性,如: 宽度高度、背景颜色、内外边距等,如果想要所有属性都变化,用 all
- 花费时间: 单位为秒,如:0.5s
- 运动曲线: 默认为 ease (可以省略)
- 何时开始 : 单位为 秒,可以设置延时触发事件 (0 秒 可以省略)
详情
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>CSS3 过渡效果</title>
		<style>
			.box1 {
				margin: 50px auto;
				width: 200px;
				height: 100px;
				background-color: pink;
				/* transition: 变化的属性 花费时间 运动曲线 何时开始; */
				/* 如果想要写多个属性,利用逗号进行分割 */
				transition: width 0.5s, height 0.5s;
			}
			.box2 {
				margin: 50px auto;
				width: 200px;
				height: 100px;
				background-color: rgb(30, 218, 243);
				/* transition: 变化的属性 花费时间 运动曲线 何时开始; */
				/* 如果想要多个属性都变化,属性写all就可以了 */
				transition: all 0.5s;
			}
			.box1:hover {
				width: 400px;
				height: 200px;
				background-color: skyblue;
			}
			.box2:hover {
				width: 400px;
				height: 200px;
				background-color: rgb(75, 243, 9);
			}
		</style>
	</head>
	<body>
		<div class="box1">鼠标滑过,改变样式</div>
		<div class="box2">鼠标滑过,改变样式</div>
	</body>
</html>