开发者社区 问答 正文

html高度自适应

不借助javascript,不使用绝对定位,不用overflow:hidden将溢出部分隐藏,如何使div的高度自适应?
不需要考虑低版本浏览器。
不需要考虑低版本浏览器。

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style type="text/css">
        html,body         {width: 100%;height: 100%;}
        </style>
    </head>
    <body>
        <div style="height:100px">top</div>
        <div>middle</div>
        <div style="height:100px">bottom</div>
    </body>
    </html>

比如上面的middle,如何让它的高度铺满屏幕剩余的空间?

展开
收起
杨冬芳 2016-06-06 17:31:01 3490 分享 版权
1 条回答
写回答
取消 提交回答
  • IT从业

    用flex布局

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style type="text/css">
        html,
        body {
            width: 100%;
            height: 100%;
        }
        body {
            display: -webkit-flex;
            -webkit-flex-direction:column;
        }
        .flex1 {
            -webkit-flex: 1;
        }
        </style>
    </head>
    
    <body>
        <div style="height:100px" >top</div>
        <div class="flex1">middle</div>
        <div style="height:100px">bottom</div>
    </body>
    

    使用table-row布局

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style type="text/css">
        html,
        body {
            width: 100%;
            height: 100%;
            padding: 0;
            margin: 0;
        }
        body {
           display: table;
        }
        .one {
            background: red;
        }
        .two {
            background: blue;
        }
        .three {
            background: green;
        }
        div {
            display: table-row;
        }
        </style>
    </head>
    
    <body>
        <div style="height:100px" class="one" >top</div>
        <div class="two">middle</div>
        <div style="height:100px" class="three">bottom</div>
    </body>
    
    </html>
    2019-07-17 19:29:06
    赞同 展开评论
问答分类:
问答标签:
问答地址: