css浮动和清除浮动
前言
从业前端有一段时间了,做过一些大小项目,回想当初自学前端,记得当初刚写代码的时候,那时候布局没有了解浮动(float),我常用margin的负值来达到自己想要的布局,所以出现很多问题,现在回想一下自己也是挺搞笑的,所以用我自己的理解来介绍一下浮动和清除浮动的作用,也是对自己知识体系有梳理作用, 也希望对大家有些许帮助。
以下是本篇文章正文内容,下面案例可供参考
一、什么是 CSS Float(浮动)?
1.CSS 的 Float(浮动)
CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列。浮动最初设计的目的就只是用来实现文字环绕效果而已。
浮动的特征就体现在:浮动元素会脱离文档流并向左/向右浮动,直到碰到父元素或者另一个浮动元素。此外,浮动带来的负效果也算是它的特征之一。浮动会脱离文档,脱离文档,也就是说浮动不会影响普通元素的布局
2.元素怎样浮动
元素的水平方向浮动,意味着元素只能左右移动而不能上下移动。
一个浮动元素会尽量向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
浮动元素之后的元素将围绕它。
浮动元素之前的元素将不会受到影响。
3.浮动的使用
没有浮动之前
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box-wrap{
width: 500px;
height: 200px;
background-color: black;
}
.box1{
width: 50px;
height: 50px;
background-color: green;
}
.box2{
width: 50px;
height: 50px;
background-color: red;
}
.box3{
width: 50px;
height: 50px;
background-color: blue;
}
</style>
</head>
<body>
<div class="box-wrap">
<div class="box">box1</div>
<div class="box">box2</div>
<div class="box">box3</div>
</div>
</body>
</html>
左浮动后
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
<!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-wrap{
width: 500px;
height: 200px;
background-color: black;
}
.box1{
width: 50px;
height: 50px;
background-color: green;
}
.box2{
width: 50px;
height: 50px;
background-color: red;
}
.box3{
width: 50px;
height: 50px;
background-color: blue;
}
.fl{
float: left;
}
</style>
</head>
<body>
<div class="box-wrap">
<div class="box1 fl">box1</div>
<div class="box2 fl">box2</div>
<div class="box3 fl">box3</div>
</div>
</body>
</html>
右浮动后
<!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-wrap{
width: 500px;
height: 200px;
background-color: black;
}
.box1{
width: 50px;
height: 50px;
background-color: green;
}
.box2{
width: 50px;
height: 50px;
background-color: red;
}
.box3{
width: 50px;
height: 50px;
background-color: blue;
}
.fr{
float: right;
}
</style>
</head>
<body>
<div class="box-wrap">
<div class="box1 fr">box1</div>
<div class="box2 fr">box2</div>
<div class="box3 fr">box3</div>
</div>
</body>
</html>
二、清除浮动
为啥要清除浮动,因为浮动带来很多布局的影响,使元素脱离普通流造成的父元素高度塌陷,下方非文本元素与浮动元素重叠,破坏了原本布局,所以我们要清理这些影响。
没有浮动前(示例):
<!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-wrap{
border: 5px solid pink;
}
.box{
width: 50px;
height: 50px;
background-color: green;
margin: 20px;
}
</style>
</head>
<body>
<div class="box-wrap">
<div class="box">box1</div>
<div class="box">box2</div>
<div class="box">box3</div>
</div>
</body>
</html>
浮动后(示例):
<!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-wrap{
border: 5px solid pink;
}
.box{
width: 50px;
height: 50px;
background-color: green;
margin: 20px;
float: left;
}
</style>
</head>
<body>
<div class="box-wrap">
<div class="box">box1</div>
<div class="box">box2</div>
<div class="box">box3</div>
</div>
</body>
</html>
浮动后的元素就脱离了文档流,并不占据文档流的位置,自然父元素也就不能被撑开,所以没了高度。
所以这个时候就要加清除浮动clear,clear属性不允许被清除浮动的元素的左边/右边挨着浮动元素,底层原理是在被清除浮动的元素上边或者下边添加足够的清除空间。要注意了,我们是通过在别的元素上清除浮动来实现撑开高度的, 而不是在浮动元素上。
清除浮动后(示例):
<!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-wrap{
border: 5px solid pink;
}
.box{
width: 50px;
height: 50px;
background-color: green;
margin: 20px;
float: left;
}
.clearfix:after {
display: block;
content: " ";
clear: both;
}
</style>
</head>
<body>
<div class="box-wrap clearfix">
<div class="box">box1</div>
<div class="box">box2</div>
<div class="box">box3</div>
</div>
</body>
</html>
这样高度塌陷就解决了
总结
浮动最初设计只是用来实现文字环绕排版的。
浮动的三个特点:
1.脱离文档流。
2.向左/向右浮动直到遇到父元素或者别的浮动元素。
3.浮动会导致父元素高度坍塌。
解决父元素高度坍塌的方式就是清除浮动,虽然清除浮动的方法不止一种,但推荐clearfix命名的方式,在父元素后面加伪元素来清除浮动。