Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:21:36+00:00 2026-06-14T23:21:36+00:00

how to detect, in a cross-browser compatible way , the pixel density of the

  • 0

how to detect, in a cross-browser compatible way, the pixel density of the device visiting a webpage so that one can either serve standard or highres images without forcing doubled images to any device?

Is there any javascript library that automates this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:21:36+00:00Added an answer on June 14, 2026 at 11:21 pm

    Why setting for Retina

    iPhone 4s, iPhone 5, iPad3, iPad4, Macbook 15″, Macbook 13″ all use Retina display.

    Android also support high resolution display, as well as Windows 8(Lumia 920) as mentioned by @JamWaffles.

    Adding high resolution support is good for user experience but it definitely add load for developer, as well as bandwidth for mobile. Somebody don’t suggest doing that.(Peter-Paul Koch, see the bottom “further reading”)

    Breifing

    There are two methods to implement this function. One is Javascript and the other is CSS. All current solutions are for Retina, but could extend to Android high resolution easily.

    CSS solution is about Media Query and -webkit-min-device-pixel-ratio or -webkit-device-pixel-ratio

    • Simple to use.
    • Apply to all browsers.
    • Disadvantage: Good for background. Harder for <img> tag

    Javascript solution is about window.devicePixelRatio property.

    • Advantage: Javascript could manipulate image source. So, if you are going to serve direct image instead of background, better to use Javascript
    • Could not apply to all browsers but current support is good enough. See below for list.
    • Need a bit more setting.

    CSS Solution

    For normal images, say an icon

    .sample-icon {
        background-image: url("../images/sample-icon.png");
        background-size: 36px 36px;
    }
    

    For Retina, add those below

    @media only screen and (-webkit-min-device-pixel-ratio: 2), /* Webkit */
        (min-resolution: 192dpi) /* Everyone else */ {
        .sample-icon {
            background-image: url("../images/sample-icon-highres.png");
            background-size: 18px 18px;
    }
    

    You can use min-resolution: 2dppx to replace min-resolution: 192dpi, for those who don’t want to remember numbers

    Note the difference:

    1. Two different icons, one normal, one high res. High res icon is double size than normal one.
    2. The background size. The later is half. But you need test it in your real use.

    Resource:
    + http://www.w3.org/blog/CSS/2012/06/14/unprefix-webkit-device-pixel-ratio/
    + http://view.jquerymobile.com/master/demos/widgets/buttons/#CustomIcons

    Javascript Solution

    Use window.devicePixelRatio property to detect resolution.

    if (window.devicePixelRatio >= 2) {
      alert("This is a Retina screen");
      //Do something to manipulate image url attribute
      //for example add `@2x-` before all image urls
    }
    

    Browser Support

    Safari, Android WebKit, Chrome 22+ and on Android, Opera Mobile, BlackBerry WebKit, QQ, Palm WebKit,
    Ref: http://www.quirksmode.org/blog/archives/2012/06/devicepixelrati.html

    For Android

    Android device use 1.5 as high resolution instead of 2 in Retina.
    http://developer.android.com/guide/webapps/targeting.html –#Targeting Device Density with CSS, #Targeting Device Density with JavaScript

    Further Good Reading

    http://www.quirksmode.org/blog/archives/2012/07/more_about_devi.html
    “I’m not a big fan of serving special retina images because it makes the web too heavy — especially over a mobile connection. Nonetheless people will do it.” — Peter-Paul Koch

    Update 2013-04-18 Update jQuery mobile link

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help me out, I need a cross platform way to detect if
What is the best cross-browser way to detect the scrollTop of the browser window?
Does anyone of you know a cross browser working way to detect paste or
Is there any cross-browser JavaScript/jQuery code to detect if the browser or a browser
I need a cross-browser way of binding to the scroll event for the browser's
I'm trying to write a Cross-Browser script that detects when a link is clicked
To detect rotation of the mouse wheel in .NET/WinForms, I can override OnMouseWheel .
I'm trying to detect and remove those fields that after a sync are still
I want to detect and insert location of the visitor on a webpage to
How can I detect if an iPhone has a retina display or not? Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.