简体   繁体   English

iPhone 5 CSS 媒体查询

[英]iPhone 5 CSS media query

The iPhone 5 has a longer screen and it's not catching my website's mobile view. iPhone 5 的屏幕较长,无法捕捉我网站的移动视图。 What are the new responsive design queries for the iPhone 5 and can I combine with existing iPhone queries? iPhone 5 有哪些新的响应式设计查询,我可以与现有的 iPhone 查询结合使用吗?

My current media query is this:我目前的媒体查询是这样的:

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

Another useful media feature is device-aspect-ratio . 另一个有用的媒体功能是device-aspect-ratio

Note that the iPhone 5 does not have a 16:9 aspect ratio . 请注意, iPhone 5的宽高比不是16:9 It is in fact 40:71. 实际上是40:71。

iPhone < 5: iPhone <5:
@media screen and (device-aspect-ratio: 2/3) {}

iPhone 5: iphone 5:
@media screen and (device-aspect-ratio: 40/71) {}

iPhone 6: iPhone 6:
@media screen and (device-aspect-ratio: 375/667) {}

iPhone 6 Plus: iPhone 6 Plus:
@media screen and (device-aspect-ratio: 16/9) {}

iPad: iPad的:
@media screen and (device-aspect-ratio: 3/4) {}

Reference: 参考:
Media Queries @ W3C 媒体查询@ W3C
iPhone Model Comparison iPhone型号比较
Aspect Ratio Calculator 宽高比计算器

There is this, which I credit to this blog : 有这个,我赞同这个博客

@media only screen and (min-device-width: 560px) and (max-device-width: 1136px) and (-webkit-min-device-pixel-ratio: 2) {
    /* iPhone 5 only */
}

Keep in mind it reacts the iPhone 5, not to the particular iOS version installed on said device. 请记住,它会对iPhone 5做出反应,而不是对所述设备上安装的特定iOS版本做出反应。

To merge with your existing version, you should be able to comma-delimit them: 要与现有版本合并,您应该能够用逗号分隔它们:

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

NB: I haven't tested the above code, but I've tested comma-delimited @media queries before, and they work just fine. 注意:我没有测试过上面的代码,但之前我已经测试了逗号分隔的@media查询,并且它们工作得很好。

Note that the above may hit some other devices which share similar ratios, such as the Galaxy Nexus. 请注意,以上可能会遇到其他一些具有相似比率的设备,例如Galaxy Nexus。 Here is an additional method which will target only devices which have one dimension of 640px (560px due to some weird display-pixel anomalies) and one of between 960px (iPhone <5) and 1136px (iPhone 5). 这是另一种方法,它只针对一维640px(560px,由于一些奇怪的显示像素异常)和一个960px(iPhone <5)和1136px(iPhone 5)之间的设备。

@media
    only screen and (max-device-width: 1136px) and (min-device-width: 960px) and (max-device-height: 640px) and (min-device-height: 560px),
    only screen and (max-device-height: 1136px) and (min-device-height: 960px) and (max-device-width: 640px) and (min-device-width: 560px) {
    /* iPhone only */
}

All these answers listed above, that use max-device-width or max-device-height for media queries, suffer from very strong bug: they also target a lot of other popular mobile devices (probably unwanted and never tested, or that will hit the market in future). 上面列出的所有这些答案,使用max-device-widthmax-device-height进行媒体查询,都会遇到非常严重的错误:它们还针对很多其他流行的移动设备 (可能不需要,从未测试,或者会受到影响)未来的市场)。

This queries will work for any device that has a smaller screen , and probably your design will be broken. 此查询适用于屏幕较小的任何设备,可能您的设计将被破坏。

Combined with similar device-specific media queries (for HTC, Samsung, IPod, Nexus...) this practice will launch a time-bomb. 结合类似设备特定的媒体查询(对于HTC,三星,iPod,Nexus ......),这种做法将发布定时炸弹。 For debigging, this idea can make your CSS an uncontrolled spagetti. 对于debigging,这个想法可以使你的CSS成为一个不受控制的spagetti。 You can never test all possible devices. 您永远无法测试所有可能的设备。

Please be aware that the only media query always targeting IPhone5 and nothing else , is: 请注意,唯一一个始终以iPhone 5目标的媒体查询是:

/* iPhone 5 Retina regardless of IOS version */
@media (device-height : 568px) 
   and (device-width : 320px) 
   and (-webkit-min-device-pixel-ratio: 2)
/* and (orientation : todo: you can add orientation or delete this comment)*/ {
                 /*IPhone 5 only CSS here*/
}

Note that exact width and height, not max-width is checked here. 请注意,此处检查精确的宽度和高度,而不是最大宽度。


Now, what is the solution? 现在,解决方案是什么? If you want to write a webpage that will look good on all possible devices, the best practice is to you use degradation 如果您想编写一个在所有可能的设备上看起来都很好的网页,最佳做法是使用降级

/* degradation pattern we are checking screen width only sure, this will change is turning from portrait to landscape*/ / *退化模式我们只检查屏幕宽度,这将改变从纵向转为横向* /

/*css for desktops here*/

@media (max-device-width: 1024px) {
  /*IPad portrait AND netbooks, AND anything with smaller screen*/
  /*make the design flexible if possible */
  /*Structure your code thinking about all devices that go below*/
}
@media (max-device-width: 640px) {
 /*Iphone portrait and smaller*/
}
@media (max-device-width: 540px) {
 /*Smaller and smaller...*/
}
@media (max-device-width: 320px) {
 /*IPhone portrait and smaller. You can probably stop on 320px*/
}

If more than 30% of your website visitors come from mobile, turn this scheme upside-down, providing mobile-first approach. 如果超过30%的网站访问者来自移动设备,请将此方案颠倒过来,提供移动优先方法。 Use min-device-width in that case. 在这种情况下使用min-device-width This will speed up webpage rendering for mobile browsers. 这将加快移动浏览器的网页呈现速度。

对我来说,完成这项工作的查询是:

only screen and (device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)

iPhone 5 in portrait & landscape iPhone 5的纵向和横向

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 568px) {

/* styles*/
}

iPhone 5 in landscape iPhone 5在风景中

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 568px) 
and (orientation : landscape) { 
/* styles*/

}

iPhone 5 in portrait iPhone 5的肖像

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 568px) 
and (orientation : portrait) { 
 /* styles*/

}

None of the response works for me targeting a phonegapp App. 我没有针对phonegapp应用程序的响应。

As the following link points, the solution below works. 如下面的链接点,下面的解决方案有效。

@media screen and (device-height: 568px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 2) {
    // css here
}

Just a very quick addition as I have been testing a few options and missed this along the way. 只是一个非常快速的添加,因为我一直在测试一些选项,并错过了这一点。 Make sure your page has: 确保您的页面包含:

<meta name="viewport" content="initial-scale=1.0">

You can get your answer fairly easily for the iPhone5 along with other smartphones on the media feature database for mobile devices: 您可以轻松地为iPhone5以及移动设备媒体功能数据库上的其他智能手机获得您的答案:

http://pieroxy.net/blog/2012/10/18/media_features_of_the_most_common_devices.html http://pieroxy.net/blog/2012/10/18/media_features_of_the_most_common_devices.html

You can even get your own device values on the test page on the same website. 您甚至可以在同一网站的测试页面上获取自己的设备值。

(Disclaimer: This is my website) (免责声明:这是我的网站)

afaik no iPhone uses a pixel-ratio of 1.5 afaik没有iPhone使用1.5的像素比率

iPhone 3G / 3GS: (-webkit-device-pixel-ratio: 1) iPhone 4G / 4GS / 5G: (-webkit-device-pixel-ratio: 2) iPhone 3G / 3GS :( -webkit-device-pixel-ratio:1)iPhone 4G / 4GS / 5G :( -webkit-device-pixel-ratio:2)

/* iPad */
@media screen and (min-device-width: 768px) {
    /* ipad-portrait */
    @media screen and (max-width: 896px) { 
        .logo{
            display: none !important;
        }
    }
    /* ipad-landscape */
    @media screen and (min-width: 897px) { 

    }
}
/* iPhone */
@media screen and (max-device-width: 480px) {
    /* iphone-portrait */
    @media screen and (max-width: 400px) { 

    }
    /* iphone-landscape */
    @media screen and (min-width: 401px) { 

    }
}

You should maybe down the "-webkit-min-device-pixel-ratio" to 1.5 to catch all iPhones ? 您应该将“-webkit-min-device-pixel-ratio”降低到1.5以捕获所有iPhone?

@media only screen and (max-device-width: 480px), only screen and (min-device-width: 640px) and (max-device-width: 1136px) and (-webkit-min-device-pixel-ratio: 1.5) {
/* iPhone only */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM