zoukankan      html  css  js  c++  java
  • DIV上中下布局高度自适应的研究[转]

    本文转自:http://blog.sina.com.cn/s/blog_4b8d35b70100mfy2.html

    一、背景

        在采用基于DIV+CSS的布局开发时,经常需要考虑各种浏览器版本的兼容性问题。

        常用的布局模式主要包括:左中右、上中下,以及两种模式的结合。

        在早期的开发,一般都采用Table标记的方式实现。

        当尝试采用基于DIV的模式,发现一切都变的似乎没那么简单了。特别是浏览器的兼容性问题,更加突出了。

    二、需求

        本文只讨论上中下布局模式的实现,关于左中右模式的实现,相比较来说要简单得多了。如果时间充,我会另文详述。

        1.上部(top)Div高度固定100px,宽度100%;

        2.下部(footer)Div高度固定100px,宽度100%;

        3.中部(middle)DIV高度根据屏幕高度,自适应充满,宽度100%;

        4.用纯DIV+CSS实现,不采用脚本计算高度的方式;

        5.调整浏览器大小时,中部DIV能随着屏幕高度自适应。

    三、HTML部分

        本部分非常简单,只是定义了三个DIV,分别对应:top、middle、footer

        <div id="header">
            抬头</div>
        <div id="middle">
            1页中<br />
            2页中<br />
            3页中<br />
            4页中<br />
            5页中<br />
            6页中<br />
            7页中<br />
            8页中<br />
            9页中<br />
        </div>
        <div id="footer">
            页脚

         </div>

    四、CSS实现

        为了便于理解实现原理,分两部分说明:

        1.IE6下的实现

    <style type="text/css">
    *{
     margin:0;
     padding:0;
    }
    html,body{
     padding:100px 0;   
     100%;
     height:100%;
     overflow:hidden;
    }
    #header{
     position:absolute;
     top:0;
     100%;
     height:100px;
     background:#ccc;
     line-height:100px;
     text-align:center;
    }
    #middle{
     position: relative;
     top:-100px; 
     height:100%;
      
     bottom:100px;
     100%;
     background:#ffc;
     text-align:center;
     overflow: auto;
    }
    #footer{
     position:absolute;
     bottom:0;
     100%;
     height:100px;
     background:#ccc;
     line-height:100px;
     text-align:center;
    }
    </style>   

        2.IE7、IE8下的实现

    <style type="text/css">
    *{
     margin:0;
     padding:0;
    }
    html,body{
     100%;
     height:100%;
     overflow:hidden;
    }
    #header{
     position:absolute;
     top:0;
     100%;
     height:100px;
     background:#ccc;
    }
    #middle{
     position: absolute;
     top:100px;
     height:auto;
     bottom:100px;

     100%;
     background:#ffc;
     text-align:center;
     overflow: auto;
    }
    #footer{
     position:absolute;
     bottom:0;
     100%;
     height:100px;
     background:#ccc;
     line-height:100px;
     text-align:center;
    }
    </style>

    五、全部CSS代码

    <style type="text/css">
    *{
     margin:0;
     padding:0;
    }
    html,body{
     
     padding:0 !important;
     
     padding:100px 0;
      
     100%;
     height:100%;
     overflow:hidden;
    }
    #header{
     position:absolute;
     top:0;
     100%;
     height:100px;
     background:#ccc;
     line-height:100px;
     text-align:center;
    }
    #middle{
     
     position: absolute!important;
     top:100px!important;
     height:auto!important;
     
     position: relative;
     top:-100px; 
     height:100%;
      
     bottom:100px;
     100%;
     background:#ffc;
     text-align:center;
     overflow: auto;
    }
    #footer{
     position:absolute;
     bottom:0;
     100%;
     height:100px;
     background:#ccc;
     line-height:100px;
     text-align:center;
    }
    </style>

  • 相关阅读:
    H5性能优化
    【JavaScript 】for 循环进化史
    (四十四)通过系统Gallery获取图片
    (四十三)获取图片exif信息
    (四十二)、加载大分辨率图片到内存
    (九)JAVA设计模式之单例模式
    (四十一)Activity切换动画
    (四十)android在代码中,如何设置自定义对话框在屏幕中的位置和大小
    (三十九)android动画 Animation四大属性 详解(转载:http://www.android100.org/html/201304/25/2295.html)
    (三十八)android:windowSoftInputMode属性详解
  • 原文地址:https://www.cnblogs.com/hejunrex/p/1985705.html
Copyright © 2011-2022 走看看