0

I am currently using the image container (below)

}
.image_container {
width: 570px;
height: 120px;
margin: 275px auto 0;
}

This code horizontally centers the div just fine, but I cannot figure out how to set automatic vertical alignment. Any advice?

  • Refer to this question, http://stackoverflow.com/questions/356809/best-way-to-center-a-div-on-a-page-vertically-and-horizontally?rq=1. – Josh Powell Nov 13 '13 at 05:28
  • Doesn't seem to work. The item in the iframe jumps to the top of the page and stays horizontally centered. The top margin is able to keep it on the page in the right spot with my current screen resolution of 1600x900, but on larger resolutions the top margin needs to automatically shift to center and with my current code that won't happen. – Brandon Covington Nov 13 '13 at 05:32
  • Ahh I see I see, you are also using an iframe? – Josh Powell Nov 13 '13 at 14:06

3 Answers3

0

As long as you have static heights to work with, you can use a couple of strategies for vertical centering.

The most simple is to combine line-height and vertical-align: middle:

.image_container {
    width: 570px;
    height: 120px;
    margin: 275px auto 0;
    line-height: 120px;
}
.image_container img {
     vertical-align: middle;
}

<div class="image_container">
    <img src="http://jsfiddle.net/img/logo.png" />    
</div>

See it: http://jsfiddle.net/ka8Hw/1/ (note: I removed your top margin for the demo)

The line-height rule is a sort-of hacky way for inline elements to treat the entire box's height as a single line. The image will treat the middle of the box as the line that it needs to align with. But, it will align so the bottom of the image rides the midpoint. So, we use vertical-align:middle to tell the image to align its midpoint with the line's midpoint.

Documentation

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
0

you need to set text-align:center for container and line-height:120px as the height of container, then set vertical-align:middle to img in the container.

jsFiddle

center aligned image vertically and horizontally

HTML

<div class="image_container">
    <img src="http://placehold.it/70x70"/>
</div>

CSS

.image_container {
    width: 570px;
    height: 120px;
    margin: 100px auto 0;

    background:green;

    line-height:120px;
    text-align:center;
}

.image_container img{
    vertical-align:middle;
}
Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
-1

try this,

.image_container {
    width:400px;
    height: 500px;
    background:#000;
    display: table-cell;
    vertical-align: middle;
    text-align:center;
}

http://jsfiddle.net/ka8Hw/3/

radha
  • 782
  • 4
  • 8