Skip to content

Instantly share code, notes, and snippets.

@alecperkins
Created March 30, 2012 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alecperkins/2251600 to your computer and use it in GitHub Desktop.
Save alecperkins/2251600 to your computer and use it in GitHub Desktop.
Retina images with CSS background-image
<!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