前端面试常考 | CSS垂直居中解决方案



一. 前言

前段时间刷到一篇帖子,说面试竟然遇到了CSS的考点,让回答CSS实现垂直居中的方式有哪些?,都2022年了还能遇到这种面试问题也是挺奇怪的,但是还是突发奇想想写这么一篇文章,文章只介绍到五种方案,但是还是有其它实现方法,感兴趣的小伙伴可以自行查阅相关文章。
下面我们来聊聊CSS中实现垂直居中的方案及其优劣性。

二. flx布局实现

使用flx布局可以轻松的实现水平居中垂直居中,这也是我们工作中使用的比较多的一种方式,后期项目迭代起来也不容易出现问题。
垂直居中

    <style>
        .wapper {
            width: 200px;
            height: 200px;
            background-color: antiquewhite;
            display: flex;
            align-items: center; /* 设置垂直居中 */
            justify-content: center; /* 设置水平居中 */
        }

        .box {
            width: 50px;
            height: 50px;
            background-color: aqua;
        }
    </style>
	
<body>
    <div class="wapper">
        <div class="box">
        </div>
    </div>
</body>

三. Grid布局实现

使用Grid布局也可以轻松的实现元素的水平居中和垂直居中,目录工作中用的也比较多,后期项目维护不容易出现问题,推荐使用。

    <style>
        .wapper {
            width: 200px;
            height: 200px;
            background-color: antiquewhite;
            display: grid;
        }

        .box {
            width: 50px;
            height: 50px;
            background-color: aqua;
            justify-self: center; /* 设置水平居中 */
            align-self: center; /* 设置垂直居中 */
        }
    </style>
 <body>
    <div class="wapper">
        <div class="box">
        </div>
    </div>
</body>

四. 绝对定位+transform

这是一种比较老的做法了,使用定位+transform,这种方式比较好的点就是不需要知道子元素和父元素的宽高。

    <style>
        .wapper {
            width: 200px;
            height: 200px;
            background-color: antiquewhite;
            position: relative;
        }

        .box {
            width: 50px;
            height: 50px;
            background-color: aqua;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
   </style>
 <body>
    <div class="wapper">
        <div class="box">
        </div>
    </div>
</body>

五. 绝对定位+负margin

这种方法需要知道子元素自身的宽高 margin-left margin-top 都需要设置为子元素自身宽高的一半

    <style>
        .wapper {
            width: 300px;
            height: 300px;
            background-color: antiquewhite;
            position: relative;
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }
    </style>
  <body>
    <div class="wapper">
        <div class="box">
        </div>
    </div>
</body>

五. 绝对定位+calc

这种方法也需要知道子元素自身的宽度,这其中的 50px就是子元素宽度的一半

    <style>
        .wapper {
            width: 300px;
            height: 300px;
            background-color: antiquewhite;
            position: relative;
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: absolute;
            left: calc(50% - 50px);
            top: calc(50% - 50px);
        }
    </style>
 <body>
    <div class="wapper">
        <div class="box">
        </div>
    </div>
</body>

六. 定位加margin

这种方法比较难理解,日常工作中还是不建议使用。

    <style>
        .wapper {
            width: 300px;
            height: 300px;
            background-color: antiquewhite;
            position: relative;
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
 <body>
    <div class="wapper">
        <div class="box">
        </div>
    </div>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值