views:

1124

answers:

4

I have a bunch of images which all fit into a 400px × 400px box (that is, one of their dimensions is 400px and the other is smaller). I would like to be able to, using CSS, but jquery/javascript if necessary, fit that image to a 200px by 200px box, so that two edges of the image touch the box, and there is a gap between the other two edges of the box. Aspect ratio must be maintained.

My HTML is as follows:

<div class="small">
    <img src="/images/photos/View.jpg" alt="View" />
</div>

And my CSS is:

div.images div.small
{
    width:200px;
    height:200px;
    line-height:200px;
    text-align:center;
}
div.images div.small img
{
    vertical-align:middle;
    max-width:200px;
    max-height:200px;
}

You can see a sample here.

Unfortunately, my landscape images hug the top of the box, whereas I would like them to be centered. Also, I'm not sure of the wisenees of relying upon max-width/max-height.

How can I center my images within these boxes?

A: 

have you tried using:

display:block;
margin-left:auto;
margin-right:auto;

that should center block level elements

knittl
I was under the impression `<img/>` was an inline element. At any rate, that doesn't work for vertical centering.
Eric
oh my bad, it is an inline element. edited my answer. if your container div has a fixed height then `margin:auto;` should als center vertically
knittl
`margin:auto` only centres horizontally, not vertically.
DisgruntledGoat
+2  A: 

I needed to do the same some time ago, and I found a good implementation in this link. "Center an image inside a div, vertical and horizontal, without knowing the image's size".

It works for me as intended.

GmonC
Awesome, I didn't know you could do this.height in CSS.
Zoidberg
I would stay away from expressions and other IE hacks, I'm not sure they're even necessary.
DisgruntledGoat
The main problem is that the image needs to be stretched to fit the box, while preserving it's aspect ratio. I'm pretty sure the centering can be acheived with `line-height`. My problem is resizing the image.
Eric
+1  A: 

I set this up on my computer and it worked fine. After looking at your example page, the problem is because you've set the image to display:block. Either remove that rule from your general img rule (weird thing to set globally, anyway), or change the image rule you posted above to this:

div.images div.small img
{
 vertical-align: middle;
 max-width: 200px;
 max-height: 200px;
 display: -moz-inline-box; /* Firefox 2 */
 display: inline-block;
}

By default, the img element and other "replaced" elements (Flash, etc) are "inline-block" - this means they flow inline like text, but have a width and height.

DisgruntledGoat
There seems to be a pixel shift at the top of the portrait pictures (see previous link). Any ideas why?
Eric
I'm not 100% sure but it'll be something weird with text (even though there's no text there). Setting `div.images div.small` to `line-height: 98px;` or adding `font-size: 1%` to `div.images div.small img` worked for me.
DisgruntledGoat
+1  A: 

I ran into this vertical centering problem once and to get it working correctly in all browsers, I resorted to javascript / jQuery:

$(document).ready(function() {
    $('img').each(function() {
     image_height = $(this).height();
     margin_top = (200 - image_height) / 2;
     $(this).css('margin-top', margin_top + 'px');
    });
});

Please note that you will need $(window).load instead of $(document).ready if the image dimensions are not given in the html.

jeroen