Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize images with javascript before assign them to an img tag

I'm very very new on HTML and CSS development.

I have a HTML page with an img tag. This tag will hold some images. These images has different sizes. I set up a width and height to img tag using CSS.

All images will be shown using JavaScript.

May I need to resize images with JavaScript before I assign an image to IMG tag? Maybe, I don't need to do that because I set up IMG size using CSS. Proportions will be constrained?

Sorry for my English.

like image 769
VansFannel Avatar asked May 29 '11 09:05

VansFannel


People also ask

How do you scale an image in JavaScript?

All this can be done in JavaScript, since you have access to input image original height ( img. width ) and width using ( img. width ). For example, we can fit the output image in a container of 300x300 dimension.

How do I resize an image as an IMG tag?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels. For example, the original image is 640×960.

Can you change an images size using JavaScript?

In plain JavaScript, you can directly modify the CSS width and height property of the image. The following example demonstrates this by altering the CSS width property by specifying the absolute value in pixels and leave the browser to calculate the corresponding height while maintaining the aspect ratio.


1 Answers

The aspect ratio wont be maintained if you have set both the height and width using css. But if you specify any one of the properties using the html img tag then the aspect ratio would be maintained.

like image 107
Sujit Agarwal Avatar answered Sep 30 '22 08:09

Sujit Agarwal