I have spent a significant amount of time researching and coding, but I am still unable to get this seemingly trivial task to work. Here are the conditions:
- The browser window size is unknown, so any solution involving absolute pixel sizes will not work.
- The original dimensions of the image are also unknown and it may or may not fit the browser window.
- The image should be centered both vertically and horizontally.
- The proportions of the image must be conserved.
- The entire image must be displayed in the window without cropping.
- I do not want scrollbars to appear, they should only show if the image doesn't fit the window.
- The image needs to automatically resize when the window dimensions change, filling all available space without exceeding its original size.
In essence, I am looking for something like this:
.fit {
max-width: 99%;
max-height: 99%;
}
<img class="fit" src="pic.png">
The issue with the code above is that it causes the image to take up vertical space beyond what is needed, resulting in a vertical scrollbar being added.
I have access to PHP, Javascript, JQuery, but I would prefer a CSS-only solution. Compatibility with IE is not a concern for me.