高度100%-固定值的问题

  html,body {
            height: 100%;
            padding: 0;
            margin: 0;
           /* height:100vh 或者这样*/
        }

vh的意思是把浏览器视口横向分为100份,100vh就是100%高度。

如果上方有个导航条,可以减去导航条高度,如:

height:calc(100vh - 60px)
height:calc(100% - 20px);

如果浏览器版本低,使用jQuery

function setSizes() {
   var containerHeight = $("#listContainer").height();
   $("#myList").height(containerHeight - 18);
}

我recalc绑定到它的窗口大小,浏览器,无论是工具窗口(如果容器的大小和窗口大小的变化)

$(window).resize(function() { setSizes(); });

古老浏览器的写法

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>高度自适应布局</title>
    <style>
        html,
        body {
            height: 100%;
            margin: 0;
            padding: 0;
            color: #F00;
        }
        .top {
            background: #36C;
            height: 100px;
        }
        .main {
            background: #F90;
            position: absolute;
            width: 100%;
            top: 100px;
            bottom: 0;
            overflow: auto;
        }
		/*for ie6*/
        * html {
            padding-top: 100px;
        }

        * html .top {
            background: #36C;
            height: 100px;
            position: absolute;
            top: 0;
            width: 100%;
        }

        * html .main {
            background: #F90;
            position: static;
            height: 100%;
        }
    </style>
</head>

<body>
    <div class="top">我是top,固定高度</div>
    <div class="main">我是main,高度随浏览器大小变化而变化<p style="height:500px;"></p>
    </div>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值