Dev.to WebDev 🛠 Dev 👁 1 📖 1 min read

Why Can a Half-Pixel Shift Make a 16 px SVG Icon Look Blurry?

The three bars below come from the same SVG path. Every PNG is 16 × 16 px. In the middle export, the bars look softer because the first number in the viewBox changed by 0.5. Change one number Save this as v

Why Can a Half-Pixel Shift Make a 16 px SVG Icon Look Blurry?

The three bars below come from the same SVG path. Every PNG is 16 × 16 px. In the middle export, the bars look softer because the first number in the viewBox changed by 0.5.

Three Inkscape PNG exports on pixel grids. At viewBox origins 0 and 1, each bar is one opaque pixel wide. At 0.5, each bar spans two partially transparent columns.

Change one number

Save this as viewbox-0.svg, then make two copies. Set the first viewBox value to 0.5 in one copy and 1 in the other. Keep the path and the other three values identical.

<svg xmlns="http://www.w3.org/2000/svg"
     width="16" height="16" viewBox="0 0 16 16">
  <path fill="#192d4a"
        d="M5 4h1v9H5z M8 4h1v9H8z M11 4h1v9h-1z"/>
</svg>

Export the page area in Inkscape 1.4 as a transparent PNG at 16 × 16 px. In these exports, the PNG alpha channel gives the following counts across all three bars.

viewBox Fully opaque pixels Partially transparent pixels
0 0 16 16 27 0
0.5 0 16 16 0 54
1 0 16 16 27 0

All 54 partially transparent pixels have an alpha value of 128 out of 255.

Why the whole-pixel shift stays sharp

A 16-unit-wide viewBox exported to 16 pixels gives a scale of one pixel per SVG unit. Increasing the horizontal origin by 0.5 shifts the drawing half a pixel to the left in the PNG. Each one-unit-wide bar then covers half of each of two neighboring pixel columns, so antialiasing spreads the bar across both columns. Increasing the origin by 1 shifts the drawing one full pixel to the left and puts both edges of each bar on pixel boundaries again.

The export size matters. At 32 × 32 px, with the same 16-unit viewBox, a shift of half an SVG unit corresponds to one output pixel. Check the icon at the size you actually ship.

If an icon softens after you adjust its framing, compare the first two viewBox values and inspect a PNG at the target size. In IconVectors, you can also use Pixel mode in the Preview panel to inspect how the icon rasterizes. Axialis guide to pixel-perfect SVG icons

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.