CSS Code to Fix blurry image when scaling down

A large image shown in a smaller box can look blurry or jagged in some browsers and devices. Here are the fixes, in the order that you should try them.


1. Use an image of the right size

The best fix is not CSS. Do not scale a 3000px photo down to 300px. Save a smaller file, and give the browser a bigger one for high resolution screens with srcset.

<img src="photo-600.jpg"
     srcset="photo-600.jpg 600w, photo-1200.jpg 1200w"
     sizes="(max-width: 600px) 100vw, 600px"
     width="600" height="400" alt="Photo">

2. Keep the width and height as whole numbers

A size like width: 33.33% can produce a fractional pixel size, and the browser blurs the edges. Use fixed or rounded sizes where you can.


3. Fix blur with a transform

For images that are scaled with transform, this old trick forces the browser to use a different rendering path.

img {
  backface-visibility: hidden;
  transform: translateZ(0);
}

4. image-rendering

image-rendering tells the browser how to scale. Use pixelated for pixel art and icons that must stay sharp and square. Use crisp-edges or Chrome’s -webkit-optimize-contrast to keep the edges sharp.

.pixel-art {
  image-rendering: pixelated;
}

.sharp {
  image-rendering: -webkit-optimize-contrast;  /* Chrome and Safari */
  image-rendering: crisp-edges;                /* Firefox */
}
Output in the browser

Note: pixelated is not a good choice for photos, because it makes them look blocky. For logos and icons, use an SVG file, which is always sharp at any size.

Learn more about the similar topics:
Tutorials
CSS Colors
CSS Background
CSS Gradients
CSS Images
Exercises & Assignments
Write CSS to change the text colour and background of a paragraph
Interview Questions & Answers
How to add a background image in CSS?
What is the difference between opacity and rgba in CSS?