{"id":55,"date":"2011-05-23T18:52:41","date_gmt":"2011-05-23T18:52:41","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2011\/05\/detecting_retina_displays_from_javascript_and_css-shtml\/"},"modified":"2011-05-23T18:52:41","modified_gmt":"2011-05-23T18:52:41","slug":"detecting_retina_displays_from_javascript_and_css-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/","title":{"rendered":"Detecting Retina Displays From JavaScript And CSS"},"content":{"rendered":"<p>\nI was looking at how to target Apple&#8217;s retina display for high resolution web pages earlier.\n<\/p>\n<p>\nMy first attempt was to look at the user agent string, but an up to date iPhone 4 sends the same string as an up to date iPhone 3GS.\n<\/p>\n<p>\nA quick look around the internet reveals the solution for JavaScript is to actually use the <code>window.devicePixelRatio<\/code> property.\n<\/p>\n<p>\nFor a standard browser, this will be 1. However, on a retina display it will 2 &#8211; meaning twice the pixels.\n<\/p>\n<p>\nFor example&#8230;<\/p>\n<div class=\"code\"><code>if (window.devicePixelRatio &gt;= 2) {<br \/>\n\/\/ retina display<br \/>\n} else {<br \/>\n\/\/ standard display<br \/>\n}<br \/>\n<\/code><\/div>\n<\/p>\n<p>\nYou can check your current devicePixelRatio using the following&#8230;\n<\/p>\n<div class=\"code\"><code>&lt;script type=\"text\/javascript\"&gt;<br \/>\ndocument.write(\"Your browser has a devicePixelRatio of \" + window.devicePixelRatio + \"\");<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/div>\n<p>\n<script type=\"text\/javascript\">\ndocument.write(\"<\/p>\n<p>Your browser has a devicePixelRatio of \" + window.devicePixelRatio + \"<\/p>\n<p>\");\n<\/script>\n<\/p>\n<p>\nApple aren&#8217;t the only ones using this in their webkit based browsers, you can read how to <a href=\"http:\/\/developer.android.com\/guide\/webapps\/targeting.html\">target device pixel density on Android<\/a>. Android defines a density of 1 to be medium density, anything 1.5 or above as high density and anything under 0.75 to be low density.\n<\/p>\n<p>\nOn the client side we can use this to decide if we want to bring in retina quality imags, or standard ones. There is a <a href=\"https:\/\/github.com\/baotuo\/jQuery-Retina-Plugin\">jQuery plugin called Retina<\/a> that does this.\n<\/p>\n<p>\nIf want to to pass display information back to the server to decide what images to render, this is a bit more complicated.\n<\/p>\n<p>\nThe best way to approach this would be to have a JavaScript set a cookie containing the <var>devicePixelRadio<\/var> and have the server read this back.  A quick Google reveals that Ben Doran is doing just this, <a href=\"http:\/\/www.bdoran.co.uk\/2010\/07\/19\/detecting-the-iphone4-and-resolution-with-javascript-or-php\/\">Detecting the iPhone4 and Resolution with Javascript or PHP<\/a>.\n<\/p>\n<p>\nHere&#8217;s Ben&#8217;s example code (slightly tweaked)&#8230;\n<\/p>\n<div class=\"code\"><code>&lt;?php<br \/>\nif( isset($_COOKIE[\"pixel_ratio\"]) ){<br \/>\n$pixel_ratio = $_COOKIE[\"pixel_ratio\"];<br \/>\nif( $pixel_ratio  &gt;= 2 ){<br \/>\necho \"Is HiRes Device\";<br \/>\n}else{<br \/>\necho \"Is NormalRes Device\";<br \/>\n}<br \/>\n}else{<br \/>\n?&gt;<br \/>\n&lt;script type=\"text\/javascript\"&gt;<br \/>\nwriteCookie();<br \/>\nfunction writeCookie()<br \/>\n{<br \/>\nthe_cookie = document.cookie;<br \/>\nif( the_cookie ){<br \/>\nif( window.devicePixelRatio &gt;= 2 ){<br \/>\nthe_cookie = \"pixel_ratio=\"+window.devicePixelRatio+\";\"+the_cookie;<br \/>\ndocument.cookie = the_cookie;<br \/>\nlocation = '&lt;?=$_SERVER['PHP_SELF']?&gt;';<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;?php<br \/>\n<\/code><\/div>\n<p>\nA different approach could be to use CSS and media types. Webkit browsers like Safari on an iPhone have <var>-webkit-min-device-pixel-ratio<\/var>, Gecko browsers like Firefox have <var>-moz-device-pixel-ratio<\/var>.\n<\/p>\n<p>\nAn example using this approach on webkit could be\n<\/p>\n<p><div class=\"code\"><code>&lt;link<br \/>\nrel=\"stylesheet\"<br \/>\ntype=\"text\/css\"<br \/>\nhref=\"\/css\/retina.css\"<br \/>\nmedia=\"only screen and (-webkit-min-device-pixel-ratio: 2)\"<br \/>\n\/&gt;<br \/>\n<\/code><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was looking at how to target Apple&#8217;s retina display for high resolution web pages earlier. My first attempt was to look at the user agent string, but an up to date iPhone 4 sends the same string as an up to date iPhone 3GS. A quick look around the internet reveals the solution for &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Detecting Retina Displays From JavaScript And CSS&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[31,75],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-dev","tag-javascript","tag-web-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Detecting Retina Displays From JavaScript And CSS - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Detecting Retina Displays From JavaScript And CSS - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I was looking at how to target Apple&#8217;s retina display for high resolution web pages earlier. My first attempt was to look at the user agent string, but an up to date iPhone 4 sends the same string as an up to date iPhone 3GS. A quick look around the internet reveals the solution for &hellip; Continue reading &quot;Detecting Retina Displays From JavaScript And CSS&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2011-05-23T18:52:41+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/detecting_retina_displays_from_javascript_and_css-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/detecting_retina_displays_from_javascript_and_css-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Detecting Retina Displays From JavaScript And CSS\",\"datePublished\":\"2011-05-23T18:52:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/detecting_retina_displays_from_javascript_and_css-shtml\\\/\"},\"wordCount\":288,\"keywords\":[\"JavaScript\",\"Web Development\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/detecting_retina_displays_from_javascript_and_css-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/detecting_retina_displays_from_javascript_and_css-shtml\\\/\",\"name\":\"Detecting Retina Displays From JavaScript And CSS - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2011-05-23T18:52:41+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/detecting_retina_displays_from_javascript_and_css-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/detecting_retina_displays_from_javascript_and_css-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/detecting_retina_displays_from_javascript_and_css-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Detecting Retina Displays From JavaScript And CSS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Detecting Retina Displays From JavaScript And CSS - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Detecting Retina Displays From JavaScript And CSS - Robert Price","og_description":"I was looking at how to target Apple&#8217;s retina display for high resolution web pages earlier. My first attempt was to look at the user agent string, but an up to date iPhone 4 sends the same string as an up to date iPhone 3GS. A quick look around the internet reveals the solution for &hellip; Continue reading \"Detecting Retina Displays From JavaScript And CSS\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/","og_site_name":"Robert Price","article_published_time":"2011-05-23T18:52:41+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Detecting Retina Displays From JavaScript And CSS","datePublished":"2011-05-23T18:52:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/"},"wordCount":288,"keywords":["JavaScript","Web Development"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/","name":"Detecting Retina Displays From JavaScript And CSS - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2011-05-23T18:52:41+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/detecting_retina_displays_from_javascript_and_css-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Detecting Retina Displays From JavaScript And CSS"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}