I have a div with a height of 300px. I want to have one or more child images that scale according to this height.
It works great when I use an img with the #src CSS below. Width and height are not defined as pixel resolutions so it scales as it should if I changed the height of #wrap.
Is it possible to do the same with CSS backgrounds (no Javascript) without defining a pixel width/height? I can set a width of 100% and place a max-width in there but that restricts the image to the max-width, and if the image is smaller than the max-width, there will be a gap to the right (which can be very large depending on the value of max-width).
Thanks!
#wrap
{
width:100%;
height:300px;
}
#src
{
display:inline-block;
width:auto;
height:100%;
border:1px #0f0 solid;
}
#bg
{
display:inline-block;
background-image:url(https://i.imgur.com/6672G7J.png);
background-repeat:no-repeat;
background-size:contain;
width:auto;
height:100%;
border:1px #f00 solid;
}
<div id="wrap">
<img id="src" src="https://i.imgur.com/6672G7J.png" />
<div id="bg"></div>
</div>