1

JavaScript. I want to create simple script, that will be resize loaded image using css width and height. Here is code example:

       <img style="width:200px; height:200px" 
src="http://doc.jsfiddle.net/_downloads/jsfiddle-desktop-1440x900-a.png" onLoad="    
    //need resize code here
    "/>​

I'm trying to resize image with original size ratio. But I can't access original size. When I want to select original size by this.height it gives me css size. And where to find original value? And how to change size? this.style.height = '200px' ????

2
  • Does setting style.width = 'auto' and style.height = 'auto' first and then measuring it work? Commented Apr 10, 2012 at 15:32
  • put it as <img width="200" height="200" then access with .height Commented Apr 10, 2012 at 15:32

2 Answers 2

1

How about using max-width and max-height in the CSS instead?

<img style="max-width:200px; max-height:200px" src="http://doc.jsfiddle.net/_downloads/jsfiddle-desktop-1440x900-a.png"/>​

jsFiddle example

Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at this fiddle. The only issue is that the naturalWidth/Height properties are not supported by Opera and IE < version 9 (according to dottoro).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.