小白前端开发笔记-前端面试必备--元素的水平垂直居中(超级全)

元素水平垂直居中

本文对元素的水平垂直居中通过两方面来呈现,每一方面又分为元素宽高已知和未知,以上代码均通过测试,可直接使用,如有缺陷欢迎指正!

相对于父元素(宽高已知)

用右侧目录点击观看体验更佳

1.1 margin硬挤

<style>
        .a1;
            border: 1px solid red;{
            width: 600px;
            height: 600px solid red;
        }

        .a2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            margin-left: 250px;
            margin-top: 250px;
        }
</style>
<body>
    <div class="a1">
        <div class="a2"></div>
    </div>
</body>

1.2 text-align+vertical-align

<style>
        .a1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            line-height: 600px;
            text-align: center;
        }

        .a2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            vertical-align: middle;
            display: inline-block;
            /* margin-left: 250px; */
            /* margin-top: 250px; */
        }
    </style>

1.3.1 position+margin

<style>
        .a1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            position: relative;
            /* line-height: 600px; */
            /* text-align: center; */
        }

        .a2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            top: 0px;
            right: 0px;
            left: 0px;
            bottom: 0px;
            margin: auto;
            /* vertical-align: middle; */
            /* display: inline-block; */
            /* margin-left: 250px; */
            /* margin-top: 250px; */
        }
    </style>

1.3.2 positon+calc函数

<style>
        .a1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            position: relative;
            /* line-height: 600px; */
            /* text-align: center; */
        }

        .a2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            top: calc(50% - 100px / 2);
            /* right: 0px; */
            left: calc(50% - 100px / 2);
            /* bottom: 0px; */
            /* margin: auto; */
            /* vertical-align: middle; */
            /* display: inline-block; */
            /* margin-left: 250px; */
            /* margin-top: 250px; */
        }
    </style>

1.3.3 position+margin-top

<style>
        .a1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            position: relative;
            /* line-height: 600px; */
            /* text-align: center; */
        }

        .a2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            margin-top: -50px;
            top: 50%;
            margin-left: -50px;
            left: 50%;
            /* top: calc(50% - 100px / 2); */
            /* right: 0px; */
            /* left: calc(50% - 100px / 2); */
            /* bottom: 0px; */
            /* margin: auto; */
            /* vertical-align: middle; */
            /* display: inline-block; */
            /* margin-left: 250px; */
            /* margin-top: 250px; */
        }
    </style>

1.3.4 position+translate

<style>
        .a1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            position: relative;
            /* line-height: 600px; */
            /* text-align: center; */
        }

        .a2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            transform: translate(-50%, -50%);
            /* margin-top: -50px; */
            top: 50%;
            /* margin-left: -50px; */
            left: 50%;
            /* top: calc(50% - 100px / 2); */
            /* right: 0px; */
            /* left: calc(50% - 100px / 2); */
            /* bottom: 0px; */
            /* margin: auto; */
            /* vertical-align: middle; */
            /* display: inline-block; */
            /* margin-left: 250px; */
            /* margin-top: 250px; */
        }
    </style>

1.4.2 flex-box

   <style>
        .a1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            display: flex;
            justify-content: center;
            align-items: center;
            /* position: relative; */
            /* line-height: 600px; */
            /* text-align: center; */
        }

        .a2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            /* position: absolute; */
            /* transform: translate(-50%, -50%); */
            /* margin-top: -50px; */
            /* top: 50%; */
            /* margin-left: -50px; */
            /* left: 50%; */
            /* top: calc(50% - 100px / 2); */
            /* right: 0px; */
            /* left: calc(50% - 100px / 2); */
            /* bottom: 0px; */
            /* margin: auto; */
            /* vertical-align: middle; */
            /* display: inline-block; */
            /* margin-left: 250px; */
            /* margin-top: 250px; */
        }
    </style>

1.4.3 flex-box+margin

<style>
        .a1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            /* position: relative; */
            display: flex;
            /* line-height: 600px; */
            /* text-align: center; */
        }

        .a2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            /* position: absolute; */
            /* transform: translate(-50%, -50%); */
            /* margin-top: -50px; */
            /* top: 50%; */
            /* margin-left: -50px; */
            /* left: 50%; */
            /* top: calc(50% - 100px / 2); */
            /* right: 0px; */
            /* left: calc(50% - 100px / 2); */
            /* bottom: 0px; */
            margin: auto;
            /* vertical-align: middle; */
            /* display: inline-block; */
            /* margin-left: 250px; */
            /* margin-top: 250px; */
        }
</style>

1.5 table-cell

.a1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            display: table-cell;
            vertical-align: middle;
            /* justify-content: center; */
            /* align-items: center; */
            /* position: relative; */
            /* line-height: 600px; */
            text-align: center;
        }

        .a2 {
            width: 100px;
            height: 100px;
            display: inline-block;
            background-color: yellow;
            /* position: absolute; */
            /* transform: translate(-50%, -50%); */
            /* margin-top: -50px; */
            /* top: 50%; */
            /* margin-left: -50px; */
            /* left: 50%; */
            /* top: calc(50% - 100px / 2); */
            /* right: 0px; */
            /* left: calc(50% - 100px / 2); */
            /* bottom: 0px; */
            /* margin: auto; */
            vertical-align: middle;
            /* display: inline-block; */
            /* margin-left: 250px; */
            /* margin-top: 250px; */
        }

相对于父元素(宽高未知)

1.2 text-aligin+vertical-align

1.3.4 position+translate

1.4.2 flex-box

1.4.2 flex-box+margin

1.5 table-cell

相对于浏览器窗口(宽高已知)

1.1 margin硬挤

1.3.1 position+margin

1.3.2 positon+calc函数

1.3.3 position+margin-top

1.3.4 position+translate

1.4.2 flex-box

1.4.3 flex-box+margin

1.5 table-cell

相对于浏览器窗口(宽高未知)

1.3.4 position+translate

1.4.2 flex-box

1.4.3 flex-box+margin

1.5 table-cell

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值