5种 CSS 垂直水平居中方法 (未知元素大小2种)

2 篇文章 0 订阅
1 篇文章 0 订阅

不要仅仅看,而不去实际操作写代码看效果;

也许一看就懂,而实战中却需要查阅资料或笔记才能完成的话,就更要自己动手写写了,切忌眼高手低!

<!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>水平垂直居中</title>
  <style>
    /* 1.已知元素宽度高度大小  absolute, left top right bottom 0; margin auto*/
    /* .parent {
      position: relative;
      width: 200px;
      height: 200px;
      background-color: #ffff00;
    }
    .child {
      width: 150px;
      height: 100px;
      background-color: #ff0000;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;

      line-height: 100px;
      text-align: center;
    } */

  /* 2.已知元素高度宽度大小,absolute, left top 50%; 
       margin-left:-width/2; margin-top: -height/2; */
  /* .parent {
    position: relative;
    width: 200px;
    height: 200px;
    background-color: #ffff00;
  }
  .child {
    position: absolute;
    width: 150px;
    height: 100px;
    top: 50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -75px;
    background-color: #ff0000;

    line-height: 100px;
    text-align: center
  } */

  /* 3. 已知元素高度宽度大小 display:table-cell, text-align: center, veryical: middle */
  /* .parent {
    position: relative;
    width: 200px;
    height: 200px;
    background-color: #ffff00;

    display: table-cell; 
    text-align: center; //水平居中
    vertical-align: middle; //垂直居中
  }
  .child {
    background-color: #ff0000;
  } */

  /* 4.元素大小未知,absolute, transform: translate(-50%,-50%)  */
  /* .parent {
    position: relative;
    width: 200px;
    height: 200px;
    background-color: #ffff00;
  }
  .child{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #ff0000;
  } */

  /* 5.元素大小未知, flex布局 */
  /* .parent {
    position: relative;
    width: 200px;
    height: 200px;
    background-color: #ffff00;

    display: flex;
    justify-content: center;
    align-items: center;
  }
  .child {
    background-color: #ff0000;
  } */
  </style>
</head>
<body>
  <div class="parent">
    <div class="child">水平垂直居中</div>
  </div>
</body>
</html>

已知元素宽高大小
  1. absolute,left top right bottom 0; margin auto(此时必须知道元素宽高)

      .parent {
          position: relative;
          width: 200px;
          height: 200px;
          background-color: #ffff00;
        }
        .child {
          width: 150px;
          height: 100px;
          background-color: #ff0000;
          position: absolute;
          left: 0;
          right: 0;
          top: 0;
          bottom: 0;
          margin: auto;
    
          line-height: 100px;
          text-align: center;
        } 
    

    在这里插入图片描述

  2. absolute, left top 50%; margin-left:-width/2; margin-top: -height/2;(此时必须知道元素宽高)

      .parent {
        position: relative;
        width: 200px;
        height: 200px;
        background-color: #ffff00;
      }
      .child {
        position: absolute;
        width: 150px;
        height: 100px;
        top: 50%;
        left: 50%;
        margin-top: -50px;
        margin-left: -75px;
        background-color: #ff0000;
    
        line-height: 100px;
        text-align: center
      }
    

    在这里插入图片描述

  3. display:table-cell, text-align: center, veryical: middle (此时必须知道元素宽高)

    虽然已经水平垂直居中,但是给子元素加上背景色后就发现不一样的地方,其实是子元素的宽度继承了父元素造成的;

      .parent {
        position: relative;
        width: 200px;
        height: 200px;
        background-color: #ffff00;
    
        display: table-cell; 
        text-align: center; //水平居中
        vertical-align: middle; //垂直居中
      }
      .child {
        background-color: #ff0000;
      } 
    

    在这里插入图片描述

未知元素宽高大小
  1. absolute, transform: translate(-50%,-50%) ;若translate(0,-50%)则为垂直居中,translate(-50%,0)则为水平居中;所以都是互通的,其实水平居中,垂直居中会了,水平垂直居中也就水到渠成;

      .parent {
        position: relative;
        width: 200px;
        height: 200px;
        background-color: #ffff00;
      }
      .child{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #ff0000;
      } 
    

    在这里插入图片描述

  2. flex布局,justify-content: center; 水平居中 align-items: center;垂直居中

     .parent {
        position: relative;
        width: 200px;
        height: 200px;
        background-color: #ffff00;
    
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .child {
        background-color: #ff0000;
      } 
    

    在这里插入图片描述

小结:

方法不在多,而在精;善于在不同场景进行不用的方法处理,灵活变通即可。参考学习文章链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值