使div盒子水平垂直居中的方法

不固定和固定宽高的盒子垂水平直居中的几种方法

1.不固定宽高让盒子垂直居中有以下方法:

第一种:使用css 方法

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .father {
      display: table-cell;
      text-align: center;
      vertical-align: middle;
      height: 400px;
      width: 400px;
      background: red;
    }
    /*子盒子宽高不固定*/
    .son {
      display: inline-block;
      vertical-align: middle;
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="son"></div>
  </div>
</body>
</html>

第二种:使用定位和位移

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .father {
      position: relative;
      height: 400px;
      width: 400px;
      background: red;
    }
  /* 子盒子宽高不固定*/
    .son {
      position: absolute;
      transform: translate(-50%, -50%);
      top: 50%;
      left: 50%;
      /*background: pink;*/
    }
  </style>
</head>
  <body>
    <div class="father">
      <div class="son"></div>
    </div>
  </body>
</html>

第三种使用弹性模型

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
  .father {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 400px;
    width: 400px;
    background: red;
  }
</style>
</head>
<body>
  <div class="father">
    <div class="son"></div>
  </div>
</body>
</html>
2.宽高固定让盒子垂直水平居中

第一种 使用绝对定位和 margin: auto;

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Document</title>
 <style>
   .father {
     position: relative;
     height: 400px;
     width: 400px;
     background: red;
   }
   .son {
     height: 250px;
     width: 250px;
     position: absolute;
     left: 0px;
     top: 0px;
     bottom: 0px;
     right: 0px;
     margin: auto;
     background: #000;
   }
 </style>
</head>
<body>
 <div class="father">
   <div class="son"> </div>
 </div>
</body>
</html>

第二种:使用定位和外边距负值

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .father {
      position: relative;
      height: 400px;
      width: 400px;
      background: red;
    }
    .son {
      height: 200px;
      width: 200px;
      position: absolute;
      /* 定位父盒子的50% */
      left: 50%;
      top: 50%;
      /* 子盒子宽高各一半 */
      margin-left: -100px;
      margin-top: -100px;
      background: #000;
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="son"></div>
  </div>
</body>
</html>

第三种使用弹性盒子

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .father {
      display: flex;
      height: 100%;
      flex-flow: row wrap;
      align-items: center;
      justify-content: center;
      background: red;
    }
    .son {
      height: 200px;
      width: 200px;
      background: #000;
    }
  </style>
</head>
<body style="height: 100%">
  <div class="father">
    <div class="son"></div>
  </div>
</body>
</html>
方法不在乎多,适合就好 !
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Friday--星期五

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值