Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale up images in HTML5 without blurriness

Tags:

html

css

image

Is there a way to make an image bigger in HTML5 without it getting blurry.

For example, this image is 24x24: enter image description here

If you instead load it with a width of 150, it gets blurry. I want pixelated!

http://png-1.findicons.com/files/icons/734/phuzion/24/bug.png

Instead I would like to change the rendering engine to display like what you would see in photoshop. Something like this:

enter image description here

I only care about modern browsers.

like image 608
at. Avatar asked Mar 28 '13 18:03

at.


People also ask

How do I resize an image without losing quality 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 I resize an image without losing quality in CSS?

Use object fit property in your css, and give a fixed width and height to your image tag or respective class so that every image will have same width and height, Now Your Image won't be distorted.

Why are my pictures blurry in HTML?

Simple, the browser is trying to maintain the image aspect ratio based on the original size, resulting in a calculated height with a decimal value which in turn causes pixel compression, hence a blurry image.

How do I Auto scale an image in HTML?

The max-height property sets the maximum height of an element, and the max-width property sets the maximum width of an element. To resize an image proportionally, set either the height or width to "100%", but not both. If you set both to "100%", the image will be stretched.


2 Answers

If you just leave it to the browser, they will try to make it as smooth as possible nowadays (old IE versions didn't). So your best bet is to load it into a canvas and do the (nearest-neighbour) upscaling yourself. I'm not saying that this a sane idea, just putting it out there. :)

That, or have multiple versions of the image and switch them around somehow. There are a few technics you can try, like css sliding-doors.

like image 168
DanMan Avatar answered Oct 22 '22 13:10

DanMan


After some research: unfortunately what you want to do is limited by browser specific limitations. Firefox has a limited experimental code (image-rendering) that can force the browser to stop interpolating the pixels and thus causing the blurriness.

Unfortunately there is no other supported way of doing this currently. My suggestion would be for you to simply scale the image as I did in the edit to your original post.

Like this: enter image description here

Mozilla's documentation page: https://developer.mozilla.org/en-US/docs/CSS/image-rendering

like image 2
Michael Avatar answered Oct 22 '22 13:10

Michael