首页 > 其他 > 详细

元素垂直居中

时间:2021-07-15 01:02:42      阅读:15      评论:0      收藏:0      [点我收藏+]

元素垂直居中


?? position + transform

<div class="big">
    <div class="small"></div>
</div>
<style>
 .big {
     height: 300px;
     width: 300px;
     background-color: orangered;
     display: flex;
     justify-content: center;
     align-items: center;
  }
  .small {
     height: 100px;
     width: 100px;
     background-color: blue;
  }
</style>

?? position + transform 元素平移

<div class="big">
    <div class="small"></div>
</div>
<style>
 .big {
     height: 300px;
     width: 300px;
     background-color: orangered;
     position: relative;
  }
  .small {
     height: 100px;
     width: 100px;
     background-color: blue;
     position: absolute;
     top: 50%;
     left: 50%;
     transform: translate(-50%, -50%);
  }
</style>

?? position + margin:auto

<div class="big">
    <div class="small"></div>
</div>
<style>
 .big {
     height: 300px;
     width: 300px;
     background-color: orangered;
     position: relative;
  }
  .small {
     height: 100px;
     width: 100px;
     background-color: blue;
     position: absolute;
     top: 0;
     bottom: 0;
     left: 0;
     right: 0;
     margin: auto;
  }
</style>

?? position 需要确定父子元素的宽高

<div class="big">
    <div class="small"></div>
</div>
<style>
 .big {
     height: 300px;
     width: 300px;
     background-color: orangered;
     position: relative;
  }
  .small {
     height: 100px;
     width: 100px;
     background-color: blue;
     position: absolute;
     top: 50%;
     left: 50%;
     margin-left: -50px;
     margin-top: -50px;
  }
</style>

元素垂直居中

原文:https://www.cnblogs.com/svyt/p/15013474.html

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