且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

css多个div浮动float高度自适应(自增)

更新时间:2021-12-16 05:56:43

css多个div float并排,高度都自适应(自增)

采用 Div + CSS 进行三列或二列布局时,要使两列(或三列)的高度相同,用 Table 很容易实现,但采用 Div + CSS 就显得比较麻烦了。按照一般的做法,大都采用背景图填充或 JS 脚本的方法使高度相同。

css多个div浮动float高度自适应(自增)

方法一:纯css解决办法(“隐藏容器溢出”和“正内补丁”和“负外补丁”结合的方法):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<style type="text/css">
<!--
#wrap{overflow:hidden;}
#sidebar_left,#sidebar_right{padding-bottom:100000px;margin-bottom:-100000px;}
-->
</style>
<div id="wrap" style="width:300px; background:#FFFF00;">
    <div id="sidebar_left" style="float:left;width:100px; height:1000px; background:#FF0000;">Left</div>
    <div id="sidebar_mid" style="float:left;width:100px; background:#666;">
    Middle<br />
    Middle<br />
    Middle<br />
    Middle<br />
    Middle<br />
    Middle<br />
    Middle<br />
    Middle<br />
    Middle<br />
    </div>
    <div id="sidebar_right" style="float:right;width:100px; height:500px; background:#0000FF;">Right</div>
</div>


方法二:js解决办法(思路,此方法不推荐):

1
2
3
4
5
6
<script>
var a=Math.max(document.getElementById("left").offsetHeight,document.getElementById("center").offsetHeight,document.getElementById("right").offsetHeight);  //获取3个div的最大高度
document.getElementById("left").style.height = a + "px";
document.getElementById("center").style.height = a + "px";
document.getElementById("right").style.height = a + "px";
</script>





      本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1252383,如需转载请自行联系原作者