子元素浮动会导致父元素盒子无法被撑开,导致父元素的样式无法显示,以下介绍几种清除浮动的方法
原代码:
html>
<meta>
<title></title>
<style>
#content{
border: 1px red solid;
}
.fl{
border: 1px blueviolet solid;
height: 100px;
width: 100px;
float: left;
}
.fr{
border: 1px green solid;
height: 200px;
width: 200px;
float: right;
}
</style><p>
</p><p>内容一</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/js/7891" title="Html5图片悬浮效果代码"><img
src="https://img.php.cn/upload/jscode/000/000/014/170642194112432.jpg" alt="Html5图片悬浮效果代码" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/js/7891" title="Html5图片悬浮效果代码" class="overflowclass">Html5图片悬浮效果代码</a>
<p class="overflowclass">html5图片悬浮效果代码,图像浮动代码,图片网格布局,网页中的图像会跟随鼠标移动产生上下左右浮动效果,适合图片产品展示用途。</p>
</div>
<a rel="nofollow" href="/xiazai/js/7891" title="Html5图片悬浮效果代码" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<p>内容二</p>
显示如下:

1、设置父元素高度:
height: 500px; /*设置父元素高度*/
html>
<meta>
<title></title>
<style>
#content{
border: 1px red solid;
height: 500px; /*设置父元素高度*/
}
.fl{
border: 1px blueviolet solid;
height: 100px;
width: 100px;
float: left;
}
.fr{
border: 1px green solid;
height: 200px;
width: 200px;
float: right;
}
</style><p>
</p><p>内容一</p>
<p>内容二</p>
2、父元素绝对定位:position:absolute;
html>
<meta>
<title></title>
<style>
#content{
border: 1px red solid;
position: absolute; /*父元素绝对定位*/ }
.fl{
border: 1px blueviolet solid;
height: 100px;
width: 100px;
float: left;
}
.fr{
border: 1px green solid;
height: 200px;
width: 200px;
float: right;
}
</style><p>
</p><p>内容一</p>
<p>内容二</p>
3、父元素设置overflow:hidden
html>
<meta>
<title></title>
<style>
#content{
border: 1px red solid;
overflow: hidden;
}
.fl{
border: 1px blueviolet solid;
height: 100px;
width: 100px;
float: left;
}
.fr{
border: 1px green solid;
height: 200px;
width: 200px;
float: right;
}
</style><p>
</p><p>内容一</p>
<p>内容二</p>
4、父元素设置浮动:float:left/right
html>
<meta>
<title></title>
<style>
#content{
border: 1px red solid;
float: left;
}
.fl{
border: 1px blueviolet solid;
height: 100px;
width: 100px;
float: left;
}
.fr{
border: 1px green solid;
height: 200px;
width: 200px;
float: right;
}
</style><p>
</p><p>内容一</p>
<p>内容二</p>
5、在子元素最后添加一个空盒子,并设置样式为clear:both;
html>
<meta>
<title></title>
<style>
#content{
border: 1px red solid;
}
.fl{
border: 1px blueviolet solid;
height: 100px;
width: 100px;
float: left;
}
.fr{
border: 1px green solid;
height: 200px;
width: 200px;
float: right;
}
.clear{
clear: both;
}
</style><p>
</p><p>内容一</p>
<p>内容二</p>
<p></p>
6、在父元素样式上添加一个伪类,相当于在子元素最后添加一个空盒子,原理与5类似
html>
<meta>
<title></title>
<style>
#content{
border: 1px red solid;
}
.fl{
border: 1px blueviolet solid;
height: 100px;
width: 100px;
float: left;
}
.fr{
border: 1px green solid;
height: 200px;
width: 200px;
float: right;
}
#content:after{
content: '';
display: block;
/!*height: 0;*!/
clear: both;
}
</style><p>
</p><p>内容一</p>
<p>内容二</p>










