Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image re-size to 50% of original size in HTML

Tags:

html

jquery

image

I'm trying to re-size a image in HTML, it's got width 314px and height 212px. I want to re-size it to 50%...

but using this I still get a bigger image instead of a half-size image.

<img src="image.jpg" width="50%" height="50%" /> 

What did I do wrong? Thanks

<html>     <head>     <title></title>     </head>         <body>         <div>         <img src="image4.png" width="50%" height="50%"/>         </div>     </body> </html> 

I resolved the above problem using jquery below:

$(document).ready(function(e) {         var imgs = document.getElementsByTagName('img');         var imgLength = imgs.length;          for(var i=0; i<= imgLength-1;i++){              var imgWidth = imgs[i].clientWidth;             var imgHeight = imgs[i].clientHeight;              $('img').eq(i).attr({width:imgWidth/2, height: imgHeight/2});              console.log(imgWidth);         }          console.log(imgLength);       }); 
like image 553
Sam Avatar asked Jun 20 '12 10:06

Sam


People also ask

How do you change the size of an original image in HTML?

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.

How do you resize an image proportionally 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).


2 Answers

You did not do anything wrong here, it will any other thing that is overriding the image size.

You can check this working fiddle.

And in this fiddle I have alter the image size using %, and it is working.

Also try using this code:

<img src="image.jpg" style="width: 50%; height: 50%"/>​ 

Here is the example fiddle.

like image 188
Asif Avatar answered Sep 22 '22 21:09

Asif


We can do this by css3 too. Try this:

.halfsize {     -moz-transform:scale(0.5);     -webkit-transform:scale(0.5);     transform:scale(0.5); }  <img class="halfsize" src="image4.jpg"> 
  • subjected to browser compatibility
like image 41
Nalan Madheswaran Avatar answered Sep 24 '22 21:09

Nalan Madheswaran