首页 > 当一个父级元素高度为百分比的时候,怎么让子元素垂直居中呢?

当一个父级元素高度为百分比的时候,怎么让子元素垂直居中呢?

首先,line-height肯定不行了,增加标签清除浮动法必须得是块级元素?绝对定位可以吗?应该怎么写呢?感谢各位博学的大大花时间解答!!~~


方法还是挺多的,我写了2种:

html代码

<div class="container-wrapper">
    <div class="container">
      <div class="center">
        
      </div>
    </div>
  </div>

css代码

.container-wrapper{
  width:400px;
  height:300px;
}
.container{
  width:100%;
  height:100%;
  border:1px solid violet;
  position:relative;
}
.center{
  width:100px;
  height:100px;
  position:absolute;
  margin:auto;
  left:0px;
  top:0px;
  right:0px;
  bottom:0px;
  border:1px solid green;
}

html代码同上
css代码

.container-wrapper{
  width:400px;
  height:300px;
}
.container{
  width:100%;
  height:100%;
  border:1px solid violet;
  position:relative;
}
.center{
  border:1px solid green;
  width:100px;
  height:100px;
  position:absolute;
  left:50%;
  top:50%;
  transform:translate(-50%, -50%);
  -moz-transform:translate(-50%, -50%);
  -ms-transform:translate(-50%, -50%);
  -o-transform:translate(-50%, -50%);
  -webkit-transform:translate(-50%, -50%);

点击看看,目前所见总结得最全的垂直居中


http://www.cnblogs.com/crystalmoore/p/5041522.html 提供了4种方法
1.利用vertical-align:middle

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        body{margin: 0;}
        .father{width: 100%;height: 100%;position: fixed;left: 0;top: 0;text-align: center;font-size: 0;/*设置font-size为了消除代码换行所造成的空隙*/background:rgba(0,0,0,0.5); }
        .daughter{vertical-align: middle;}/*实现daughter居中*/
        .son{vertical-align: middle;display:inline-block;height: 100%;}
    </style>
</head>
<body>
<div class = "father">
    <div class="son"></div>
    <img class = "daughter" src="1.jpg" alt="我要居中">
</div>
</body>
</html>

2.使用transform实现

<style>
    body{margin: 0;}
    .father {position: absolute; left:50%; top:50%; -webkit-transform:translate(-50%,-50%); transform:translate(-50%,-50%);background:rgba(0,0,0,0.5); }
</style>
<div class="father">
    <img src="1.jpg">
</div>

3.弹性盒模型

<style>
    body{margin: 0;}
    .father{position:fixed; width:100%; height:100%; background:rgba(0,0,0,0.5); left:0px; top:0px;display:flex;justify-content:center;align-items:center;}
</style>


<div class="father">
        <img src="1.jpg">
</div>

4.用表格布局

<style>
*{margin:0px; padding:0px;}
table {position:absolute; width:100%; height:100%; text-align:center; background:rgba(0,0,0,0.5);}
.daughter {display:inline-block;}
</style>
<table>
    <tr>
        <td>
            <div class="daughter">
                <img src="1.jpg">
            </div>
        </td>
    </tr>
</table>

可以使用translate
手机回答 代码从略

父元素高度
width: 20%;

子元素 设置绝对定位
top:50%;
tranlateY:-50%;

translate的位移是针对这个子元素的-50%

所以可以做到垂直居中


  html,body{width: 100%;height: 100%;}
        .box{width: 300px;height:50%;overflow: hidden;margin: 200px auto;background: #DAC8A7;border-radius: 10px;}
        .item{width: 50px;height: 50px;border-radius: 50%;background: #4A4439;display: block;}
        .box {
          display: flex;
          align-items: center;/**居中**/
          flex-direction:column;
          justify-content:center;/**平均分**/
        }

<div class="box">
            <span class="item"></span>
        </div>

有一种方法是父元素display为tablecell,子元素为inline-block;还有一种是父元素是用flex-box,items:center来实现垂直居中。

【热门文章】
【热门文章】