首页 > Web开发 > 详细

html----实现元素上下左右居中

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

技术分享图片

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

方法1:

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

    .con {
      width: 100px;
      height: 100px;
      background: blue;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;
    }
  </style>

方法2:

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

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

方法3:

<style>
    #box {
      width: 300px;
      height: 200px;
      background: lightpink;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .con {
      width: 100px;
      height: 100px;
      background: blue;

    }
  </style>

 

html----实现元素上下左右居中

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

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