There is no fixed correct height. If i set a fixed height in css the image will not resize with correct aspect ratio in my responsive layout.
The main problem is that css calculates the automatic height and aspect ratio from the image which is set by the src-attribute and not by the width- and height-attribute. So if there is a real image which has a width and height everything works fine. But if there is a blank (this is only a stretched 1x1 image) the aspect ratio will not be calculated correctly because the width and height set by html has no influence on the css-calculation (but how it shown by the browser without the css-calculation).
One thing i thought about was to set "height: auto" only for "real images" and calculate the height of "blank images" by jquery on every window resize:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<style type="text/css">
img {
max-width: 100%;
}
.lazy-loaded {
height: auto;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script language="JavaScript">
$(document).ready(function(){
resizeBlankImages();
});
$(window).resize(function(){
resizeBlankImages();
});
function resizeBlankImages() {
$(".lazy-blank").each(function () {
var originalWidth = $(this).attr('width');
var originalHeight = $(this).attr('height');
var ratio = originalWidth/originalHeight;
var width = $(this).width();
var height = width/ratio;
$(this).height(height);
});
}
</script>
</head>
<body>
<img data-original="image.jpg" src="image.jpg" class="lazy lazy-loaded" width="500" height="300">
<br/>
<img data-original="image.jpg" src="blank.gif" class="lazy lazy-blank" width="500" height="300">
</body>
</html>
It works but it could get very cumbersome on a page with many images. Anyone another idea for that?