{"id":197696,"date":"2015-03-18T08:11:02","date_gmt":"2015-03-18T15:11:02","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=197696"},"modified":"2021-09-13T15:40:51","modified_gmt":"2021-09-13T22:40:51","slug":"image-rendering","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/i\/image-rendering\/","title":{"rendered":"image-rendering"},"content":{"rendered":"\n

The image-rendering<\/code> property defines how the browser should render an image if it is scaled up or down from its original dimensions. By default, each browser will attempt to apply aliasing to this scaled image in order to prevent distortion, but this can sometimes be a problem if we want the image to preserve its original pixelated form.<\/p>\n\n\n\n

img {\n  image-rendering: auto;\n  image-rendering: crisp-edges;\n  image-rendering: pixelated;\n\n  \/* Safari seems to support, but seems deprecated and does the same thing as the others. *\/\n  image-rendering: -webkit-optimize-contrast;\n}<\/code><\/pre>\n\n\n\n

About those three possible values:<\/p>\n\n\n\n

  • auto<\/code>: default value that uses the browser\u2019s standard algorithm to maximize the appearance of an image.<\/li>
  • crisp-edges<\/code>: the contrast, colors and edges of the image will be preserved without any smoothing or blurring. According to the spec this was specifically intended for pixel art<\/a>. This value applies to images scaled up or down.<\/li>
  • pixelated<\/code>: as the image changes size the browser will preserve its pixelated style by using nearest-neighbour scaling<\/a>. This value only applies to images that are scaled up.<\/li><\/ul>\n\n\n\n

    This property can be applied to background images, canvas<\/code> elements as well inline images. It’s important to note however that testing these values at this time is particularly confusing because of the lack of consistent browser support.<\/p>\n\n\n

    Example<\/h3>\n\n\n

    Here’s a very small inline image that’s made up of four colored pixels:<\/p>\n\n\n\n

    <img src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAAXNSR0IArs4c6QAAACdJREFUCB1j9Pf3\/88ABMmMjCCKgQlMIhGMu3btAquY9mMDWBhDBQAutwfDrUlKzQAAAABJRU5ErkJggg==\"><\/code><\/pre>\n\n\n\n

    If we change the width of this inline image to 300px<\/code> then in Chrome (41) we’ll find the browser has attempted to optimize the image as best it can:<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    To preserve its original pixelated look we can use the following pixelated<\/code> value, like so:<\/p>\n\n\n\n

    img {\n  image-rendering: pixelated;\n}<\/code><\/pre>\n\n\n\n

    This results in the browser choosing an alternative algorithm with which to process the image. In this example Chrome will remove the default aliasing:<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    Unfortunately, after a lot of testing, it seems that browsers interpret the crisp-edges and pixelated values in very confusing ways at the moment so it’s important to note once again that this specification is in its very early days. For instance, Chrome appears to render pixelated<\/code> images in the same way that Firefox and Safari will render images with crisp-edges<\/code>.<\/p>\n\n\n

    QR code example<\/h3>\n\n\n

    Another use case of this property might be for QR codes where you want to increase its size without distorting it by using the standard anti-aliasing. Make sure to check this example in full-screen mode<\/a> to see the image stretch:<\/p>\n\n\n\n