一.常见的网页布局
代码
<!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>07常见的网页布局</title>
<style>
/* 清除内外边距 */
* {
margin: 0;
padding: 0;
}
.top {
height: 36px;
background-color: #ecedf8;
}
.banner {
width: 505px;
height: 94px;
background-color: #ecedf8;
/* 让盒子居中显示 */
margin: 10px auto;
/* margin-top: 10px; */
}
.box {
width: 502px;
height: 175px;
background-color: #ecedf8;
margin: 0 auto;
/* margin-top: 10px; */
}
/* 清除小圆点 */
li {
list-style: none;
}
.box li {
float: left;
width: 121px;
height: 175px;
background-color: pink;
margin-right: 6px;
}
/* 清除最后一个小盒子的右边距 */
.box .last {
margin-right: 0;
}
/* 只要是通栏的盒子(和浏览器一样宽) 不需要指定宽度 */
.footer {
height: 125px;
background-color: #ecedf8;
margin-top: 10px;
}
</style>
</head>
<body>
<!-- .top+.banner+.box+.footer -->
<div class="top">top</div>
<div class="banner">banner</div>
<div class="box">
<!-- ul>li*4{$} -->
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li class="last">4</li>
</ul>
</div>
<div class="footer">footer</div>
</body>
</html>
结果
二.浮动的两个注意点
代码
<!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>08-浮动的两个注意点</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
/* 如果一个子元素浮动了,尽量其他盒子也浮动,这样保证这些子元素一行显示 */
.box {
height: 500px;
background-color: palegreen;
margin: 20px auto;
}
.damao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.ermao {
float: left;
width: 300px;
height: 300px;
background-color: red;
}
.sanmao {
float: left;
width: 200px;
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<!-- .box>ul>li*3 -->
<div class="box">
<ul>
<li class="damao">大毛</li>
<li class="ermao">二毛</li>
<li class="sanmao">三毛</li>
</ul>
</div>
</body>
</html>
结果
三.清除浮动
清除浮动方法
(1)额外标签法也称为隔墙法,是W3C推荐的做法。
(2)父级添加overflow属性
(3)父级添加after伪元素
(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>09-为甚末要清除浮动</title>
<style>
.box {
width: 800px;
border: 5px solid blue;
margin: 20px auto;
}
.damao {
float: left;
width: 300px;
height: 300px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.footer {
height: 200px;
background-color: rgb(38, 207, 22);
}
</style>
</head>
<body>
<!-- .box>.damao{大毛}+.ermao{二毛} -->
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
结果
(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>10-清除浮动之额外标签法</title>
<style>
.box {
width: 800px;
border: 1px solid blue;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 300px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.clear {
clear: both;
}
.footer {
height: 200px;
background-color: rgb(38, 207, 22);
}
</style>
</head>
<body>
<!-- .box>.damao{大毛}+.ermao{二毛} -->
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<!-- 这个新增的盒子要求必须是块级元素不能是行内元素 -->
<div class="clear"></div>
<!-- span.clear -->
<!-- <span class="clear"></span> -->
</div>
<div class="footer"></div>
</body>
</html>
结果
(2)父级添加overflow属性
代码
<!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>03 清除浮动之overflow</title>
<style>
.box {
/* 清除浮动 */
overflow: hidden;
width: 800px;
border: 1px solid blue;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.footer {
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
结果
(3)父级添加after伪元素
代码
<!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>伪元素清除浮动</title>
<style>
/* 这段代码直接加上,然后让父盒子用 */
.clearfix:after {
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
/* IE6、7 专有 */
*zoom: 1;
}
.box {
width: 800px;
border: 1px solid blue;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.footer {
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div class="box clearfix">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
结果
(4)父级添加双伪元素
代码
<!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>伪元素清除浮动</title>
<style>
/* 还是把这段代码粘贴过来,然后给父盒子写类名 */
.clearfix:before,
.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
/* xxxxxxxxxxxxxxxx */
.box {
width: 800px;
border: 1px solid blue;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.footer {
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div class="box clearfix">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
结果