Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created July 22, 2012 20:43
Show Gist options
  • Save madrobby/3161015 to your computer and use it in GitHub Desktop.
Save madrobby/3161015 to your computer and use it in GitHub Desktop.
Detect retina support. Tested on iOS, Android 2.x, Chrome for Android, Safari on MacBook Pro Retina, Firefox mobile (beta) for Android, Opera Mobile (Android)
function isRetina(){
return (('devicePixelRatio' in window && devicePixelRatio > 1) ||
('matchMedia' in window && matchMedia("(min-resolution:144dpi)").matches))
}
@madrobby
Copy link
Author

This should work on all browsers that support retina screens. Is this enough? Improvements welcome. This is for inclusion in my ebook http://retinafy.me

@ryanseddon
Copy link

Would be worth pooling knowledge in this pull request for high DPI detection Modernizr/Modernizr#443

@mathiasbynens
Copy link

Is -moz-device-pixel-ratio:1.0 really high-resolution? Shouldn’t that be 1.5? Or perhaps 1.1 or something, as the other check is for > 1.

@madrobby
Copy link
Author

@mathiasbynens good catch, that should be a !...

@madrobby
Copy link
Author

I have updated the gist and moved away from the moz-specific check and test for min-resolution:144dpi. This works on Firefox (beta) on the Nexus 7, where as all other approaches (moz--min-device-pixel-ratio and min-resoution:1.5dppx) didn't.

@noamtm
Copy link

noamtm commented Aug 29, 2012

Also works on Chrome on MacBook Pro Retina (where window.devicePixelRatio==2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment