绝对定位和浮动的区别
程序员文章站
2022-03-02 15:33:01
...
float和absolute的区别
①float
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box1 {
float: left;
width: 100px;
height: 100px;
background-color: lightseagreen;
}
.box2 {
float: left;
width: 100px;
height: 100px;
background-color: lightgreen;
}
.box3 {
float: left;
width: 100px;
height: 100px;
background-color: green;
}
.box4 {
width: 100px;
height: 100px;
background-color: darkblue;
}
</style>
</head>
<body>
<div class="box1">qwqwqwqwaaaaaaaaaa</div>
<div class="box2">fgfgfgaaaaaaaaaa</div>
<div class="box3">vbvbvbvbaaaaaaaaaa</div>
<div class="box4"></div>
</body>
</html>
看效果
第四个盒子为什么多出来一点?原因是float只是脱离了文档流但是里面的文本不脱离文档流仍然在文档里占据位置影响别的元素
②absolute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box1 {
position: absolute;
width: 100px;
height: 100px;
background-color: lightseagreen;
}
.box2 {
position: absolute;
width: 100px;
height: 100px;
background-color: lightgreen;
}
.box3 {
position: absolute;
width: 100px;
height: 100px;
background-color: green;
}
.box4 {
width: 100px;
height: 100px;
background-color: darkblue;
}
</style>
</head>
<body>
<div class="box1">qwqwqwqwaaaaaaaaaa</div>
<div class="box2">fgfgfgaaaaaaaaaa</div>
<div class="box3">vbvbvbvbaaaaaaaaaa</div>
<div class="box4"></div>
</body>
</html>
看效果完完全全的在一起了说明元素一旦设置为absolute就没有一席之地了就会被别人骑在头上了
上一篇: HTML的浮动定位float简单使用