浮动
1、结构伪类选择器
1、作用:
- 根据元素在HTML中的结构关系查找元素
- 减少对于HTML中类的依赖,有利于保持代码整洁
- 常用于查找父级选择器中的子元素
2、选择器:
选择器 | 说明 |
---|---|
E:first-child {} | 匹配父元素中的第一个子元素,并且是E元素 |
E:last-child {} | 匹配父元素中的最后一个子元素,并且是E元素 |
E:nth-child(n) {} | 匹配父元素中的第n个子元素,并且是E元素 |
E:nth-last-child(n) {} | 匹配父元素中的倒数第n个子元素,并且是E元素 |
<!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>结构伪类</title>
<style>
li:first-child {
/* 找到第一个 */
background-color: red;
}
li:last-child {
/* 找到最后一个 */
background-color: green;
}
li:nth-child(3) {
/* 找到第n个 */
background-color: blue;
}
li:nth-last-child(5) {
/* 找到倒数第n个 */
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>
<li>第9个元素</li>
<li>第10个元素</li>
<li>第11个元素</li>
<li>第12个元素</li>
<li>第13个元素</li>
</ul>
</body>
</html>
3、n可以是0,、1、2、3、…,也可以是公式
功能 | 公式 |
---|---|
偶数 | 2n、even |
奇数 | 2n+1、2n-1、odd |
找到前五个 | -n+5 |
找到第五个之后的 | n+5 |
找到4的倍数 | 4n |
<!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>结构伪类</title>
<style>
li:nth-child(even) {
/* 找到偶数元素:2n/even */
background-color: red;
}
li:nth-child(odd) {
/* 找到奇数元素:2n+1/2n-1/odd */
background-color: blue;
}
li:nth-child(4n) {
/* 找到4的倍数 */
background-color: skyblue;
}
li:nth-child(-n+5) {
/*
找到前五个
-n+10-->找到前10个
*/
background-color: pink;
}
li:nth-child(n+5) {
/*
找到第5个之后的,包括第五个
n+10-->找到第10个之后的
*/
background-color: orange;
}
</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>
<li>第9个元素</li>
<li>第10个元素</li>
<li>第11个元素</li>
<li>第12个元素</li>
<li>第13个元素</li>
</ul>
</body>
</html>
4、nth-of-type结构伪类选择器
选择器 | 说明 |
---|---|
E:nth-of-type(n) {} | 只在父元素的同类型(E)子元素范围内,匹配第n个 |
与:nth-child的区别:
- :nth-child → 直接在所有孩子中数个数
- :nth-of-type → 先通过该 类型 找到符合的一堆子元素,然后在这一堆子元素中数个数
<!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>结构伪类</title>
<style>
li:nth-child(3) {
background-color: red;
}
li:nth-of-type(2) {
background-color: orange;
}
</style>
</head>
<body>
<ul>
<li>第1个元素</li>
<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>
<li>第2个元素,我变橘</li>
<li>第3个元素</li>
<li>第4个元素</li>
<li>第5个元素</li>
<li>第6个元素</li>
</ul>
</body>
</html>
2、伪元素
1、伪元素:一般页面中的非主体内容可以使用伪元素
2、特点:是CSS模拟出的标签效果
3、种类
伪元素 | 作用 |
---|---|
::before | 在父元素内容的最前面添加一个伪元素 |
::after | 在父元素内容的最后面添加一个伪元素 |
4、注意点:
- 必须设置content属性才能生效
- 伪元素默认是行内元素
5、代码示例:
<!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>
.father {
color: red;
width: 300px;
height: 300px;
background-color: orchid;
}
.father::before {
/* content属性必须添加, 否则伪元素不生效 */
content: '我是伪元素最前面的内容';
color: pink;
/* 默认行内元素,宽高不生效,设置为块元素 */
display: block;
width: 100px;
height: 100px;
background-color: skyblue;
}
.father::after {
content: '在不在';
color: yellow;
}
</style>
</head>
<body>
<div class="father">div标签</div>
</body>
</html>
3、标准流
1、标准流:又称文档流,是浏览器在渲染显示网页内容时默认采用的一套排版规则,规定了应该以何种方式排列元素
2、 常见标准流排版规则:
- 块级元素:从上往下,垂直布局,独占一行
- 行内元素 或 行内块元素:从左往右,水平布局,空间不够自动折行
4、浮动
1、属性名:float
2、属性值:
属性名 | 效果 |
---|---|
left | 左浮动 |
right | 右浮动 |
3、浮动特点:
-
浮动元素会脱离标准流(简称:脱标),在标准流中不占位置,相当于从地面飘到了空中
-
浮动元素比标准流高半个级别,可以覆盖标准流中的元素
-
浮动找浮动,下一个浮动元素会在上一个浮动元素后面左右浮动
-
浮动元素有特殊的显示效果:一行可以显示多个、可以设置宽高
-
浮动的元素不能通过text-align:center或者margin:0 auto
4、浏览器解析行内块或行内标签的时候, 如果标签换行书写会产生一个空格的距离
<!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>
/*
浏览器解析行内块或行内标签的时候,
如果标签换行书写会产生一个空格的距离
*/
div {
display: inline-block;
height: 100px;
width: 100px;
}
.one {
background-color: pink;
}
.two {
background-color: skyblue;
}
</style>
</head>
<body>
<div class="one">one</div>
<div class="two">two</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2kJtVFyU-1647147833521)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1647139084902.png)]
5、使用浮动的效果
<!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>
div {
height: 100px;
width: 100px;
}
.one {
background-color: pink;
float: left;
}
.two {
background-color: skyblue;
float: left;
}
</style>
</head>
<body>
<div class="one">one</div>
<div class="two">two</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ll9IaGmH-1647147833522)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1647139196445.png)]
6、浮动特点:
<!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>
/* 浮动: 在一行排列, 宽高生效 -- 浮动后的标签具备行内块特点 */
.one {
width: 100px;
height: 100px;
background-color: pink;
float: left;
margin-top: 50px;
}
.two {
width: 200px;
height: 200px;
background-color: red;
float: left;
/* 浮动之后不会生效 */
margin: 0 auto;
}
.three {
width: 300px;
height: 300px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="one">one</div>
<div class="two">two</div>
<div class="three">three</div>
</body>
</html>
7、网页导航书写步骤
-
清除默认的margin和padding
-
找到ul,去除小圆点
-
找到li标签,设置浮动让li一行中显示
-
找到a标签,设置宽高 → a标签默认是行内元素,默认不能设置宽高??
- 方法一:给a标签设置 display : inline-block
- 方法二:给a标签设置 display : block
- 方法三:给a设置 float : left
5、清除浮动
1、介绍
1、含义:清除浮动带来的影响
- 影响:如果子元素浮动了,此时子元素不能撑开标准流的块级父元素
2、原因:子元素浮动之后脱离标准流,不再占用位置
3、目的:需要父元素有高度,从而不影响其他网页元素的布局
<!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>
.top {
margin: 0 auto;
width: 1000px;
/* height: 300px; */
background-color: pink;
}
.bottom {
height: 100px;
background-color: green;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right {
float: right;
width: 790px;
height: 300px;
background-color: skyblue;
}
</style>
</head>
<body>
<!-- 父子级标签, 子级浮动, 父级没有高度, 后面的标准流盒子会受影响, 显示到上面的位置 -->
<div class="top">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
2、清除浮动的方法1:直接设置父元素的高度
1、特点:方便,但是有些布局中不能固定父元素高度,比如新闻列表等等
2、代码示例:
<!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>
.top {
margin: 0 auto;
width: 1000px;
background-color: pink;
/*方法1:给父元素设置高度*/
height: 300px;
}
.bottom {
height: 100px;
background-color: rebeccapurple;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: bisque;
}
.right {
float: left;
width: 200px;
height: 300px;
background-color: burlywood;
}
</style>
</head>
<body>
<!--
父子级标签, 子级浮动, 父级没有高度,
后面的标准流盒子会受影响, 显示到上面的位置
-->
<div class="top">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
3、清除浮动的方法2:额外标签法
1、步骤:
- 在父元素内容的最后添加一个块级元素
- 给添加的块级元素设置clear:both
2、特点:会在页面中添加额外标签,让页面中的html结构变复杂
3、代码示例:
<!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>
.top {
margin: 0 auto;
width: 1000px;
background-color: pink;
}
.bottom {
height: 100px;
background-color: rebeccapurple;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: bisque;
}
.right {
float: left;
width: 200px;
height: 300px;
background-color: burlywood;
}
.clearfix {
clear: both;
}
</style>
</head>
<body>
<!--
父子级标签, 子级浮动, 父级没有高度,
后面的标准流盒子会受影响, 显示到上面的位置
-->
<div class="top">
<div class="left"></div>
<div class="right"></div>
<!-- 方法2:添加额外标签,设置clear:both -->
<div class="clearfix"></div>
</div>
<div class="bottom"></div>
</body>
</html>
4、清除浮动的方法3:单伪元素清除法
1、使用伪元素替代额外元素
//方法1
.clearfix::after {
content:'';
display:block;
clear:both;
}
//方法2
.clearfix::after {
contebt:'';
display:block;
clear:both;
/*补充代码,在网页中看不到伪元素*/
height:0;
visibility:hidden;
}
2、特点:项目中优先使用,直接给标签加类即可清除浮动
3、代码示例:
<!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>
.top {
margin: 0 auto;
width: 1000px;
background-color: pink;
}
.bottom {
height: 100px;
background-color: rebeccapurple;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: bisque;
}
.right {
float: left;
width: 200px;
height: 300px;
background-color: burlywood;
}
/* 单伪元素清除浮动 和 额外标签法原理是一样的 */
.clearfix::after {
content: '';
/* 伪元素是行内元素,要求设置为块级元素 */
display: block;
clear: both;
/* 为了兼容性,可以设置如下属性 */
height: 0;
visibility: hidden;
}
</style>
</head>
<body>
<!--
父子级标签, 子级浮动, 父级没有高度,
后面的标准流盒子会受影响, 显示到上面的位置
-->
<div class="top clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
5、清除浮动的方法4:双伪元素清除法
1、操作代码:
.clearfix::bofore,
.clearfix::after {
content:'';
display:table;
}
.clearfix::after {
clear:both;
}
2、特点:项目中优先使用,直接给标签加类即可清除浮动
3、示例代码:
<!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>
.top {
margin: 0 auto;
width: 1000px;
background-color: pink;
}
.bottom {
height: 100px;
background-color: rebeccapurple;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: bisque;
}
.right {
float: left;
width: 200px;
height: 300px;
background-color: burlywood;
}
/*
clearfix::before作用:解决外边距塌陷问题
外边距塌陷:父子标签都是块级元素,
子标签设置margin影响父元素的位置
*/
/* 清除浮动 */
.clearfix::before,
.clearfix::after {
content: '';
display: table;
}
/* 真正清除浮动 */
.clearfix::after {
clear: both;
}
</style>
</head>
<body>
<!--
父子级标签, 子级浮动, 父级没有高度,
后面的标准流盒子会受影响, 显示到上面的位置
-->
<div class="top clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
6、清除浮动的方法5:给父元素设置overflow:hidden
1、操作步骤:直接给父元素设置 overflow : hidden
2、特点:方便
3、示例代码
<!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>
.top {
margin: 0 auto;
width: 1000px;
background-color: pink;
/* 父元素设置:overflow: hidden; */
overflow: hidden;
}
.bottom {
height: 100px;
background-color: rebeccapurple;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: bisque;
}
.right {
float: left;
width: 200px;
height: 300px;
background-color: burlywood;
}
</style>
</head>
<body>
<!--
父子级标签, 子级浮动, 父级没有高度,
后面的标准流盒子会受影响, 显示到上面的位置
-->
<div class="top clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
.bottom {
height: 100px;
background-color: rebeccapurple;
}
.left {
float: left;
width: 200px;
height: 300px;
background-color: bisque;
}
.right {
float: left;
width: 200px;
height: 300px;
background-color: burlywood;
}
</style>