Code copy on jsfiddle
html, body { height: 100%; } .table { display: table; width: 100%; height: 100%; } .table__cell { display: table-cell; vertical-align: middle; } img { display: block; max-width: 100%; margin: 0 auto; } <div class="table"> <div class="table__cell"> <img src="http://placehold.it/750x350" alt=""> </div> </div> It uses a standard technique with display: block; and max-width: 100% for images:
img { display: block; max-width: 100%; } If you increase / decrease the width of the container, the image reduces / increases its width and at the same time proportionally decreases / increases its height. This is a very cool feature of images, where it helps a lot.
How to make it so that when you increase / decrease the height of the container, the image behaves like the first case: the height of the image becomes equal to the height of the container and the width of the image also proportionally decreased / increased? How to do this without javascript? (I know how to do it with javascript.) You can use the most fashionable CSS3 stuff.
