Re asking this as the answers on this 2013 thread (100-width-background-image-with-an-auto-height) don't seems to work anymore.
- I need to make a responsive background image with 100% width and auto height.
- I would like to serve different images depending on the screen size using media queries and not Javascript.
- It would be good if it's working on different browsers (At least Chrome / Firefox)
Using the following code:
<img src="image.jpg" style="width: 100%;">
The image is displayed correctly, 100% width and auto height (See here: JSFIDDLE1) but I am unable to replace the source of it when changing screen sizes, unless I use Javascript.
Therefore I am working on using a background image on a DIV.
<div class="imageContainer1"></div>
and, using the following CSS, everything works fine (at least on Chrome/Safari):
.imageContainer1 {
content:url("image.jpg");
}
See here JSFIDDLE2
but.. this doesn't seems to work on Firefox! :-( as the "content" property seems not to be compatible on firefox.
So.... I would like to use "background-image" property, and find a solution that is compatible with all the browsers but I am struggling with setting a 100% width and auto height.
This
<div class="imageContainer2"></div>
.imageContainer2 {
background-image: url("image.jpg");
background-position: center top;
background-size: 100% auto;
}
Doesn't show anything, but if you set, for example:
height: 500px;
you will be able to see a part of the picture.
This is the JSFiddle: JSFiddle3
I have tried to use:
background-size: cover/contain
But this is not what I am trying to do, as "cover" will allow the image to extend out from the sides and "contain" does not allow the image to extend out at all).
Any solution to this nightmare?