MDN self-hosted library · e01.org

Crisp pixel art look with image-rendering

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

html
<img
  src="technique_original.png"
  alt="small pixelated man, upscaled with width and height attributes, appearing blurry" />
css
img {
  width: 48px;
  height: 136px;
}
html
<img
  src="technique_original.png"
  alt="small pixelated man, upscaled with CSS, appearing crisp" />
css
img {
  width: 48px;
  height: 136px;
  image-rendering: pixelated;
}
html
<canvas id="game" width="128" height="128">A cat</canvas>
css
canvas {
  width: 512px;
  height: 512px;
  image-rendering: pixelated;
}
js
// Get canvas context
const ctx = document.getElementById("game").getContext("2d");

// Load image
const image = new Image();
image.onload = () => {
  // Draw the image into the canvas
  ctx.drawImage(image, 0, 0);
};
image.src = "cat.png";
css
img {
  /* 5.7x scale factor */
  width: 68.4px;
  height: 193.8px;
  image-rendering: pixelated;
}