Created
March 30, 2012 13:35
-
-
Save alecperkins/2251600 to your computer and use it in GitHub Desktop.
Retina images with CSS background-image
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
<!doctype html> | |
<html> | |
<head> | |
<style> | |
div.image, div.image1x { | |
width: 1024px; | |
height: 768px; | |
background: url(image_1x.jpg) no-repeat; | |
-webkit-background-size: cover; | |
} | |
@media all and (-webkit-device-pixel-ratio: 2) { | |
div.image { | |
background-image: url(image_2x.jpg); | |
} | |
body { | |
color: red; | |
} | |
} | |
</style> | |
</head> | |
<body> | |
<div id="image_div" class="image"></div> This text is red if the device has a pixel ratio of 2. Tap the image to switch to the 1x version. | |
<script> | |
image_div.onclick = function(){ | |
if(this.className === 'image') { | |
this.className = 'image1x'; | |
} else { | |
this.className = 'image'; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment