CSS3 introduces new units that are measured relative to the viewport, which is the window in this case. These are vh
and vw
, which measure viewport height and width, respectively. Here is a simple CSS only solution:
img {
max-width: 100%;
max-height: 100vh;
height: auto;
}
The one caveat to this is that it only works if there are no other elements contributing height on the page.