I have a div which is 145px X 145px. I have an img inside this dive. The img could be of any size (longest side being 130px). I would like the image to be centered vertically in the div. Everything that I have tried works in most browsers, but not IE7. I need something that will work in IE7.
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
Not sure about IE7 but for IE9 and rest of the modern browsers following works.
To use it
This places images at dead centre.
Not sure what you have tried so far but the vertical-align CSS property should work if the images are displayed as inline elements.
Some info on vertical-align: http://www.w3schools.com/css/pr_pos_vertical-align.asp
If you have to account for all image heights, that is pretty much the only way without using JavaScript.
If the images are not inline elements and you only had to accommodate images of a consistent height, you could do something like this:
Otherwise the only way I can think to accomodate images of varying height would be to do something similar with your CSS but set the negative margin to half of the image's height with JS.
You can replace the image by a background on the div like this :
I created a little jQuery code to do this without having to use nasty tables:
And you also need a little bit of css:
Using the
line-height
property solved the problem for me.Reference: vertical-align image in div
HTML:
CSS:
here's a cross-browser solution: