- 浮动的最本质功能:用来实现并排。
float: left;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
div {
width: 100px;
height: 100px;
float: left;
}
#div1 {
background-color: red;
}
#div2 {
background-color: green;
}
#div3 {
background-color: blue;
}
</style>
</head>
<body>
<!-- 浮动实现并排 -->
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>
浮动的使用
- 浮动使用要点:要浮动,并排的盒子都要设置浮动。
- 父盒子要有足够的宽度,否则子盒子会掉下去。
其中一个盒子没有浮动的效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
div {
width: 100px;
height: 100px;
}
#div1 {
background-color: red;
float: left;
}
#div2 {
background-color: green;
}
#div3 {
background-color: blue;
float: left;
}
</style>
</head>
<body>
<!-- 浮动实现并排 -->
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>
父盒子宽度不足的效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
* {
padding: 0;
margin: 0;
}
.main {
width: 299px;
height: 100px;
border: 1px solid #000;
}
#div1 {
width: 100px;
height: 100px;
background-color: red;
float: left;
}
#div2 {
width: 100px;
height: 100px;
background-color: green;
float: left;
}
#div3 {
width: 100px;
height: 100px;
background-color: blue;
float: left;
}
</style>
</head>
<body>
<!-- 浮动实现并排 -->
<div class="main">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</div>
</body>
</html>
浮动的顺序贴靠特性
- 子盒子会按顺序进行贴靠,如果没有足够空间,则会寻找再前一个兄弟元素。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
* {
padding: 0;
margin: 0;
}
.main {
position: relative;
top: 10px;
left: 10px;
width: 300px;
height: 300px;
border: 1px solid #000;
}
#div1 {
width: 200px;
height: 200px;
background-color: red;
float: left;
}
#div2 {
width: 100px;
height: 100px;
background-color: green;
float: left;
}
#div3 {
width: 100px;
height: 150px;
background-color: blue;
float: left;
}
#div4 {
width: 100px;
height: 50px;
background-color: yellow;
float: left;
}
</style>
</head>
<body>
<!-- 浮动的顺序贴靠特性 -->
<div class="main">
<div id="div1">1</div>
<div id="div2">2</div>
<div id="div3">3</div>
<div id="div4">4</div>
</div>
</body>
</html>
浮动的元素一定能设置宽高
- 浮动的元素不再区分块级元素、行内元素,已经脱离了标准文档流,一律能够设置宽度和高度,即使它是span或者a标签等。
右浮动
float:right;/*即可设置右浮动*/
注意事项
- 垂直显示的盒子,不要设置浮动,只有并排显示的盒子才要设置浮动!
- “大盒子带着小盒子跑”,一个大盒子中,又是一个小天地,内部可以继续使用浮动。
使用浮动实现网页布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
* {
padding: 0;
margin: 0;
}
header {
width: 800px;
height: 100px;
margin: 0 auto;
border: 1px solid #000;
}
main {
width: 800px;
height: 600px;
margin: 10px auto;
border: 1px solid #000;
}
footer {
width: 800px;
height: 100px;
background-color: silver;
margin: 0 auto;
border: 1px solid #000;
}
header div.logo {
width: 200px;
height: 100px;
background-color: yellow;
float: left;
}
header div.r1 {
width: 200px;
height: 40px;
background-color: yellowgreen;
float: right;
}
header div.r2 {
width: 500px;
height: 40px;
background-color: blueviolet;
float: right;
margin-top: 20px;
}
main div.aside {
width: 250px;
height: 600px;
background-color: red;
float: left;
}
main div.content {
width: 500px;
height: 600px;
float: right;
}
main div.content div.cont1 {
width: 500px;
height: 500px;
background-color: rgb(75, 160, 230);
}
main div.content ul li {
list-style: none;
width: 100px;
height: 80px;
margin-top: 20px;
margin-right: 25px;
background-color: cyan;
float: left;
}
main div.content ul li:last-child {
width: 125px;
margin-right: 0;
float: left;
}
</style>
</head>
<body>
<!-- 使用浮动实现网页布局 -->
<header>
<div class="logo"></div>
<div class="r1"></div>
<div class="r2"></div>
</header>
<main>
<div class="aside"></div>
<div class="content">
<div class="cont1"></div>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</main>
<footer>
</footer>
</body>
</html>
BFC规范
- BFC(Box Formatting Context,块级格式化上下文)是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素,反之亦然。
从一个现象开始说起
- 一个盒子不设置height,当内容子元素都浮动时,无法撑起自身。
- 这个盒子没有形成BFC。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
* {
padding: 0;
margin: 0;
}
.main {
width: 600px;
border: 10px solid #000;
}
.v1 {
width: 300px;
height: 300px;
background-color: red;
float: left;
}
.v2 {
width: 300px;
height: 300px;
background-color: green;
float: left;
}
</style>
</head>
<body>
<div class="main">
<div class="v1"></div>
<div class="v2"></div>
</div>
</body>
</html>
如何创建BFC
- 方法①:float的值不是none。
- 方法②:position的值不是static或者relative。
- 方法③:display的值是inline-block、flex或者inline-flex。
- 方法④:overflow:hidden;
什么是overflow:hidden;
- overflow:hidden;表示溢出隐藏,溢出盒子边框的内容将会被隐藏。
- overflow:hidden;是非常好用的让盒子形成BFC的方法。
BFC的其他作用
- BFC可以取消盒子margin塌陷。
- BFC可以可以阻止元素被浮动元素覆盖 。
清除浮动
- 清除浮动:浮动一定要封闭到一个盒子中,否则就会对页面后续元素产生影响。
没有清除的效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
p {
float: left;
width: 100px;
height: 100px;
background-color: orange;
margin-right: 20px;
}
</style>
</head>
<body>
<div>
<p></p>
<p></p>
</div>
<div>
<p></p>
<p></p>
</div>
</body>
</html>
清除浮动方法1
- 清除浮动方法1:让内部有浮动的父盒子形成BFC,它就能关闭住内部的浮动。此时最好的方法就是overflow:hidden属性。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
p {
float: left;
width: 100px;
height: 100px;
background-color: orange;
margin-right: 20px;
}
</style>
</head>
<body>
<div>
<p></p>
<p></p>
</div>
<div>
<p></p>
<p></p>
</div>
</body>
</html>
清除浮动方法2
- 清除浮动方法2:给后面的父盒子设置clear:both属性。clear表示清除浮动对自己的影响,both表示左右浮动都清除。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
.v2 {
clear: both;
}
p {
float: left;
width: 100px;
height: 100px;
background-color: orange;
margin-right: 20px;
}
</style>
</head>
<body>
<div class="v1">
<p></p>
<p></p>
</div>
<div class="v2">
<p></p>
<p></p>
</div>
</body>
</html>
清除浮动方法3
- 清除浮动方法3:使用::after伪元素给盒子添加最后一个子元素,并且给::after设置clear:both。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
div::after {
/* 一定要添加content,注意要转为块级元素 */
content: "";
clear: both;
display: block;
}
p {
float: left;
width: 100px;
height: 100px;
background-color: orange;
margin-right: 20px;
}
</style>
</head>
<body>
<div class="v1">
<p></p>
<p></p>
</div>
<div class="v2">
<p></p>
<p></p>
</div>
</body>
</html>
清除浮动方法4
- 清除浮动方法4:在两个父盒子之间“隔墙”,隔一个携带clear:both的盒子。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First HTML Web</title>
<style>
p {
float: left;
width: 100px;
height: 100px;
background-color: orange;
margin-right: 20px;
}
.wall {
clear: both;
}
</style>
</head>
<body>
<div>
<p></p>
<p></p>
</div>
<div class="wall"></div>
<div>
<p></p>
<p></p>
</div>
</body>
</html>