上面列出的所有这些答案,使用max-device-width 或max-device-height 进行媒体查询,都存在非常严重的错误:它们还针对许多其他流行的移动设备(可能不需要并且永远不会测试,否则将在未来投放市场)。
此查询适用于任何屏幕较小的设备,您的设计可能会被破坏。
结合类似的特定于设备的媒体查询(针对 HTC、三星、iPod、Nexus...),这种做法将引爆定时炸弹。对于 debigging,这个想法可以使您的 CSS 成为不受控制的意大利面条。您永远无法测试所有可能的设备。
请注意,唯一的媒体查询始终以 iPhone5 为目标,是:
/* 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*/
}
注意exact宽度和高度,而不是在这里检查最大宽度。
现在,解决方案是什么?如果您想编写一个在所有可能的设备上看起来都不错的网页,最好的做法是使用降级
/* 退化模式
我们只检查屏幕宽度
当然,这将改变是从纵向变成横向*/
/*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*/
}
如果您的网站访问者中有 30% 以上来自移动设备,请颠倒此方案,提供移动优先的方法。在这种情况下使用min-device-width。这将加快移动浏览器的网页呈现速度。