The following code works well in Firefox and Chrome, but doesn't in Safari (tested on Mac and iPad): http://jsfiddle.net/eFd87/.
<div id="wrapper">
<div id="content">
<img src="http://farm3.staticflickr.com/2783/4106818782_cc6610db2c.jpg">
</div>
</div>
#wrapper {
position: relative;
padding-bottom: 33.33%; /* Set ratio here */
height: 0;
}
#content {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: green;
text-align: center;
}
#content img {
max-height: 100%;
}
The goal is to have a wrapper div keep a fixed aspect ratio (in my web application it is a carousel), and the image inside it resize to fit in the div (and center).
On Safari the image doesn't display because of the height: 0
(which will give a height of 0 for the img). With height: 100%
the image display but doesn't fit the div (it overflows).
Do you see any solution for this problem? I've been on this for hours...
${window.innerHeight * 9 / 16}px
} – Sulky