盒子水平垂直居中的7种方法

以下七种方法:

  1. 父元素设置display:flex     子元素设置:margin:auto
  2. 父元素设置display:flex; justify-content:center; align-items:center;
  3. 父元素设置 display:grid;justify-content:center;align-items:center
  4. 子绝父相;配合属性全部为0,margin:auto
  5. 子绝父相;top:50%;left:50%;margin-left:-width/2;margin-top:-height/2
  6. 给父元素设置border或者overflow: hidden;

    给子元素设置margin:(父height-子height)/2(父width-子width)/2 

  7. 给父元素设置border-box 设置padding:(父height-子height)/2(父width-子width)/2 

执行结果:

 

该例子的html代码:

<body>
  <div class="parent">
    <div class="child"></div>
  </div>
</body>

第一种:父元素设置display:flex     子元素设置:margin:auto

.parent {
      width: 300px;
      height: 300px;
      background-color: paleturquoise;
      display: flex;

    }

    .child {
      width: 100px;
      height: 100px;
      background-color: palevioletred;
      margin: auto;

    }

第二种:父元素设置display:flex; justify-content:center; align-items:center;

    .parent {
      width: 300px;
      height: 300px;
      background-color: paleturquoise;
      display: flex;
      justify-content: center;
      align-items: center;

    }

第三种:父元素设置 display:grid;justify-content:center;align-items:center

    .parent {
      width: 300px;
      height: 300px;
      background-color: paleturquoise;
      display: grid;
      justify-content: center;
      align-items: center
    }

第四种:子绝父相;配合属性全部为0,margin:auto

    .parent {
      width: 300px;
      height: 300px;
      background-color: paleturquoise;
      position: relative;
    }

    .child {
      width: 100px;
      height: 100px;
      background-color: palevioletred;
      position: absolute;
      margin: auto;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
    }

第五种:子绝父相;top:50%;left:50%;margin-left:-width/2;margin-top:-height/2

    .parent {
      width: 300px;
      height: 300px;
      background-color: paleturquoise;
      position: relative;
    }

    .child {
      width: 100px;
      height: 100px;
      background-color: palevioletred;
      position: absolute;
      margin: auto;
      top: 50%;
      left: 50%;
      margin-left: -50px;
      margin-top: -50px;
    }

第六种:

给父元素设置border或者overflow: hidden;

给子元素设置margin:(父height-子height)/2(父width-子width)/2 

    .parent {
      width: 300px;
      height: 300px;
      background-color: paleturquoise;
      border: 1px solid red;

    }

    .child {
      width: 100px;
      height: 100px;
      background-color: palevioletred;
      margin: 100px 100px;

    }

第7种:

给父元素设置border-box 设置padding:(父height-子height)/2(父width-子width)/2 

    .parent {
      width: 300px;
      height: 300px;
      background-color: paleturquoise;
      box-sizing: border-box;
      padding: 100px 100px;
    }

    .child {
      width: 100px;
      height: 100px;
      background-color: palevioletred;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值