Skip to main content
PixelRatio gives access to the device’s pixel density, allowing you to load appropriately sized images and create pixel-perfect layouts.

Methods

get()

Returns the device pixel density. Returns:
  • Device pixel ratio as a number
Typical Values:
  • 1 - mdpi Android devices (160 dpi)
  • 1.5 - hdpi Android devices (240 dpi)
  • 2 - iPhone 4, 4S, 5, 5c, 5s, 6, 7, 8, SE; xhdpi Android devices (320 dpi)
  • 3 - iPhone 6 Plus, 7 Plus, 8 Plus, X, XS, XR, 11, 12; xxhdpi Android devices (480 dpi)
  • 3.5 - Nexus 6, Pixel XL
Example:

getFontScale()

Returns the scaling factor for font sizes. This is the ratio used to calculate absolute font size, reflecting the user’s font size preference. Returns:
  • Font scale as a number (defaults to pixel ratio if not set)
User Settings:
  • Android: Settings > Display > Font size
  • iOS: Settings > Display & Brightness > Text Size
Example:

getPixelSizeForLayoutSize()

Converts a layout size (dp/pt) to pixel size (px). Guaranteed to return an integer. Parameters:
  • layoutSize: Size in density-independent pixels
Returns:
  • Size in physical pixels (rounded to nearest integer)
Example:

roundToNearestPixel()

Rounds a layout size (dp/pt) to the nearest layout size that corresponds to an integer number of pixels. Parameters:
  • layoutSize: Size in density-independent pixels
Returns:
  • Rounded size that maps to whole pixels
Example:

Common Use Cases

Loading Appropriately Sized Images

Creating Pixel-Perfect Hairlines

Responsive Font Sizes

Pixel Grid Snapping

React Native automatically rounds pixel values to avoid blurry elements. All layout calculations use arbitrary precision numbers, but when setting native element positions and dimensions, values are rounded relative to the root (not the parent) to avoid accumulating rounding errors. Use roundToNearestPixel() when you need to ensure a calculated value snaps correctly to the pixel grid: