使用css在父div中垂直对齐图像

时间:2010-08-27 12:12:43

标签: css html vertical-alignment

所以我试图在容器div中垂直对齐图像。我尝试添加vertical-align:middle;没有运气的父母div。

    <div class="contributor_thumbnail"><img src="image.jpg"></div>
    <div class="contributor_thumbnail"><img src="image.jpg"></div>

.contributor_thumbnail {

    position: relative;
    display: block;
    float: left;
    width: 150px;
    height: 150px;
    line-height: 100%;
    text-align: center;
    margin-right: 15px;
    margin-bottom: 15px;
    padding: 5px;
    vertical-align: middle;
    border: 1px solid #bbbbbb;
    border-top: 1px solid #333;
    border-left: 1px solid #333;

}

3 个答案:

答案 0 :(得分:6)

我要做的是将图像设置为背景图像。

.contributor_thumbnail {
    /* Background image instead of using and img tag */
    background: url(image.jpg) center center no-repeat;
    position: relative;
    display: block;
    float: left;
    width: 150px;
    height: 150px;
    line-height: 100%;
    text-align: center;
    margin-right: 15px;
    margin-bottom: 15px;
    padding: 5px;
    border: 1px solid #bbbbbb;
    border-top: 1px solid #333;
    border-left: 1px solid #333;

}

答案 1 :(得分:1)

试试这个:

HTML

<div class="contributor_thumbnail">
<div class="content">
    <img src="image.jpg"> 
</div>
</div>

CSS:

.contributor_thumbnail  {float:left; height:50%; margin-bottom:-120px;}
.content    {clear:both; height:240px; position:relative;}

灵感来自:Lost in the Woods vertically centering with CSS

答案 2 :(得分:0)

.contributor_thumbnail img {
    margin-left: auto;
    margin-right: auto;
}

http://www.bluerobot.com/web/css/center1.html