I want an image on a webpage to grow/shrink to the available height or width of a flexbox and keep the original aspect ratio.
I have used max-height:100%; max-width:100%
on the image but that doesn't work.
The page always fills the browser window without overflow. It has 3 rows:
- a title at the top with fixed height
- an article, with image and description side by side
- a footer at the bottom with fixed height
The image should grow/shrink with the available height or width, depending on the aspect ratio of the image and surrounding box.
This means most of the times empty space is present left or under the image.
This is a example page:
<html><head>
<title>Title</title>
<style type="text/css">
* {font-size:100%; font:inherit; padding:0; border:0; margin:0}
body {background:#FFC; color:#333; font-family:sans-serif}
body {display:flex; flex-direction:column; height:100%}
h1 {text-align:right; margin-right:20em; background:#CAA}
article {flex:1; display:flex}
article figure {flex:1}
article figure img {display:block; max-width:100%; max-height:100%}
#description {width:20em; background:#ACA}
footer {background:#AAC}
</style>
</head><body>
<h1>Title</h1>
<article>
<figure> <img src="https://placekitten.com/987/765" /> </figure>
<div id="description">
<p>The description.</p>
</div>
</article>
<footer> Footer </footer>
</body></html>
I made a JSFiddle, but it does not place the footer fixed at the bottom as it does when viewed in a separate browser window.
It would be nice to use flexbox as it's promoted for it's easy layout. But if this cannot be done with it I'll use absolute positioning.