Skip to content

Instantly share code, notes, and snippets.

@donovanh
Created March 23, 2012 15:17
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 donovanh/2171648 to your computer and use it in GitHub Desktop.
Save donovanh/2171648 to your computer and use it in GitHub Desktop.
Retina Article: Image tag reference
.logo {
background:url('/your_logo.png');
height:50px;
width:150px;
}
@media only all and (-webkit-min-device-pixel-ratio: 2) {
/* Media query for double density devices */
.logo {
background:url(/your_larger_logo.png); /* This is your double size image */
background-size:150px 50px; /* Make sure the double size image is displayed within the original size */
}
}
<img src="http://www.gravatar.com/avatar/068ce5abf934fd1ae13067300a36d160?s=125" class="hi-res-available" data-rel="http://www.gravatar.com/avatar/068ce5abf934fd1ae13067300a36d160?s=250" width="125">
// Query the device pixel ratio.
//-------------------------------
function getDevicePixelRatio() {
if(window.devicePixelRatio === undefined) return 1; // No pixel ratio available. Assume 1:1.
return window.devicePixelRatio;
}
// Process all document images marked as highres-available
// If the screen is (approx) 800 or bigger across (high-res tablet upwards)
//-----------------------------
function processTaggedImages() {
if (getDevicePixelRatio() > 1 && $(window).width() >= 700) {
$('img.high-res-available').each(function(i, image) {
// Set the src to that in the data-rel
var thisDataRel = $(this).attr('data-rel');
if (thisDataRel) $(this).attr('src', thisDataRel);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment