0基础学前端-----CSS DAY9
视频参考:B站Pink老师
今天是CSS学习的第九天,今天开始的笔记对应Pink老师课程中的CSS第四天的内容。
本节重点:常见网页布局以及清除浮动
2. 常见网页布局
2.1 常见网页布局
有以下三种:

参考代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.top {
height: 50px;
background-color: gray;
}
.banner {
width: 980px;
height: 150px;
background-color: gray;
margin: 0 auto;
margin: 10px auto;
}
.box {
width: 980px;
margin: 0 auto;
height: 300px;
background-color: pink;
}
.box li {
float: left;
width: 237px;
height: 300px;
background-color: gray;
margin-right: 10px;
}
.box .last {
margin-right: 0;
}
/*只要是通栏的盒子(和浏览器一样宽)不需要指定宽度*/
.footer {
height: 200px;
background-color: gray;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="top">top</div>
<div class="banner">banner</div>
<div class="box">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li class="last">4</li>
</ul>
</div>
<div class="footer">footer</div>
</body>
</html>
这里展示第三种常见页面布局。
结果展示:

2.2 浮动布局注意点
- 浮动的标准流的父盒子搭配
先用标准流的父元素排列上下位置,之后内部子元素采取浮动排列左右位置。 - 一个元素浮动了,理论上其余的兄弟元素也要浮动
一个盒子里面有多个盒子,如果其中一个盒子浮动了,那么其他兄弟也应该浮动,以防引起问题
浮动的盒子只会影响浮动盒子后面的标准流,不会影响前面的标准流(所以结果展示中三毛的位置是由二毛的位置决定的,结果中的二毛在大毛下面,因为二毛没浮动)
参考代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
height: 500px;
background-color: pink;
}
.damao {
float: left;
width: 200px;
height: 200px;
background-color: purple;
}
.ermao {
width: 200px;
height: 150px;
background-color: red;
/* float: left; */
}
.sanmao {
float: left;
width: 300px;
height: 240px;
background-color: blue;
/*1,3浮动,此时三毛贴着二毛的下沿浮动*/
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
<div class="sanmao">三毛</div>
</div>
</body>
</html>
结果展示:

3. 清除浮动
3.1 为什么要清除浮动?
由于父级盒子很多情况下,不方便给高度,但是子盒子浮动又不占有位置,最后父级盒子高度为0时,就会影响下面的标准流盒子。
由于浮动元素不再占用原文档流的位置,所以他会对后面的元素排版产生影响。
参考代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 800px;
border: 1px solid blue;
margin: 0 auto;
}
.damao {
width: 300px;
height: 200px;
background-color: purple;
float: left;
}
.ermao {
width: 200px;
height: 200px;
background-color: pink;
float: left;
}
.footer {
background-color: black;
height: 200px;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer">footer</div>
</body>
</html>
结果展示:

比如这里box中蓝色的部分高度已经为0,变成一条线了。
3.2 清除浮动本质
- 本质是清除浮动元素造成的影响
- 如果父盒子本身有高度,则无需清除浮动
- 清除浮动后,父级就会根据浮动的子盒子自动检测高度。父级有了高度,就不会影响下面的标准流了
3.3 清除浮动
语法:选择器{clear: 属性值;}
| 属性值 | 描述 |
|---|---|
| left | 不允许左侧有浮动元素 |
| right | 不允许右侧有浮动元素 |
| both | 同时清除左右两侧浮动影响 |
实际工作中,几乎只使用clear: both;
清除浮动的策略是:闭合浮动。
方法:
-
额外标签法(隔墙法)-----W3C推荐的做法
-
父级添加overflow属性
-
父级添加after伪元素
-
父级添加双伪元素
接下来分别介绍这四种方法:
方法1:额外标签法(隔墙法)-----W3C推荐的做法,但不常用
额外标签法会在浮动元素末尾添加一个空标签 。例如:<div style="clear:both"></div>或其他标签(如<br/>等)
这里提及的空标签必须为块级元素!!! -
优点:通俗易懂,方便书写
-
缺点:添加许多无意义的标签,结构化较差
参考代码:Document