I figured out how to do this without js, although you need to use a transparent image.
Set up your html structure as:
<div class="rect_container"><img class="rect_image" src="rect_image.png"/>
<div class="rect">Your favorite content here</div>
</div>
Use the transparent PNG AxB for rect_image, where AxB is the aspect ratio.
Meanwhile, set up a stylesheet, for example:
.rect_container {width: 50%; position: relative;}
.rect_image {width: 100%; display: block;}
.rect {width: 100%; height: 100%; position: absolute; left: 0px; top: 0px;}
Of great importance here is that the images maintain aspect ratio when resizing in one direction. Meanwhile, we need a useful div, so we make the image display as a block, wrap it in a div and put an absolutely positioned div in it. I redid this code from something more complex that I really tested. It works like a charm.
source
share