Last active
May 23, 2017 20:07
-
-
Save bergantine/2114328 to your computer and use it in GitHub Desktop.
CSS and JS to detect a retina display. #css #javascript #retina #responsive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style> | |
/* from http://www.mobilexweb.com/blog/ios-5-1-new-ipad-web-developers */ | |
@media screen and (-webkit-device-pixel-ratio: 2) { | |
/* retina display */ | |
} | |
</style> | |
<script> | |
// from https://developer.mozilla.org/en/DOM/window.matchMedia | |
if (window.matchMedia("(-webkit-device-pixel-ratio: 2)").matches) { | |
/* retina display */ | |
} | |
</script> |
But window.devicePixelRatio
is not very reliable on mobile devices ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, in JavaScript,
window.devicePixelRatio
is a safer alternative.(See: http://caniuse.com/#feat=devicepixelratio vs. http://caniuse.com/#feat=css-media-resolution)