Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize image in HTML using percentage as parameters?

Tags:

html

css

image

I would like to resize an image on my website. I know how to do it by either resizing the image before or calculating the width and height and setting the values in pixels. But I use the same picture multiple times with different dimmensions, so it would take me less time if I could resize the image relatively to its own size.

<img src='images/logo-beta.png' id="logo" height="75%" width="75%"/>

I have tried this, however the problem is that the size is set relative to its parent element.

like image 757
Borut Flis Avatar asked Oct 07 '12 15:10

Borut Flis


People also ask

How do I make an image a percentage in HTML?

Try It: Set the width of an image using a percentage and resize your browser window. For the width attribute, you can also use percentages, where 100% is the total space available. So, if you wanted an image to be one quarter the width of the window, you could set width to 25% (one quarter of 100% is 25%).

How do I resize a photo percentage?

Resizing the image using a percentage is done the same way as if you were using pixel values. Just double-click inside the Width or Height value box and type in a new percentage. When you're done, click the OK button and Photoshop will resize the image to whatever percent value you entered.

How do I resize an image by percentage in CSS?

Resize images with the CSS width and height properties Another way of resizing images is using the CSS width and height properties. Set the width property to a percentage value and the height to "auto". The image is going to be responsive (it will scale up and down).

How can we resize the image in HTML using which attribute?

The width attribute specifies the width of an image, in pixels. Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.


2 Answers

There's no way to do what you want automatically using HTML or CSS alone. You'll need to use JavaScript to get the image's dimensions, then calculate a percentage of those dimensions and reapply them to the image in pixels

like image 101
danwellman Avatar answered Oct 20 '22 04:10

danwellman


There is a method, but it isn't perfect. It requires a wrapping element whose display is set to 'inline-block' and the image is resized using 'max-width'.

The issue is that the parent element retains the image's original width, which could cause problems depending on your requirements.

Example: http://jsfiddle.net/amustill/GnEw5/

like image 44
amustill Avatar answered Oct 20 '22 03:10

amustill