css盒子垂直水平居中的四种方式

绝对定位居中法有三种,还有一种是flex属性居中

1.绝对定位居中法

(利用绝对定位)(如果是在父盒子里居中,则个父盒子设置相对定位)子绝父相

      top: 0px;
      left: 0px;
      right: 0px;
      bottom: 0px;
      margin: auto;

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
  /* 绝对定位垂直水平居中的方法 */
    .box {
      position: absolute;
      top: 0px;
      left: 0px;
      right: 0px;
      bottom: 0px;
      margin: auto;
      width: 400px;
      height: 300px;
      background-color: aquamarine;
    }
  </style>
</head>

<body>
  <div class="box">
  </div>
</body>

</html>

2.绝对定位居中法

先让子盒子向右移动到父盒子长度的百分之五十,然后向左移动自身的一半

即是left:50%; margin-left为负数

然后向下移动父盒子高度的一半,在向上移动自身的一半

即是top50%;margin-top为负数

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
  /* 第二种绝对定位垂直水平居中的方法 */
    .box {
      position: absolute; 
      left: 50%;
      margin-left: -200px;
      top: 50%;
      margin-top: -150px;
      width: 400px;
      height: 300px;
      background-color: aquamarine;
    }
  </style>
</head>

<body>
  <div class="box">
  </div>
</body>

</html>

3.绝对定位垂直水平居中法

和第二种很像(有兼容性问题I6-I7-I8浏览器不支持)

先让子盒子向右移动到父盒子长度的百分之五十,

即是left:50%; 

然后向下移动父盒子高度的一半,

即是top50%;

然后向左移动自身的一半、在向上移动自身的一半。使用transform属性

transform: translate(-50%,-50%);

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
  /* 第三种绝对定位垂直水平居中的方法 */
    .box {
      position: absolute; 
      left: 50%; 
      top: 50%;
      transform: translate(-50%,-50%);
      /* 存在兼容性问题 I6-I7-I8浏览器不支持 */
      width: 400px;
      height: 300px;
      background-color: aquamarine;
    }
  </style>
</head>

<body>
  <div class="box">
  </div>
</body>

</html>

4.flex属性居中法

首先给父盒子或者body设置flex属性

然后垂直居中align-items: center;

水平居中justify-content: center;

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>flex属性居中法</title>
  <style>
  /* 第4种绝对定位垂直水平居中的方法 */
  html,body{
    /* 给body设置网页高度 */
    height: 100%;
  }
    body{
      display: flex;
      /* 垂直方向居中 */
      align-items: center;
      /* 水平方向居中 */
      justify-content: center;
    }
    .box {
      width: 400px;
      height: 300px;
      background-color: aquamarine;
    }
  </style>
</head>

<body>
  <div class="box">
  </div>
</body>

</html>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一个好好的程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值