浮动属性
<!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>Document</title>
<style>
div{
width: 100px;
height: 100px;
}
.red{
background-color: red;
float: left;
}
.green{
width: 500px;
background-color: green;
/* float: left; */
}
.blue{
background-color: blue;
/* float: left; */
}
</style>
</head>
<body>
<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>
</body>
</html>
如果三个方框都设置浮动属性,则就并排显示
设置多个div浮动时,一行排不满就换另一行
右浮动
见缝插针原则
<!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>Document</title>
<!-- 见缝插针原则 -->
<style>
div{
width: 100px;
height: 100px;
}
.red{
width: 800px;
height: 800px;
background-color: red;
float: left;
}
.green{
width: 600px;
height: 600px;
background-color: green;
float: left;
}
.blue{
width: 300px;
height: 300px;
background-color: blue;
float: left;
}
</style>
</head>
<body>
<div class="red"></div>
<div class="green"></div>
<div class="blue"></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>Document</title>
<style>
div{
width: 100px;
height: 100px;
}
.red{
background-color: red;
float: left;
}
.green{
width: 300px;
height: 300px;
background-color: green;
/* float: left; */
}
.blue{
background-color: blue;
/* float: left; */
}
</style>
</head>
<body>
<div class="red"></div>
<div class="green">大家好你好</div>
<div class="blue"></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>Document</title>
<style>
.box1,.box2{
width: 200px;
height: 200px;
float: left;
}
.box1{
background-color: yellow;
}
.box2{
background-color: blue;
}
.box{
width: 300px;
height: 300px;
background-color: red;
clear: left;
}
.container{
overflow: hidden;
}
/*
1.写固定高度
2.清浮动 clear:none/right/left/both
3.当前浮动元素后补一个div,不设置宽高,clear:both
4.overflow;hidden
*/
</style>
</head>
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
<!-- <div style="clear: both;"></div> -->
</div>
<div class="box"></div>
</body>
</html>