Skip to main content

How to target iPhone 3GS AND iPhone 4 in one @media query



I am trying to implement alternate layouts for both the iPad/iPhone and older iPhones as well.





I have established that the best method is to use @media queries from the CSS3 spec,





As such these are my media queries at the minute:







@media screen and (max-width: 1000px) { ... }







Above is for small desktop and laptop screens.







@media screen and (max-width: 700px) { ... }







Above is for the iPad and VERY small desktop/laptop screens.







@media screen and (max-device-width: 480px) { ... }







Above is for iPhone 3GS- and mobile devices in general.





However, the new iPhone 4 with Steve Jobs's all-singing all-dancing "retina" display means that it has a pixel ratio of 2-1 meaning 1 pixel actually appears to the browser as 2x2 pixels making its resolution (960x640 - meaning it will trigger the iPad layout rather than the mobile device layout) so this requires ANOTHER media query (only so far supported by webkit):







@media screen and (-webkit-min-device-pixel-ratio: 2) { ... }







Now, the thing is... I want my nice shiny new iPhone 4 layout amalgamated with the iPhone 3GS and mobile device layout as they will both have exactly the same inner CSS code,





Therefore making my question;





How do I create an @media query that points both the iPhone 4, 3GS and other mobiles to the same styles?


Comments

  1. I'm not sure if there's a single catch-all media query for all iPhones and iPod touches. In the meantime, you can use a comma to group both media queries for standard and Retina displays to a single rule, just like you use it to group selectors:

    @media only screen and (max-device-width: 480px),
    only screen and (-webkit-min-device-pixel-ratio: 2) {
    /* iPhone CSS rules here */
    }

    ReplyDelete
  2. BoltClock's answer seems pretty good to me at the moment.

    However, thinking in to the future, if Apple (or another manufacturer) releases another device with a device pixel ratio of 2, this media query would be used for this device too.

    I don't think it's out of the question to assume that this will happen, and that the new device could have a much larger screen, such as an iPad with a retina display.

    To make this query only applicable to the iPhone 4 and previous iPhones (and any other device of a similar size)

    @media screen and (max-device-width: 480px), screen and (max-device-width: [[IPHONE_4_WIDTH]]px) and (-webkit-min-device-pixel-ratio: 2) {
    /* iPhone CSS rules here */
    }


    Unsure of [[IPHONE_4_WIDTH]] right now - don't have one on me, and some sites say 480, some say 960. Try replacing with both. (And let me know what you find :) )

    ReplyDelete
  3. I'm not sure I follow your question. Did you try your queries on the iPhone 4? device-width is measured in logical pixels, not physical ones, so the iPhone 4 still fits the max-device-width: 480px criteria.

    Same goes for high-end Android smartphones, which have a pixel ratio of 1.5: the Nexus One has a physical screen of 480x800, logical screen of 320x533.

    ReplyDelete
  4. I have been hunting for a way to specifically target the iPhone 3 / 3GS per the second part of the request. The most acceptable solution I've found is to tailor the media queries to the fixed parameters of an iPhone 3.

    @media only screen
    and (device-width:320px)
    and (device-height:480px)
    and (-webkit-device-pixel-ratio: 1) { ... }


    In order to work this query requires that you use Safari's viewport meta tag to fix the browser to 100% with the following:

    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>


    There are a small number of Android phones that will also get picked up on that query. With the Android Market showing 18.4% of active phones in the potential screen size range of 320x480, only a subset of that will match the device-pixel-ratio on the stock webkit browser. Not perfect, but better than nothing at all.

    Lists of phone resolutions


    Slightly outdated but thorough: http://cartoonized.net/cellphone-screen-resolution-by-size.php
    Only 3 listed as a potential match here: http://en.wikipedia.org/wiki/Comparison_of_Android_devices
    Android Market weekly snapshot: http://developer.android.com/resources/dashboard/screens.html


    All information was considered as of post date.

    Also per mernen's comment #2 to his post here are the docs to target Android devices by pixel density: http://developer.android.com/guide/webapps/targeting.html#DensityCSS

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex