垂直对齐块元素

Jae*_*Lee 5 css vertical-alignment

我在div中有一个图像和文本彼此相邻.我试图将文本在中间垂直对齐,但它保持在顶部.请帮忙!

http://jsfiddle.net/9KDva/

HTML:

<div class="title-block">
  <div class="img-holder"><img width="101" height="104" src="http://www.girlsguidetomanners.com/wp-content/uploads/2014/02/url-16-101x104.jpeg" class="attachment-homepoststhumbnail wp-post-image" alt="url-16" /></div>
  <div class="title">Get Your Nose Out of Your IPhone</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.title-block {
width:272px;
height: 110px;
vertical-align:middle;
}

.img-holder {
float: left;
margin: 0 6px 0 0;
position: relative;
}

.img-holder img {
display: block;
}

.title {
display:block;
text-transform: uppercase;
margin: 8px 0 9px;
}
Run Code Online (Sandbox Code Playgroud)

Jun*_*unM 6

你可以使用tabletable-cell:移动你的class='title'内心img-holder

小提琴

填充物离开image- 小提琴

.title-block {
    width:272px;
    height: 110px;    
}

.img-holder {    
    margin: 0 6px 0 0;
    position: relative;
    display: table;
}

img, .title{
    display:table-cell;
    vertical-align: middle;
}
.title {
    text-transform: uppercase;
    margin: 8px 0 9px;  
}
Run Code Online (Sandbox Code Playgroud)

  • 由于问题是如何对齐块,因此将显示更改为表格单元格并不能帮助任何尝试将元素与显示块垂直对齐的人。如果您愿意更改显示,也可以使用柔性对齐。 (3认同)