【发布时间】:2018-01-19 17:09:45
【问题描述】:
我无法在容器 div 内垂直对齐图像。
这是我的html:
<div class="navbar">
<div class="back">
<img src="http://s16.postimg.org/m5yrst5k1/back_icon.png">
</div>
<div class="title">
<h1>title</h1>
</div>
<div class="menu">
<img src="http://s8.postimg.org/hmusw99pt/menu_icon.png">
</div>
</div>
这是css:
html,body{
height: 100%;
width: 100%;
padding:0;
margin:0 auto;
}
.navbar {
background: #09c;
width: 100%;
height: 75px;
z-index: 99999;
box-shadow: 0 0 10px #000;
}
.navbar .back {
position: absolute;
left: 0;
padding:2% 0 0 1%;
}
.navbar .menu{
position: absolute;
right: 0;
padding: 2% 1% 0 0;
}
.navbar .title{
position:absolute;
text-align: center;
width: 100%;
color: white;
}
我知道我可以使用一些媒体查询来设置上边距,以便图像始终垂直居中,但必须有更好的方法来做到这一点。
我也尝试过这样的事情,但没有成功:
img{
min-height: 10em;
display: table-cell;
vertical-align: middle;
}
我应该如何将图像垂直居中以使其始终响应?
这是我的代码笔:
http://codepen.io/anon/pen/mLxei
提前致谢!
【问题讨论】: