css实现上中下布局(上下固定,中间自适应)

先上效果图:

在这里插入图片描述

实现方法:

方法一:

这个最简单,利用css中vh单位和calc() 函数

使用场景:

页面布局

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>css上中下布局</title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        
        .top {
            height: 100px;
            background: red;
        }
        
        .center {
            height: calc(100vh - 200px);
            background: yellow;
        }
        
        .bottom {
            height: 100px;
            background: red;
        }
    </style>
</head>

<body>
    <div class="top"></div>
    <div class="center"></div>
    <div class="bottom"></div>
</body>

</html>

方法二

利用css定位属性

使用场景:

一般用于某些元素根据屏幕大小适配,这个弹窗上下部分固定,中间部分需要自适应高度时

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>css上中下布局</title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        
        .top {
            position: absolute;
            top: 0;
            width: 100%;
            height: 100px;
            background: red;
        }
        
        .center {
            position: absolute;
            top: 100px;
            bottom: 100px;
            width: 100%;
            height: auto;
            background: yellow;
        }
        
        .bottom {
            position: absolute;
            height: 100px;
            width: 100%;
            bottom: 0;
            background: red;
        }
    </style>
</head>

<body>
    <div class="top"></div>
    <div class="center"></div>
    <div class="bottom"></div>
</body>

</html>

方法三

使用flex布局,这个时候父元素一定要给高度

使用场景:

同方法二

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>css上中下布局</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            /* 父元素一定要给高度 */
            height: 100vh; 
        }
        
        .top {
            width: 100%;
            height: 100px;
            background: red;
        }
        
        .center {
            width: 100%;
            flex: 1;
            background: yellow;
        }
        
        .bottom {
            width: 100%;
            height: 100px;
            background: red;
        }
    </style>
</head>

<body>
    <div class="top"></div>
    <div class="center"></div>
    <div class="bottom"></div>
</body>

</html>

应该还有很多实现的方法,目前我常用的就是这几种,有更好的方案的同学可以一起交流。

个人水平有限,有问题欢迎大家留言指导,仅供学习和参考。

学海无涯!努力二字,共勉!

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值