两个DIV高度自适应方法汇总
1、js实现div自适应高度 代码如下:
<script type="text/javascript">
<!--
window.onload=window.onresize=function(){
if(document.getElementById("mm2").clientHeight<document.getElementById("mm1").clientHeight){
document.getElementById("mm2").style.height=document.getElementById("mm1").offsetHeight+"px";
}
else{
document.getElementById("mm1").style.height=document.getElementById("mm2").offsetHeight+"px";
}
}
-->
</script>
(注:网上公布了不少方法,但代码或多或少有错;上面的是无错代码;上面的代码在IE6.0/IE7.0下通过,并没有在opera和firefoxs下测试。)
2、纯CSS方法 css里代码(IE下测试通过,但不会显示div下边框,即border-bottom): /*左右自适应相同高度start*/ #m1,#m2 { padding-bottom: 32767px !important; margin-bottom: -32767px !important; } @media all and (min-width: 0px) { #m1,#m2 { padding-bottom: 0 !important; margin-bottom: 0 !important; } #m1:before, #m2:before { content: '[DO NOT LEAVE IT IS NOT REAL]'; display: block; background: inherit; padding-top: 32767px !important; margin-bottom: -32767px !important; height: 0; } } /*左右自适应相同高度end*/
3、加背景图片 这个方法,很多大网站在使用,如163,sina等。 XHTML代码:
浙公网安备 33010602011771号