首页 > Web开发 > 详细

html----怎样实现元素的垂直居中

时间:2019-02-19 16:20:16      阅读:156      评论:0      收藏:0      [点我收藏+]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.使用弹性盒

  
//使用弹性盒
  #box {
      width: 200px;
      height: 400px;
      background: lightblue;
      display: flex;
      align-items: center;

    }

    .con1 {
      width: 100px;
      height: 100px;
      background: lightpink;
    }

2.使用   绝对定位  + transform

<style>
    #box {
      width: 200px;
      height: 400px;
      background: lightblue;
      position: relative;

    }

    .con1 {
      width: 100px;
      height: 100px;
      background: lightpink;
      position: absolute;
      top: 50%;
      transform: translate(0, -50%);
    }
  </style>

3.使用绝对定位 + margin:auto

 <style>
    #box {
      width: 200px;
      height: 400px;
      background: lightblue;
      position: relative;
    }

    .con1 {
      width: 100px;
      height: 100px;
      background: lightpink;
      position: absolute;
      top: 0;
      bottom: 0;
      margin: auto;
    }
  </style>

 

html----怎样实现元素的垂直居中

原文:https://www.cnblogs.com/SRH151219/p/10401430.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!