Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  70] [ 6]  / answers: 1 / hits: 90597  / 15 Years ago, sat, january 16, 2010, 12:00:00

Right now, I'm using max-width to scale images to fit. However, they don't scale proportionally. Is there a way to cause this to happen? I'm open to Javascript/jQuery.



If possible, is there a way to do this without knowing the original dimensions of the image (maybe determine this using Javascript/jQuery)?


More From » jquery

 Answers
17

You need to specify the original width and height:



<img src=/whatever width=100 height=200 alt=Whatever />


And then use something like this in the CSS:



#content img { max-width: 100%; height: auto }


You could try this with jQuery if you don't have the width and height up front, but your mileage may vary:



$(function(){
$('#content img').load(function(){
var $img = $(this);
$img.attr('width', $img.width()).attr('height', $img.height());
});
});


Obviously replace #content with whatever selector you want to scope the functionality to.


[#97817] Wednesday, January 13, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tylerdamiena

Total Points: 139
Total Questions: 90
Total Answers: 118

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
tylerdamiena questions
;