6

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;

}

3 Answers 3

6

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;

}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thanks! I used line-height: 2em; since I only had a number to center.
0
.contributor_thumbnail img {
    margin-left: auto;
    margin-right: auto;
}

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

1 Comment

This is for horizontal centering, not vertical

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.