div {
width: 300px;
height: 300px;
border: 1px solid black;
}
html文件:
<body>
<div class="div1">div1div>
<div class="div2">div2div>
<div class="div3">div3div>
<div class="div4">div4div>
<div class="div5">div5div>
<div class="div6">div6div>
body>
div {
width: 300px;
height: 300px;
border: 1px solid black;
float: left;
}
html文件:
<body>
<div class="div1">div1div>
<div class="div2">div2div>
<div class="div3">div3div>
<div class="div4">div4div>
<div class="div5">div5div>
<div class="div6">div6div>
body>
div {
width: 300px;
height: 300px;
border: 1px solid black;
background-color: greenyellow;
}
.div1 {
width: 100px;
height: 100px;
background-color: red;
float: left;
}
html文件不变
效果:div1浮动在上方,覆盖在div2上,并且设置浮动之后,下方的文字会绕着浮动,因此,div2上的字没有被盖住
div {
width: 300px;
height: 300px;
border: 1px solid black;
background-color: greenyellow;
}
.div1,.div2{
width: 100px;
height: 100px;
background-color: red;
float: left;
}
.div3 {
clear: both;
}
html文件不变
效果:div1和div2为一行,div3另起一行,跟在后面
css文件:在上面的基础上再加一个右浮动
.div4 {
width: 100px;
height: 100px;
background-color: yellow;
float: right;
}
.div5 {
clear: both;
}
html文件不变
效果:div5没有清除浮动时,div5紧跟div3,清除浮动之后,会为div4让出位置,即div4独占一行
<div>
div>
我们可以使用伪元素的after来清除浮动
1.先将伪元素转变为真元素(添加内容content)
2.将after这个行元素转为块元素(独占一行,display)
3.清除浮动
css文件:
div {
border: 1px solid black;
background-color: greenyellow;
}
.div1,.div2,.div3,.div4,.div5,.div6{
width: 100px;
height: 100px;
background-color: red;
float: left;
}
.container::after {
content: "";
display: block;
clear: both;
}
html文件:
<body>
<div class="container">
<div class="div1">div1div>
<div class="div2">div2div>
<div class="div3">div3div>
<div class="div4">div4div>
<div class="div5">div5div>
<div class="div6">div6div>
div>
body>
div {
/* border: 1px solid black; */
background-color: greenyellow;
margin: 20px;
}
.div1 {
width: 100px;
height: 100px;
background-color: tomato;
}
.div2,.div3,.div4,.div5,.div6{
width: 100px;
height: 100px;
background-color: red;
float: left;
}
html文件:
<body>
<div class="container">
<div class="div1">div1div>
<div class="div2">div2div>
<div class="div3">div3div>
<div class="div4">div4div>
<div class="div5">div5div>
<div class="div6">div6div>
div>
body>
效果:div1本该与黄绿色的父级div有20px的margin,但此时父级div——"container"与body之间的边框 和 div1与container之间的间隔合在一起
.container::before {
content:"";
display:table;
}
.container::before,.container::after {
content:"";
display:table;
}
.container::after {
clear: both;
}