原因:.当一个元素是浮动元素的时候,他会失去对父元素的支撑。 位于浮动元素下的非浮动元素会显示到浮动元素的下面,非浮动元素中的文字会环绕在浮动元素周围,这并不是我们想要的。要想让该非浮动元素位于浮动元素之后(单独一行)需要清除浮动。
方法1:overflow:hidden
方法
<!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>浮动元素3</title>
<style>
.fu{
overflow: hidden;
}
.one,.two,.three{
height: 100px;
width: 300px;
background-color: cornflowerblue;
margin-bottom: 10px;
float: right;
}
.first{
float: right;
}
</style>
</head>
<body>
<div class="fu">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
<div class="first">hello</div>
</body>
</html>
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>浮动元素3</title>
<style>
.fu::before{
clear:both;
content: '';
display: block;
}
.one,.two,.three{
height: 100px;
width: 300px;
background-color: cornflowerblue;
margin-bottom: 10px;
float: right;
}
.first{
float: right;
}
</style>
</head>
<body>
<div class="first">hello</div>
<div class="fu">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
</body>
</html>