Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align image within parent div with css

So I'm trying to vertical align images within a container div. I tried adding vertical-align: middle; to the parent div with no luck.

    <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;

}
like image 401
ThinkingInBits Avatar asked Aug 27 '10 12:08

ThinkingInBits


2 Answers

What I would do is set the image as a background image.

.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;

}
like image 114
Zoidberg Avatar answered Oct 10 '22 02:10

Zoidberg


Try this:

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;}

Inspired by: Lost in the Woods vertically centering with CSS.

like image 44
Kyle Avatar answered Oct 10 '22 03:10

Kyle