Media query ipad vs iphone4

后端 未结 5 1114
别跟我提以往
别跟我提以往 2020-12-28 08:47

I am using the media query in css to differentiate iphone and ipad

Here\'s my code:

/* iphone 3 */
@media only screen and (min-device-width : 320px)          


        
相关标签:
5条回答
  • 2020-12-28 09:10

    Check out http://css-tricks.com/snippets/css/media-queries-for-standard-devices/

    /* iPhone 4 ----------- */
    @media
    only screen and (-webkit-min-device-pixel-ratio : 1.5),
    only screen and (min-device-pixel-ratio : 1.5) {
    /* Styles */
    }
    
    /* iPads (portrait) ----------- */
    @media only screen
    and (min-device-width : 768px)
    and (max-device-width : 1024px)
    and (orientation : portrait) {
    /* Styles */
    }
    
    0 讨论(0)
  • 2020-12-28 09:11

    iOS does not currently support orientation:portrait and orientation:landscape.
    Instead Apple currently uses the following:

    Portrait
    orientation:0
    orientation:180 (not currently supported on iphone)

    Landscape
    orientation:90
    orientation:-90

    Ref: http://developer.apple.com/library/safari/#documentation/DataManagement/Reference/DOMWindowAdditionsReference/DOMWindowAdditions/DOMWindowAdditions.html

    0 讨论(0)
  • 2020-12-28 09:25

    I have tried this link http://jsfiddle.net/andresilich/SpbC3/4/show/ in the responsive test tool like http://mattkersley.com/responsive/ and http://ipadpeek.com .I have seen the landscape and the portrait mode displaying the same view. Is there any problem with those tools?

    0 讨论(0)
  • 2020-12-28 09:27

    Great answer on using the colors to detect device and orientation. iPhone 4 did not work though and only rendered as either green or purple backgrounds.

    I found this article which shed some light. http://www.hemmachat.com/2011/04/21/iphone-website-for-good-beginnings/

    Now using the pixel ratio of 2 instead I can now get red and brown on an iPhone 4.

    /* iPhone 4 portrait */
    @media only screen and (-webkit-min-device-pixel-ratio: 2) {
        body {
            background-color:red;
        }
    }
    
    /* iPhone 4 landscape */
    @media only screen and (-webkit-min-device-pixel-ratio: 2) and (min-width: 480px){
        body {
            background-color:brown;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 09:31

    Modify your iPhone 4 media query to target high density pixel displays (retina = iPhone4)

    @media screen and (max-device-width: 640px), screen and (-webkit-min-device-pixel-ratio: 2) and (orientation:portrait) { ... }
    

    Didn't notice you reopened the question with an expansion so here is a reworked answer to target both iphones (3 and 4) and ipads.

    Breakdown of what you should expect:

    • On regular browsers you should get the teal background color.
    • orange on an ipad (landscape).
    • black on an ipad (portrait)
    • red on an iphone 4 (portrait)
    • pink on an iphone 4 (landscape)
    • green on regular smartphones, e.g Androids (landscape)
    • purple on a regular smartphone (portrait)

    CSS

    body {
        background-color:teal;
        -webkit-transition: background-color 1000ms linear;
        -moz-transition: background-color 1000ms linear;
        -o-transition: background-color 1000ms linear;
        -ms-transition: background-color 1000ms linear;
        transition: background-color 1000ms linear;
    }
    
    /* iPads (portrait and landscape) ----------- */
    @media only screen
    and (min-device-width : 768px)
    and (max-device-width : 1024px) {
        body {
            background-color:yellow;
        }
    }
    
    /* iPads (landscape) ----------- */
    @media only screen
    and (min-device-width : 768px)
    and (max-device-width : 1024px)
    and (orientation : landscape) {
        body {
            background-color:orange;
        }
    }
    
    /* iPads (portrait) ----------- */
    @media only screen
    and (min-device-width : 768px)
    and (max-device-width : 1024px)
    and (orientation : portrait) {
        body {
            background-color:black;
        }
    }
    
    /* iPhone 4 - (portrait) ---------- */
    @media
    only screen and (-webkit-min-device-pixel-ratio : 2) and (orientation:portrait),
    only screen and (min-device-pixel-ratio : 2)  and (orientation:portrait){
        body {
            background-color:red;
        }
    }
    
    /* iPhone 4 - (landscape) ---------- */
    @media screen and (-webkit-min-device-pixel-ratio : 2) and (orientation:landscape), screen and (min-device-pixel-ratio : 2) and (orientation:landscape){
        body {
            background-color:pink;
        }
    }
    
    /* Smartphones (landscape) ----------- */
    @media only screen
    and (min-width : 321px)
    and (max-width: 480px) {
        body {
            background-color:green;
        }
    }
    
    /* Smartphones (portrait) ----------- */
    @media only screen
    and (max-width : 320px) {
        body {
            background-color:purple;
        }
    }`<!-- language-all: lang-css -->
    

    I reformatted the @media queries found in this fine article over at CSS-tricks to comply to some iphone4-specific bits, but overall this media query set should cover both iphones (3 and 4 with separate media queries) and ipads as well.

    Here is a demo you can try in your i-devices.

    http://jsfiddle.net/andresilich/SpbC3/4/show/

    And you can also try out the queries over at http://quirktools.com/screenfly/ to see how they stack up. One thing though, the screenfly site does not differentiate between iphone 3 and 4 because regular browsers skip the webkit only -webkit-min-device-pixel-ratio : 1.5 pixel ratio count so you will get better results testing it out in your actual device.

    0 讨论(0)
提交回复
热议问题