Skip to content

Instantly share code, notes, and snippets.

@toddmotto
Created December 24, 2012 15:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toddmotto/4369618 to your computer and use it in GitHub Desktop.
Save toddmotto/4369618 to your computer and use it in GitHub Desktop.
Progressive enhancement technique for HTML5, retina detection to optimise your CSS. Usage in CSS: .no-retina { /* styles */ } .retina { /* styles */ }
// Retina Device, add 'retina' to HTML tag
if (window.devicePixelRatio >= 2) {
document.getElementsByTagName('html')[0].className += ' retina';
}
// No-retina Device, add 'no-retina' to HTML tag
else {
document.getElementsByTagName('html')[0].className += ' no-retina';
}
@phamann
Copy link

phamann commented Dec 24, 2012

It would be faster if you had "no-retina" on the html tag by default then did this:

if (window.devicePixelRatio >= 2) {
document.documentElement.className = document.documentElement.className.replace(/\bno-retina\b/g,"")+" retina ";
}
  • No getElementsBy look up
  • No string concat
  • Regex ftw

I use this pattern for js detection/progressive enhancement

@toddmotto
Copy link
Author

You could even just over-ride it if it's the only (or first) class addition.

if (window.devicePixelRatio >= 2) {
    document.documentElement.className = ' retina';
}

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