我需要根据容器div的高度在中间垂直对齐横幅。我试过但它没有对齐
<div id="left-ad">
<div id="sidebar"> <a href="#"><img src="sidewall.png" alt="LEft Banner" ></a>
</div>
</div>
<div class="right-ad"> <a href="#"><img src="wad.jpg" alt="Right Banner" style="border-width: 0px" ></a>
</div>
<div class=wrapper>//center body part</div>
答案 0 :(得分:1)
一个选项是将display:table应用于容器,并显示:table-cell和vertical-align:middle to child,如下所示:
HTML:
<div class=wrapper>
<div class="content">Content</div>
</div>
CSS:
.wrapper {
display: table;
}
.wrapper .content {
display: table-cell;
vertical-align: middle;
text-align: center;
}
更新了JS Fiddle:
答案 1 :(得分:1)
将.wrapper添加到行高和text-align,如下所示:
基于包装高度
的行高.wrapper {
width:200px;
margin:0 auto;
background-color:blue;
height:150px;
line-height:150px;
text-align:center; }
JS FIDDLE
https://jsfiddle.net/aypye6g3/
图片强>
答案 2 :(得分:0)
HTML:
<div class="wraper">
<img src=image src"/>
</div>
CSS:
.wraper{
position: relative
height: 160px;
width: 160px;
}
img {
max-height: 100%;
max-width: 100%;
width: auto;
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto
}