几种css全屏布局的解决方案

上下定高满屏布局

此类布局在工作中是非常常见,常用于管理平台
这里写图片描述

1、absolute

<style>
    *{
        padding:0;
        margin:0;
    }
    html,body,.content{
        height:100%;
    }
    .top,.bottom{
        position: absolute;
        height: 50px;
        background-color: red;
        left: 0;
        right: 0;
    }
    .top{
        top:0;
    }
    .bottom{
        bottom:0;
    }
    .center{
        position: absolute;
        top: 50px;
        bottom: 50px;
        left: 0;
        right: 0;
        background-color: antiquewhite;
        overflow: auto;
    }
</style>
<div class="content">
    <div class="top">定高</div>
    <div class="center">自适应</div>
    <div class="bottom">定高</div>
</div>

2、函数calc()
calc() = calc(四则运算) #用于计算动态值
任何长度值都可以使用calc()函数进行计算;
calc()函数支持 “+”, “-“, “*”, “/” 运算;

calc 在低版本浏览器下存在兼容性问题:
这里写图片描述

<style>
    *{
        padding:0;
        margin:0;
    }
    html,body,.content{
        height:100%;
    }
    .top,.bottom{
        height:50px;
        background-color:red;
    }
    .center{
        height: calc(100% - 100px);;
        background-color: antiquewhite;
        overflow: auto;
    }
</style>
<div class="content">
    <div class="top">定高</div>
    <div class="center">自适应</div>
    <div class="bottom">定高</div>
</div>

3、flex
使用flex进行全局布局时,会因为性能问题出现,出现卡断。

<style>
    *{
        padding:0;
        margin:0;
    }
    html,body,.content{
        height:100%;
    }
    .content{
        display: flex;
        flex-direction: column;
    }
    .top,.bottom{
        height:50px;
        background-color:red;
    }
    .center{
        display: flex;
        flex: 1;
        background-color: antiquewhite;
        overflow: auto;
    }
</style>
<div class="content">
    <div class="top">定高</div>
    <div class="center">自适应</div>
    <div class="bottom">定高</div>
</div>

4、grid
使用display:grid把容器元素定义为一个网格,使用grid-template-columns和grid-template-rows设置列和行大小,然后使用grid-column 和 grid-row把它的子元素放入网格

<style>
    *{
        padding:0;
        margin:0;
    }
    html,body,.content{
        height:100%;
    }
    .content{
        display: grid;
        grid-template-rows: 50px auto 50px;
    }
    .top,.bottom{
        height:50px;
        background-color:red;
    }
    .center{
        overflow: auto;
        background-color: antiquewhite;
    }
</style>
<div class="content">
    <div class="top">定高</div>
    <div class="center">自适应</div>
    <div class="bottom">定高</div>
</div>

内容较少时,页面的footer始终显示在底部,内容超过一屏时,放在内容后面

常用于底部网站商标,备案号、页脚布局。
这里写图片描述

<style>
    *{
        padding:0;
        margin:0;
    }
    html,body{
        height:100%;
    }
    .page-container{
        min-height: 100%;
        position: relative;
    }
    .page-main{
        width: 100%;
        padding-bottom: 50px;
        height: auto;
        overflow: hidden;
    }
    .footer{
        position: absolute;
        bottom: 0;
        left: 0;
        right:0;
        height:50px;
        background-color:red;
    }
</style>
<div class="page-container">
    <div class="page-main"></div>
    <!--<div style="height:2000px;">内容过多时</div>--> 
    <div class="footer"></div>
</div>
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值