Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting the 50% of original picture width and height using css

is there any way of setting the 50% of original picture width and height using css? for example i have this code:

    <img src="http://content.captive-portal.com/files/ign/movie-news/content/2b.jpg">

I don't specify any width and height in html, but I want it to be 50% of the original height and width. Can I use CSS to do that?

this doesn't work:

img{width:50%; height:50%}

Is there any way to overcome this? Thanks

like image 609
Piotr Ciszewski Avatar asked Feb 21 '13 13:02

Piotr Ciszewski


People also ask

How do I make my image 50% CSS?

Try it with width: 50vw; That means 50% of your viewport width. The same is working for the height, then its vh .

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 do I arrange image size in CSS?

We can resize the image by specifying the width and height of an image. A common solution is to use the max-width: 100%; and height: auto; so that large images do not exceed the width of their container. The max-width and max-height properties of CSS works better, but they are not supported in many browsers.


2 Answers

You can scale the image as described here: CSS image resize percentage of itself?

img {-webkit-transform: scale(0.5); /* Saf3.1+, Chrome */
 -moz-transform: scale(0.5); /* FF3.5+ */
  -ms-transform: scale(0.5); /* IE9 */
   -o-transform: scale(0.5); /* Opera 10.5+ */
      transform: scale(0.5);
         /* IE6–IE9 */
         filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.9999619230641713, M12=-0.008726535498373935, M21=0.008726535498373935, M22=0.9999619230641713,SizingMethod='auto expand');}​
like image 195
Jacob Avatar answered Nov 03 '22 01:11

Jacob


As I said in my comment. Setting width: 50% in CSS will only set the image's width to 50% of its containing element.

You will need to achieve this using JavaScript. jQuery makes it quite straightforward, for example:

$('img').each(function() {  
    var the_width = $(this).width(),
        new_width = Math.round(the_width / 2);
    $(this).width(new_width);
});
like image 30
BenM Avatar answered Nov 03 '22 00:11

BenM