How Pixel Converter Works: Technical Deep-Dive
A pixel converter looks like a calculator with two text boxes. Underneath, it is a small system that has to reconcile three different definitions of "pixel" (CSS, device, image), look up hardware-level data for a long tail of devices, and apply the right physical-unit conversion without losing precision. Naive implementations get visibly wrong answers on modern hardware. This deep dive unpacks how a correct pixel converter is built and why each piece matters.
The core math
The single equation everything reduces to is:
inches = pixels / dpi
with the constant 1 inch = 25.4 mm for the metric outputs. That looks trivial. The work is in deciding what number to put in for pixels and dpi, because each side has multiple legitimate interpretations.
Three definitions of "pixel"
When a designer types "100 px" into a converter, they could mean any of three things:
CSS pixel is a logical unit defined by the W3C. On a "reference" display it is 1/96 of an inch. The browser exposes this unit through window.innerWidth, getBoundingClientRect(), CSS values like width: 100px, and Figma's default ruler. On a Retina screen (DPR > 1) one CSS pixel is rendered using multiple device pixels, but layout math always uses the logical CSS pixel.
Device pixel (a.k.a. physical pixel, hardware pixel) is one addressable cell of the screen's pixel grid. An iPhone 15 has a screen that is 1179 × 2556 device pixels. The browser exposes this through screen.width * devicePixelRatio (with some platform variation). It is the actual unit that hardware fires.
Image pixel is one cell of a bitmap file. A PNG that is "1000 × 1000 pixels" has 1,000,000 image pixels regardless of where you render it. When that image is displayed in a <img width="500"> element on a Retina screen at DPR 2, 1000 image pixels are mapped to 500 CSS pixels, which are then rendered using 1000 device pixels. Three different counts, three different meanings, same image.
A correct pixel converter has to ask the user (explicitly or by default) which of these three they mean. The Screen Ruler converter defaults to CSS pixels because that is what most users have in front of them (Figma values, CSS code, DevTools), and exposes a "device pixel" toggle for the cases where it matters.
Three definitions of "DPI"
The converter side has parallel ambiguity:
Print DPI is the rate at which a printer lays ink dots on paper. Typical values: 72 (early laser), 96 (early-screen-as-print), 150 (newspaper), 300 (standard photo), 600 (fine art), 1200 (high-end label).
Display PPI is the rate at which the screen physically packs pixels per inch. Values vary by hardware: 110 PPI (typical desktop monitor), 220 PPI (MacBook Pro Retina), 326 PPI (iPhone 13/14), 460 PPI (iPhone Pro Max), 565 PPI (Galaxy S24 Ultra).
CSS-reference DPI is the W3C fiction of 96. The CSS spec defines 1 inch = 96 px as a layout anchor, regardless of the actual screen PPI. Browsers internally honor this for layout while rendering at higher density.
The converter has to know which DPI/PPI is relevant to the user's situation. The Screen Ruler converter presents the three categories explicitly:
- Print mode: enter your printer's DPI.
- Display mode (auto): detect the user's device PPI from a database lookup.
- Display mode (manual): enter a specific device's PPI for "how big on iPhone 15" hypotheticals.
- CSS mode: assume 96 DPI for pure layout questions.
Device PPI auto-detection
The interesting engineering is in the auto-detection. The browser does not directly expose the screen's physical PPI — it would be a privacy issue (fingerprinting) and platforms intentionally restrict it. The converter has to infer.
The Screen Ruler approach uses three signals:
- userAgent string to identify the device class: iPhone, iPad, Mac, Android, Windows.
- screen.width × screen.height × devicePixelRatio to compute the device pixel count of the screen.
- window.matchMedia for media queries like
(min-device-pixel-ratio: 3)to pin down the DPR family.
The combination of these signals uniquely identifies most modern devices. iPhone 15 has screen.width = 393, devicePixelRatio = 3, userAgent containing "iPhone OS 17". Cross-referenced against the device database, that resolves to a Apple A16 / iPhone 15 entry with manufacturer-specified PPI = 460.
For ambiguous matches (iPhone 13 vs iPhone 14 have identical screen specs), the converter picks the most likely current device and exposes a manual override.
For devices not in the database (custom builds, rare Androids), the converter falls back to a screen-dimension-based estimate: it asks the user to confirm the screen's physical diagonal size, then computes PPI from sqrt(width² + height²) / diagonal. This is the same approach the user would use with the Screen Ruler PPI calculator.
The Retina trap
The single mistake that breaks naive pixel converters is failing to account for devicePixelRatio. Consider a converter that does:
const ppi = window.screen.width / physical_screen_width_in_inches;
On a Retina iPhone with screen.width = 393 (CSS pixels) and a physical screen width of about 2.78 inches, this gives PPI ≈ 141. The actual hardware PPI is 460. The naive converter is wrong by a factor of devicePixelRatio = 3.
The fix is to use screen.width × devicePixelRatio for device pixel count, then divide by the physical diagonal. The Screen Ruler converter handles this internally — but if you write your own, watch for this.
The same trap appears in reverse when rendering: if you compute "100 px = 8.7 mm at 290 PPI" but then forget that the user will see 100 CSS pixels rendered as 300 device pixels on a DPR-3 phone, you mislead them about what they will actually see. The Screen Ruler converter resolves this by being explicit about which pixel mode the input refers to and showing the user both numbers when ambiguous.
Bidirectional conversion
Mathematically, bidirectional is trivial: invert the equation. UI-wise, it matters how you handle:
- Which field is "input" vs "output": locking direction after the user types prevents the math from feeding back into itself and creating jitter.
- Unit normalization: under the hood the converter stores values in a canonical unit (microns or floating-point mm), and converts to/from the user's selected display unit at the edge. This avoids accumulating rounding error across multiple unit changes.
- Rounding rules: display rounding (2 decimal places for mm, 4 for inches) is applied only at render time. Internal precision stays at f64.
The Screen Ruler converter computes in microns internally. A 1 px input at 96 DPI becomes 264.583… microns, kept at floating-point precision for any cross-unit display.
DPI database vs runtime measurement
A pixel converter can either:
a) Maintain a static device database (manufacturer specs for every device's PPI), looked up via auto-detection.
b) Ask the user to calibrate against a physical reference object (credit card on screen) and compute PPI from the calibration.
(a) is fast and zero-friction but only works for listed devices. (b) is universal but requires a calibration step. The Screen Ruler converter uses (a) as the default and offers (b) as a "calibrated mode" for users who want to verify or who have an unlisted device. The two modes can be combined: auto-detect, then offer "fine-tune by holding a credit card up" if the user wants higher confidence.
Edge cases
A correct converter handles:
- DPR fractional values: some Android devices report DPR = 2.625 or 3.5. The converter cannot assume integer multipliers.
- Zoom level: if the user has zoomed the browser to 125%,
window.innerWidthreflects the zoomed size, but PPI does not change. The converter has to use unzoomed reference dimensions or warn. - Orientation changes: rotating the device changes
screen.widthandscreen.heightsemantically but the physical PPI is the same. The converter listens for orientation events and re-detects. - External monitors: a laptop with an external 4K monitor has two different PPI values depending on which screen the window is on.
screen.availWidthreflects only the current screen; multi-monitor setups require user confirmation. - Unit consistency in print mode: when the DPI is specified for the printer and the user enters a measurement in mm, the conversion goes
mm → inches → pixels at DPIto maintain precision.
Performance
Pixel conversion is one floating-point operation. The performance budget is dominated by:
- Device database lookup: O(1) hash lookup on hash of (userAgent, DPR, screen dimensions).
- Rendering math: every keystroke triggers a re-render. The converter debounces input events to ~60 fps (16 ms) to keep typing smooth.
- Server-side rendering: the converter is mostly client-side because device detection requires browser APIs. The SSR shell renders the form with the default 96 DPI; client-side mount runs detection and updates.
The whole pipeline (keystroke → math → re-render) is under 2 ms even on low-end Android. No optimization beyond standard React rendering is needed.
Why "just use Photoshop"
Photoshop's "Image Size" panel does the same math, but it has three handicaps for ad-hoc conversion:
- Image-bound: it operates on an open image. Asking "how many pixels is 8 mm at 326 PPI" requires creating a dummy image first.
- No device PPI database: you supply the PPI manually.
- Heavyweight: app launch + open file + read panel is 30+ seconds vs 5 seconds for a web converter.
The converter exists because the math is universal but the workflow context for asking the question is varied — and a focused tool beats a panel inside a 2GB app for one-shot questions.
Try it
The Screen Ruler Pixel Converter implements everything above: auto-detected device PPI, custom print DPI, bidirectional input, CSS-vs-device-pixel handling, full-precision internal math. Open it, type a number, and watch the math you just read about run in real time.
Related Articles
15 Questions About Aspect Ratio Calculator Answered
Common questions about aspect ratio calculators — how they work, when to use one, how to interpret outputs, and the edge cases that trip up first-time users.
Aspect Ratio Calculator for Professionals: Advanced Use Cases
How video editors, broadcast engineers, motion designers, and front-end developers use aspect ratio calculators in production workflows — beyond the 16:9 basics.
Using Aspect Ratio Calculator and Screen Ruler Together
A workflow guide for pairing the aspect ratio calculator with the on-screen ruler — matching physical print dimensions to display ratios, verifying device screen ratios, and bridging from pixels to physical inches.