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
Try it with width: 50vw; That means 50% of your viewport width. The same is working for the height, then its vh .
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).
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.
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');}
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With