views:

23

answers:

2

I need to make an image change its size according to the window size, that is, if the window is re-sized the image needs to be re-sized to fit the window. This problem is easy enough with Chrome and Firefox, I just set 'width=100%', but IE isn't as kind...
This problem was raised several times, but I couldn't find a coherent answer, it might be that I'm really new to the whole web development business, but it seems to me like this problem wasn't really answered...

Could this be solved by changing the width attribute with javascript according to the window size? Did anybody encounter this problem and has a better solution?

Thanks in advance, Trout

+1  A: 

This should do it!!!

<html>
<body>
  <div style="width:100%;height:100%;position:absolute;top:0;left:0;">
     <img src="some.jpg" style="width:100%;height:100%;" />
  </div>
</body>
</html>
jerjer
This works if my img was a background, but its an element in a page.
Trout
Also, when you add both 'width' and 'height' properties the image ratio gets distorted... Thanks for the help though...
Trout
you can remove either width or height such that the image will not be distorted: it should now be <img src="some.jpg" style="width:100%;" /> or <img src="some.jpg" style="height:100%;" />
jerjer
A: 

Ok, this was solved by a friend of mine with a small javacript...

windonw.onresize = function(e){
    if (self.innerWidth){
         windowWidth = self.innerWidth
    } else if (document.documentElement && document.documentElement.clientWidth){
         windowWidth = document.documentElement.clientWidth
    } else if (document.body) {
         windowWidth = document.body.clientWidth
    }
    document.getElementById("img_id").style.width = windowWidth-(some number)
}

The reason I added (some-number) is that IE calculated its width with the margins at the sides and the images escapes the window, so what you have to do is make the image a little smaller than 'windowWidth'...
Hope this helps more lost souls like me... :)

Trout