I am trying to resize an image with a percentage of itself. For example, I just want to shrink the image by half by resizing it to 50%. But applying width: 50%; will resize the image to be 50% of the container element (the parent element which maybe the <body> for example).
Question is, can I resize the image with a percentage of itself without using JavaScript or server side? (I have no direct information of the image size)
I am pretty sure you cannot do this, but I just want to see whether there are intelligent CSS only solution. Thanks!
I have 2 methods for you.
Method 1.
This method resize image only visual not it actual dimensions in DOM, and visual state after resize centered in middle of original size.
Browser support note: browsers statistics showed inline in
css.Method 2.
Note:
img.normalandimg.fakeis the same image.Browser support note: This method will work in all browsers, because all browsers support
cssproperties used in method.The method works in this way:
#wrapand#wrap img.fakehave flow#wraphasoverflow: hiddenso that its dimensions are identical to inner image (img.fake)img.fakeis the only element inside#wrapwithoutabsolutepositioning so that it doesn’t break the second step#img_wraphasabsolutepositioning inside#wrapand extends in size to the entire element (#wrap)#img_wraphas the same dimensions as the image.width: 50%onimg.normal, its size is50%of#img_wrap, and therefore50%of the original image size.