iPhone X/8/8 Plus CSS媒体查询

nat*_*han 64 css media-queries iphone-x iphone-8 iphone-8-plus

与Apple的新设备相对应的CSS媒体查询是什么?我需要设置body's background-color来改变X的安全区域背景颜色.

nat*_*han 124

iPhone X.

@media only screen 
    and (device-width : 375px) 
    and (device-height : 812px) 
    and (-webkit-device-pixel-ratio : 3) { }
Run Code Online (Sandbox Code Playgroud)

iPhone 8

@media only screen 
    and (device-width : 375px) 
    and (device-height : 667px) 
    and (-webkit-device-pixel-ratio : 2) { }
Run Code Online (Sandbox Code Playgroud)

iPhone 8 Plus

@media only screen 
    and (device-width : 414px) 
    and (device-height : 736px) 
    and (-webkit-device-pixel-ratio : 3) { }
Run Code Online (Sandbox Code Playgroud)


iPhone 6 +/6s +/7 +/8 +共享相同的尺寸,而iPhone 7/8也可以.


寻找具体的方向?

肖像

添加以下规则:

    and (orientation : portrait) 
Run Code Online (Sandbox Code Playgroud)

景观

添加以下规则:

    and (orientation : landscape) 
Run Code Online (Sandbox Code Playgroud)



参考文献:


Yas*_*ria 60

以下是一些针对iphone的媒体查询.这是参考链接https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions

    /* iphone 3 */
    @media only screen and (min-device-width: 320px) and (max-device-height: 480px) and (-webkit-device-pixel-ratio: 1) { }

    /* iphone 4 */
    @media only screen and (min-device-width: 320px) and (max-device-height: 480px) and (-webkit-device-pixel-ratio: 2) { }

    /* iphone 5 */
    @media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (-webkit-device-pixel-ratio: 2) { }

    /* iphone 6, 6s, 7, 8 */
    @media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (-webkit-device-pixel-ratio: 2) { }

    /* iphone 6+, 6s+, 7+, 8+ */
    @media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (-webkit-device-pixel-ratio: 3) { }

    /* iphone X */
    @media only screen and (min-device-width: 375px) and (max-device-height: 812px) and (-webkit-device-pixel-ratio: 3) { }

    /* iphone XR */
    @media only screen and (min-device-width : 414px) and (max-device-height : 896px) and (-webkit-device-pixel-ratio : 2) { }

   /* iphone XS */
    @media only screen and (min-device-width : 375px) and (max-device-height : 812px) and (-webkit-device-pixel-ratio : 3) { }

   /* iphone XS Max */
    @media only screen and (min-device-width : 414px) and (max-device-height : 896px) and (-webkit-device-pixel-ratio : 3) { }
Run Code Online (Sandbox Code Playgroud)


Sam*_*ian 13

我注意到,这里的答案是使用:device-width,device-height,min-device-width,min-device-height,max-device-width,max-device-height.

请不要使用它们,因为它们已被弃用.请参阅MDN以供参考.而是使用常规min-width,max-width等等.为了进一步保证,您可以将最小值和最大值设置为相同的px量.例如:

iPhone X.

@media only screen 
    and (width : 375px) 
    and (height : 635px)
    and (orientation : portrait)  
    and (-webkit-device-pixel-ratio : 3) { }
Run Code Online (Sandbox Code Playgroud)

您可能还注意到我使用的是635px的高度.自己尝试窗口高度实际为635px.为iPhone X和Safari Web检查器运行iOS模拟器window.innerHeight.以下是有关此主题的一些有用链接: