javascript - 当一个父级元素高度为百分比的时候,怎么让子元素垂直居中呢?
高洛峰
高洛峰 2017-04-10 16:39:09
[JavaScript讨论组]

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(6)
PHP中文网

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

html代码

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

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>
<p class = "father">
    <p class="son"></p>
    <img class = "daughter" src="1.jpg" alt="我要居中">
</p>
</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>
<p class="father">
    <img src="1.jpg">
</p>

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>


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

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>
            <p class="daughter">
                <img src="1.jpg">
            </p>
        </td>
    </tr>
</table>
巴扎黑
  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;/**平均分**/
        }

<p class="box">
            <span class="item"></span>
        </p>
PHP中文网

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

黄舟

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

父元素高度
width: 20%;

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

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

所以可以做到垂直居中

PHPz

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号