Blurry downscaled images in Chrome
Asked Answered
S

12

83

I am using gravatars and it's rather often when I downscale them with css, and I believe Google Chrome used to do it properly until recently ( I may be wrong, not sure when exactly the problem started to occur ) but now, images get blurred when downscaled, and that happens only in Chrome, FF downscales pretty good. I tried using image-rendering but it doesn't solve the problem. Can someone give me a hint what is the best way to go about it?

The example can be found here, open it in Chrome and then in FF, it should be way more blurred in Chrome than in FF.

Thank you

Stonge answered 19/6, 2016 at 11:13 Comment(5)
superuser.com/questions/530317/… outlines this issue quite well.Ramakrishna
@MathiasW This is a different issue, I am not zooming anything in here. Just open that link in FF and chrome ( without zooming in ) and see the image in chrome being blurred.Stonge
This issue seems to be back 2017-05-10, I pretty sure images didn't used to be fuzzy, but currently on Chrome desktop they are fuzzy, the answer webkit setting fixes the issue (and images on Firefox are fine)Hemeralopia
It'd be nice to see your full code. For example "image-rendering doesn't work" doesn't help much if we don't know the value you used (especially since it's used in the accepted answer)Messier
In my case the problem was with the image itself, as it had an odd height.Daimon
M
163

I found the exact same issue on Mac: Firefox downscales the image really well, while Chrome makes it look blurry, which is very bad. I couldn't care less about rendering time or speed, I need the logo to look GOOD!

I found the following CSS rule fixing Chrome for Mac

image-rendering: -webkit-optimize-contrast;
Marylandmarylee answered 14/2, 2017 at 0:43 Comment(15)
saved my day - also works with background images. It's odd that Chrome really does a bad job downscaling PNGs without this rule.Mors
Beware: this caused my images to be jagged/pixelated in Safari.Glamour
Is there any way to automatically apply this for all web pages in Chrome? Like a global CSS style?Cahier
This is not working for me in Chrome 64.0.3282.186 for Mac.Noenoel
@Noenoel You may want to try image-rendering: pixelated;Juror
Still an issue in 2020, and this answer still helped me (on Chrome 83)Eyetooth
Works really well! Thanks. For a logo though, you should use svg. Then it will always look perfectly sharp.Villain
Working like a charm (both chrome & safari)!Agora
You legend!! Worked like a dream! Thanks, dude!Hiedihiemal
Wow, it is really cool, although still a bit worse than in FF, now it doesn't burning eyes at leastBaines
Doesn't work for Chrome 87 on Linux Mint.Rebba
This no longer has any effect in Chrome on MacNipha
This still causes jagged/ugly edges in Safari on macOS as of July 2021. It seems like transform: translateZ(0); is the only solution that works everywhere.Pictorial
Thanks this also worked for me on Linux (Ubuntu 20.04). Originally it looked blurry in Ungoogled Chromium but clear in Firefox, now is clear in bothConstituent
None of what is on the page works with Chrome 93. Very weird. Also I notice that the zoom page at 100% renders downscaled images blurry but 110% renders perfectly crisps images. There really is a problem with Chrome. (Also Firefox is perfectly crisp whothout anything applied to it or any page Zoom used)Ticklish
H
24

i find used transform: translateZ(0); is work.

by the similar question:How to prevent blur on image resize in Chrome?

Hertel answered 7/4, 2021 at 3:16 Comment(3)
Can you provide information why this solves the question's problem?Messier
Tested for Chromium-based (Chrome, Edge and Brave tested). According to this article, it makes the rendering be made by 3D hardware acceleration/GPU, and may cause issues with CSS animations, so better be used with caution. blog.teamtreehouse.com/…Helix
Worked like a charm, up until and including Chrome 81. Doesn't seem to work with 82 and 83 (dev) anymore.Pifer
E
15

It seems that transform: translateZ(0); does not work anymore.
The only property I found which had any effect was image-rendering: -webkit-optimize-contrast; (note: this has a much different effect on iOS safari, where it makes the image very pixelated, so you'll only want to enable it on chrome and edge)

Here is a comparison using this image: <img src="https://i.sstatic.net/acaio.jpg" style="width: 244px; height: 244px;"> (on windows 10) comparison And a close-up of the text on the sign: I think firefox's rendering is significantly better, but optimize-contrast does help. close-up comparison

Euchromatin answered 13/9, 2021 at 0:50 Comment(2)
i am f*ing flabbergasted that this is a problem in 2021Camara
I suspect -webkit-optimize-contrast may have been removed recently. it seems to have no effect now (tested in ms edge version 99.0.1150.11, on linux)Euchromatin
A
14

Update

I didn't realize that the image size after using 2x matched the target size and the browser wasn't downscaling. This solution only works if you can use a fixed size container for the image.

tl;dr

Set the image scale and Chrome will downscale properly. Tested in Chrome 84.

The important part is using srcset with 2x at the end.

<img srcset="image-2x.png 2x" alt="alt">

Full Answer

I tried image-rendering: -webkit-optimize-contrast. It improved the rendered image in Chrome but also gave me a bad looking version of the image in Safari.

At first, I needed downscaling because the 2x version of the image is still needed for Retina displays (otherwise the upscaling might look blurry). So I decided to create the two versions (1x and 2x).

After adding both, I saw that if I only used the original 2x image but with the 2x specified in srcset then the image will not render blurry anymore.

Armor answered 19/7, 2020 at 22:18 Comment(4)
This is actually the correct approach, too bad your answer got downvoted ¯_(ツ)_/¯. Thanks! developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/…Collectivize
This did the trick for me, and not the "image-rendering" from the above answers. Upvoted this because no idea why this got downvoted.Buckwheat
In 2021 this REALLY needs to be accepted as the correct answer for this issue.Norford
srcset with scale didn't work for me in my casePterous
C
8

Pastullo's answer using image-rendering totally fixes the blurry image problem on Chrome, but the image is then pixelated on Safari. This combination of media queries worked for me to set the property on Chrome 29+ and unset it on Safari 11+:

@media screen and (-webkit-min-device-pixel-ratio:0)
and (min-resolution:.001dpcm) {
  img {
    image-rendering: -webkit-optimize-contrast !important;
  }
}

/* Unset for Safari 11+ */
@media not all and (min-resolution:.001dpcm)
{ @supports (-webkit-appearance:none) and (stroke-color:transparent) {
  img {
    image-rendering: unset !important;
  }
}}
Cretic answered 10/9, 2021 at 20:52 Comment(0)
R
7

Use will-change: transform; in Chrome for Windows and image-rendering: -webkit-optimize-contrast; for Mac.

Rip answered 10/5, 2021 at 18:46 Comment(1)
UPD: do not use will-change: transform; on pages that contain a lot of images. This feature uses GPU to render images so it may slow down the browser.Rip
D
4

Using transform: translateZ(1px); fixed the issue for me in Chrome, while not visually impacting other browsers.

Determinism answered 31/3, 2022 at 8:24 Comment(0)
S
4

This will give you clean sharp images in scaled images in chrome. You need both translateZ(0) and scale not (1)

img {
border: none;
display: block;
transform: translateZ(0) scale(0.999999);
}

But if using any hover scale, make sure you also add in the translateZ(0) again.

i.e

img:hover {
transform: translateZ(0) scale(1.1);
}
Slowwitted answered 5/4, 2022 at 15:27 Comment(0)
L
3

I may complete this thread.

I found what could be considered as a bug (or maybe it's a feature).

If you downscale with CSS a logo in a big square bitmap image (500px x 500px JPEG for example) to 63px x 63px square, the result is very blurry in Chrome Version 97.0.4692.99 or any WebKit based browser I have on my computer. (Opera, Edge) But not Firefox.

Change to 64px x 64px, suddenly the result is better.

I suppose WebKit consider small sized images as unimportant and therefore could be scaled with a different, faster yet blurry algorithm.

If you scaled down logos on your website to 63px or 60px, consider making them a little bigger. Test in your inspector to verify if the rendering is satisfying.

You're all welcome!

Lateritious answered 27/1, 2022 at 14:4 Comment(1)
Thanks, I went through all the answers none of them worked for me.Convict
E
2

I propose another track because I was in the same situation: images slightly blurred under chrome but impeccable under firefox. Ctrl + "0" solved the problem. I had to one day use the zoom (Ctrl + "+" or "-") and did not reset it completely ...

Edging answered 13/8, 2019 at 7:29 Comment(0)
M
1

I've found that the best way to resolve this issue is to just use an svg. Another option is to use css media queries to load adaptive images sizes.

Mentholated answered 14/2, 2017 at 0:45 Comment(4)
For a lot of images, this is not an option. Also, they're not fully supported (see the notes for IE): caniuse.com/#feat=svgInfinitude
Not even Microsoft wants* you to care about that anymore. *) zdnet.com/article/…Juror
Not sure why this answer is rated so low. This is actually the most optimal solution. Most modern design tools support svg export now for any assetBarrybarrymore
2021 approved. Upvote this guy.Backward
I
1

I noticed that if the images are scaled 2 times more than the specified width and height they display perfectly.

For example, the "some-image.jpg" dimensions for the code below should be: 200x200

<img src="some-image.jpg" alt="" width="100" height="100">
Irs answered 10/6, 2022 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.