fixed实现div居中

前端开发中,有时候既要实现一个div  fixed固定定位,又要实现该div居中且div里的内容居中,实现方法如下:

在div的css为:

.fixed-c {
            height: 80px;
            width: 90%;
            position: fixed;
            margin: auto;
            bottom: 0; /*底部固定*/
            left: 0; /*实现div的居中*/
            right: 0; /*实现div的居中*/
            text-align: center; /*div的内容居中*/
            background: red;
            line-height: 80px; /*垂直居中*/
        }

效果图如下:

fixed底部固定,且内容居中图片

 

原代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>fixed居中</title>
    <style>
        body{
            padding: 0;
            margin: 0;
            font-size: 22px;
        }
        .content-left{
            width: 256px;
            min-width: 256px;
            max-width: 256px;
            flex: 0 0 256px;
            overflow: hidden;
            height:100vh;
            background: orange;
        }
        .content-right{
            height: 900px;

        }
        .fixed-c {
            height: 80px;
            width: 90%;
            position: fixed;
            margin: auto;
            bottom: 0; /*底部固定*/
            left: 0; /*实现div的居中*/
            right: 0; /*实现div的居中*/
            text-align: center; /*div的内容居中*/
            background: red;
            line-height: 80px; /*垂直居中*/
        }
    </style>
</head>
<body>
    <div style="display:flex">
        <div class="content-left">
        左侧任意内容
        </div>
        <div style="flex: auto">
            <div class="content-right">
                右侧任意内容
            </div>
        </div>
        <div class="fixed-c">
            fixed底部固定,且要居中
        </div>
    </div>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值