Struggling to swap out one image for another without stretching it? In my demo, I replaced an elephant with a lion using only CSS - no changes allowed to the HTML. Unfortunately, the lion image ends up stretched. Even adding background-size: cover
doesn't help. Any ideas on how to keep the lion image from stretching without altering the div or image height, maybe using something similar to background-size: cover
? Appreciate any guidance.
div {
width: 300px;
height: 400px;
background: lightgreen;
}
img {
width: 100%;
height: 400px;
content: url(https://c81s22ku6ih1er8af18cv13c-wpengine.netdna-ssl.com/wp-content/uploads/2015/04/Lion.jpg);
}
<div>
<img src='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQudlp3slhvMcKABuQ3hqSbMiBQVUO7nYlDgw_-oj2dUzQ84SEw' />
</div>