Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering large Images vertically in a smaller div

I have a problem thats driving me a little insane. I have a small div that contains a larger image (of no fixed dimension)

Im taking this larger image, and giving it a width of 100% to scale down to fit within the boxes boundaries, but I need the image to show up vertically centered inside the div rather than just top down.

Is there an easy CSS fix for this? I've been poking around, but not having much luck.

Currently:    Intended:
 --------      --------
| div /w |    |overflow|
| image  |     --------    
 --------     | div w/ |
|overflow|    |  image |
| image  |     --------
 --------     |overflow|
               --------
like image 548
Conor Avatar asked Sep 03 '10 18:09

Conor


2 Answers

I'd try this CSS:

div.container { width: ?px; height: ?px; overflow: hidden; position: relative; }
div.container img { position: absolute; top: (-50% of image height); left: (-50% of image width); }
like image 150
sitesbyjoe Avatar answered Nov 19 '22 03:11

sitesbyjoe


I would suggest sticking the image in a div and then centering the div. There is a detailed description on how to do this right here.

like image 42
Azmisov Avatar answered Nov 19 '22 03:11

Azmisov