html中子div用了浮动怎样让父div的大小自动撑开(清除浮动)

时间:2025-04-17 21:35:43

浮动子div撑开父div的几种方法:

(1)在父div中在添加一个清除浮动的子div<div style=" clear:both;"></div>,该div不设置任何样式,只用来清除浮动

(2)在父div的css样式中设置overflow:hidden;zoom:1;

(3)设置父div也为浮动元素float:left;,这样设置的坏处是不能用margin:auto;实现居中

(4)设置父元素display:inline-block;,这样设置的坏处是不能用margin:auto;实现居中

(5)在父div中添加<br clear="both">子元素

(6)给父元素引用clearfix样式(未试过)

  clearfix{

    zoom:1;

  }

  clearfix:after{

    content:"";

    display:block;

    claer:both;

  }

小结:用哪一种方法根据自身情况来使用。

参考原文地址:http://www.jb51.net/css/173074.html

     http://www.jb51.net/article/43261.htm

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5 Canvas画印章</title>
<style>
.divCss{
position: relative;
width: 1024px;
height: auto;
border: 1px solid #999999;
margin:auto;
/*display:inline-block;*/
/*float: left;*/
/*overflow: hidden;
zoom:1;*/
}
.style{
position: relative;
width: 300px;
height: 400px;
float: left;
border: 1px solid #aaaaaa;
margin:10px;
}
</style>
</head>
<body>
<div class="divCss">
<div class="style"></div>
<div class="style"></div>
<div class="style"></div>
<div style=" clear:both;"></div>
</div>
</body>
</html>