如何将一个div水平垂直居中?6种方法做推荐

https://www.cnblogs.com/Julia-Yuan/p/6648816.html

 

div{
            width: 200px;
            height: 200px;
            background: green;
            position:absolute;
            left:0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

 

、、------------

div{
            width:200px;
            height: 200px;
            background:green;
            position: absolute;
            left:50%;
            top:50%;
            margin-left:-100px;
            margin-top:-100px;
        }

、、------

div{
            width: 200px;
            height: 200px;
            background: green;
            position:absolute;
            left:50%;    /* 定位父级的50% */
            top:50%;
            transform: translate(-50%,-50%); /*自己的50% */
        }

、、---------

.box{

             height:600px;
             display:flex;
             justify-content:center;
             align-items:center;
               /* aa只要三句话就可以实现不定宽高水平垂直居中。 */
        }
        .box>div{
            background: green;
            width: 200px;
            height: 200px;
        }

、、------------

<p class="outerBox tableCell">

  </p><p class="ok">

    </p><p class="innerBox">tableCell</p>

  <p></p>

<p></p>

 

 

/*

table-cell实现居中

将父盒子设置为table-cell元素,设置

text-align:center,vertical-align: middle;

子盒子设置为inline-block元素

*/

.tableCell{

  display: table;

}

.tableCell .ok{

  display: table-cell;

  text-align: center;

  vertical-align: middle;

}

.tableCell .innerBox{

  display: inline-block;

}

 

、、-----------

 

<p class="outerBox calc">
    </p><p class="innerBox">calc</p>
<p></p>


/*绝对定位,clac计算位置*/
.calc{
  position: relative;
}
.calc .innerBox{
  position: absolute;
  left:-webkit-calc((500px - 200px)/2);
  top:-webkit-calc((120px - 50px)/2);
  left:-moz-calc((500px - 200px)/2);
  top:-moz-calc((120px - 50px)/2);
  left:calc((500px - 200px)/2);
  top:calc((120px - 50px)/2);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值