【最全】CSS盒子(div)水平垂直居中居然还有这种方式

相对于屏幕

image

方法一:利用定位

<div class="box"></div>
<style>
    body {
        background: green;
    }
    .box {
        position: fixed;
        top: 50%;
        left: 50%;
        margin: -150px 0 0 -150px;
        width: 300px; 
        height: 300px;
        background: orange;
    }
</style>

方法二:利用 transform

<div class="box"></div>
<style>
    body {
        background: green;
    }
    .box {
        position: fixed;
        top: 50%;
        left: 50%;
        width: 300px; 
        height: 300px;
        transform: translate(-50%, -50%);
        background: orange;
    }
</style>

方法三:利用 margin auto

<div class="box"></div>
<style>
    body {
        background: green;
    }
    .box {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: auto;
        width: 300px; 
        height: 300px;
        background: orange;
    }
</style>

相对于父容器

image

方法一:利用定位

<div class="parent">
    <div class="child"></div>
</div>
<style>
    .parent {
        position: relative;
        margin: 100px auto 0;
        width: 500px; 
        height: 500px;
        background: green;
    }
    .child {
        position: absolute;
        top: 50%;
        left: 50%;
        margin: -100px 0 0 -100px;
        width: 200px; 
        height: 200px;
        background: orange;
    }
</style>

方法二:利用 transform

<div class="parent">
    <div class="child"></div>
</div>
<style>
    .parent {
        position: relative;
        margin: 100px auto 0;
        width: 500px; 
        height: 500px;
        background: green;
    }
    .child {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 200px; 
        height: 200px;
        background: orange;
    }
</style>

方法三:利用 margin auto

<div class="parent">
    <div class="child"></div>
</div>
<style>
    .parent {
        position: relative;
        margin: 100px auto 0;
        width: 500px; 
        height: 500px;
        background: green;
    }
    .child {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: auto;
        width: 200px; 
        height: 200px;
        background: orange;
    }
</style>

方法四:利用 flex

<div class="parent">
    <div class="child"></div>
</div>
<style>
    .parent {
        position: relative;
        margin: 100px auto 0;
        width: 500px; 
        height: 500px;
        display: flex;
        justify-content: center;
        align-items: center;
        background: green;
    }
    .child {
        width: 200px; 
        height: 200px;
        background: orange;
    }
</style>

方法五:计算父盒子与子盒子的空间距离

<div class="parent">
    <div class="child"></div>
</div>
<style>
    .parent {
        margin: 100px auto 0;
        width: 500px; 
        height: 500px;
        overflow: hidden;
        background: green;
    }
    .child {
        margin: 150px auto;
        width: 200px; 
        height: 200px;
        background: orange;
    }
</style>

微信
前端面试交流群:剑指 Offer (3群)

posted @ 2022-07-11 20:49  蓝天8008  阅读(265)  评论(0)    收藏  举报