Dev

What devicePixelRatio Actually Means

CSS pixels, device pixels and why your 4K screen tells websites it is 2560 wide. With code.

By darkscreen.online crew4 min read
What devicePixelRatio Actually Means>_ devwhatdevicepixelratioactually means

window.devicePixelRatio is one of those numbers every front-end dev has seen and few can explain precisely. It is the ratio between physical device pixels and CSS pixels. Get it wrong and your canvas is blurry, your images are oversized, and your “1px” borders are not 1 pixel. Here is the full picture, with code.

CSS pixels vs device pixels

A CSS pixel is a layout unit. It is defined so that things look roughly the same size across devices at a typical viewing distance. A device pixel is an actual point on the panel. DPR tells you how many device pixels fit across one CSS pixel.

  • DPR 1: 1 CSS px = 1 device px. Classic 1080p monitor at 100% scaling.
  • DPR 2: 1 CSS px = 2 × 2 device px. Most MacBooks and many phones.
  • DPR 3: 1 CSS px = 3 × 3 device px. Most current flagship phones.
  • Fractional DPR: 1.25, 1.5, 1.75 on Windows with display scaling.

Real-world examples

DevicePanelScalingCSS sizeDPR
27" 1440p monitor2560 × 1440100%2560 × 14401
27" 4K monitor3840 × 2160150%2560 × 14401.5
14" laptop2560 × 1600200%1280 × 8002
Modern phone~1170 × 2532—~390 × 8443

Zoom changes it

Browser zoom multiplies DPR. At 125% zoom on a DPR 2 screen, devicePixelRatio reports 2.5. That is why your layout shrinks in CSS pixels when you zoom in. Reset with Ctrl + 0 before testing anything.

Sharp canvas

The classic bug: a canvas sized in CSS pixels looks blurry on high-DPR screens because its backing store has too few pixels. Fix it by scaling the backing store:

const dpr = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
canvas.width = Math.round(rect.width * dpr);
canvas.height = Math.round(rect.height * dpr);
const ctx = canvas.getContext('2d');
ctx.scale(dpr, dpr); // draw in CSS px from here on

Our PNG generator is the opposite case: it renders at exactly the pixel size you type, independent of DPR, because the output is a file, not a display.

Responsive images

Use srcset with width or density descriptors so the browser picks the right file for the DPR:

<img src="hero-800.jpg"
     srcset="hero-800.jpg 800w, hero-1600.jpg 1600w, hero-2400.jpg 2400w"
     sizes="(min-width: 1024px) 50vw, 100vw"
     width="800" height="450" alt="...">

On a DPR 3 phone showing the image at 390 CSS px wide, the browser needs roughly 1170 device pixels, so it picks the 1600w file.

Hairlines

A 1px border is 1 CSS pixel, which is 2 or 3 device pixels on high-DPR screens. If you want a true device-pixel hairline, you can use 0.5px on DPR 2 screens, or a media query:

@media (min-resolution: 2dppx) {
  .hairline { border-width: 0.5px; }
}

Support for sub-pixel borders is good in modern browsers, but rendering can vary. Test on real hardware.

Reacting to changes

DPR changes when a window moves between monitors or the user zooms. Listen with a resolution media query that re-registers itself:

function watchDpr(cb) {
  const mq = matchMedia(`(resolution: ${devicePixelRatio}dppx)`);
  mq.addEventListener('change', () => { cb(devicePixelRatio); watchDpr(cb); }, { once: true });
}

screen.width is CSS pixels too

screen.width and screen.height report CSS pixels, not device pixels. Multiply by DPR to estimate the physical panel. That is exactly what our resolution checker and the homepage terminal do.

Performance trade-offs

High DPR means more pixels to paint. A full-screen canvas at DPR 3 on a phone is nine times the pixels of the same canvas at DPR 1. Games and data visualisations often cap DPR, for example Math.min(devicePixelRatio, 2), to keep frame rates stable while still looking sharp. Images are similar: shipping 3× assets everywhere wastes bandwidth when 2× looks nearly identical at normal viewing distances.

Testing across DPRs

Browser dev tools let you emulate DPR in device mode, which is enough for layout checks. For sharpness, nothing beats real devices: a DPR 1 monitor, a DPR 2 laptop and a DPR 3 phone cover most of your users. Check text, thin borders, icons and canvas output on each. Our resolution checker confirms what each device reports.

SVG, icons and fonts

Vector content scales cleanly with DPR because the browser rasterises it at device resolution. SVG icons and web fonts stay crisp on any screen, which is why icon fonts and inline SVG replaced sprite sheets. Raster icons need 2× or 3× versions. One catch: thin strokes in SVG icons designed on a 1× grid can look slightly soft on fractional DPRs like 1.25, because lines land between device pixels. Designing icons with strokes of 1.5 to 2 CSS pixels helps.

TL;DR

DPR = device px ÷ CSS px. Scale canvases by it, ship images with srcset, remember zoom changes it, and multiply screen.width by it to get real pixels. Check your own numbers live on the resolution checker, and see how DPR affects motion tests in 60 vs 144 vs 240 Hz.

Keep one more thing in mind for analytics and testing: reported screen sizes are CSS pixels, so a dashboard full of 390 × 844 phones does not mean low-resolution screens. Pair viewport data with DPR before deciding which image sizes to generate. Most traffic today is DPR 2 or higher.

FAQ

No. Windows scaling and browser zoom produce values like 1.25, 1.5 or 2.5.

Higher DPR means more pixels to paint. Canvas and WebGL apps often cap DPR at 2 to keep frame rates up.

It reports CSS pixels. Multiply by devicePixelRatio for an estimate of the physical resolution.

Next up

Partner with us

Put your link in front of devs, gamers and night owls

Sponsored links in tested guides, featured tool slots and newsletter shout-outs for dev tools, peripherals and gaming hardware. Labelled, relevant, never inside the fullscreen screen.

One email a month. No spam, unsubscribe any time.