如何使一个盒子水平垂直居中?(5类8种)

结构:

  <div class="parent" style="margin: 10px;">
        <div class="child">块元素水平垂直居中</div>
  </div>
  <div class="parent" style="margin: 10px;">
        <span class="child">行内元素水平垂直居中</span>
  </div>
  <div class="parent" style="margin: 10px;">
        <span class="child" style="display: inline-block;">行内块元素水平垂直居中</span>
  </div>

布局:

第一种:flex布局

核心:

父元素:

            display: flex;

            align-items: center;

            justify-content: center;

<style>
    * {
        margin: 0;
        padding: 0;
    }

    .parent {
        width: 500px;
        height: 500px;
        background-color: #bfa;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .child {
        background-color: red;
        width: 150px;
        height: 150px;
    }
</style>
第二种:position定位

核心:

父元素:绝对定位

子元素:相对定位,会将元素都转为行内块

调整子元素的位置的方法:

  1. 子元素left:50%;top:50%,
    1. 无需宽高transform:translate(-50%,-50%)
    2. 需要宽高,margin-left/margin-top子元素宽高的一半
  2. 子元素left:0;top:0;right:0;bottom:0
    1. 需要宽高,margin:0,auto;
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .parent {
        width: 500px;
        height: 500px;
        background-color: #bfa;
        position: relative;
    }
    .child {
        background-color: red;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
    }
</style>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .parent {
        width: 500px;
        height: 500px;
        background-color: #bfa;
        position: relative;
    }
    .child {
        background-color: red;
        position: absolute;
        width: 150px;
        height: 150px;
        left: 50%;
        top: 50%;
        margin-left: -75px;
        margin-top: -75px;
    }
</style>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .parent {
        width: 500px;
        height: 500px;
        background-color: #bfa;
        position: relative;
    }
    .child {
        background-color: red;
        position: absolute;
        width: 150px;
        height: 150px;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
    }
</style>
第三种:grid布局

核心:

父元素:

        display:grid;

        place-item:center;

<style>
    * {
        margin: 0;
        padding: 0;
    }

    .parent {
        width: 500px;
        height: 500px;
        background-color: #bfa;
        display: grid;
        place-items: center;
    }

    .child {
        background-color: red;
    }
</style>
第四种:table布局

核心:

父元素:

        display:table-cell;

        vertical-align: middle;

针对子元素为行内元素/行内块元素:

父元素:text-align:center

针对子元素为块元素:

子元素:margin:0 auto;

<style>
    * {
        margin: 0;
        padding: 0;
    }

    .parent {
        width: 500px;
        height: 500px;
        background-color: #bfa;
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }

    .child {
        background-color: red;
        width: 150px;
        height: 150px;
        margin: 0 auto;
    }
</style>
第五种:只针对行内元素(不包含行内块元素)

核心:

父元素:

        text-align:center

子元素:

        line-height:为父元素的高

<style>
    * {
        margin: 0;
        padding: 0;
    }

    .parent {
        width: 500px;
        height: 500px;
        background-color: #bfa;

        text-align: center;
    }

    .child {
        background-color: red;
        width: 150px;
        height: 150px;
        line-height: 500px;
    }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值