As we all know, we can create a responsive image with 100% of parent width and height according to ratio with following code. However, I would like to achieve the same effect by treating it as background image using background-image CSS
.
It seems that I am unable to find any solution to it.
<div style="margin: 0 20px;">
<div style="width: 100%">
<img style="width: 100%" src="http://www.greycloaktech.com/wp-content/uploads/2015/07/url-small.jpg">
</div>
</div>
It seems that a lot have given answer about background: contain
and background: cover
and it does not work. I have provided a code snippet as follow for better clarification.
Rules:
1) Height of div should not be specified.
2) background
CSS should be used (and this is why I call it as background image).
3) Background image height should auto scale according to the width.
.bg-image {
width: 100%;
background: url('http://www.greycloaktech.com/wp-content/uploads/2015/07/url-small.jpg') no-repeat;
}
<div style="margin: 0 20px;">
<div class="bg-image">
</div>
</div>