【发布时间】:2011-04-19 21:10:34
【问题描述】:
我一直在尝试纯粹通过样式表来检测 iPhone 或 iPad。我尝试了here 提供的解决方案,方法是使用@media 手持设备,仅屏幕和 (max-device-width: 480px) {.
但是,这似乎不起作用。有什么想法吗?
【问题讨论】:
我一直在尝试纯粹通过样式表来检测 iPhone 或 iPad。我尝试了here 提供的解决方案,方法是使用@media 手持设备,仅屏幕和 (max-device-width: 480px) {.
但是,这似乎不起作用。有什么想法吗?
【问题讨论】:
iPhone 和 iPod touch:
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="../iphone.css" type="text/css" />
iPhone 4 和 iPod touch 4G:
<link rel="stylesheet" media="only screen and (-webkit-min-device-pixel-ratio: 2)" type="text/css" href="../iphone4.css" />
iPad:
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="../ipad.css" type="text/css" />
【讨论】:
这就是我处理 iPhone(和类似)设备 [不是 iPad] 的方式:
在我的 CSS 文件中:
@media only screen and (max-width: 480px), only screen and (max-device-width: 480px) {
/* CSS overrides for mobile here */
}
在我的 HTML 文档的头部:
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
【讨论】:
user-scalable=no
user-scaleable=no 不是一个好习惯。
我用这些:
/* Non-Retina */
@media screen and (-webkit-max-device-pixel-ratio: 1) {
}
/* Retina */
@media only screen and (-webkit-min-device-pixel-ratio: 1.5),
only screen and (-o-min-device-pixel-ratio: 3/2),
only screen and (min--moz-device-pixel-ratio: 1.5),
only screen and (min-device-pixel-ratio: 1.5) {
}
/* iPhone Portrait */
@media screen and (max-device-width: 480px) and (orientation:portrait) {
}
/* iPhone Landscape */
@media screen and (max-device-width: 480px) and (orientation:landscape) {
}
/* iPad Portrait */
@media screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
}
/* iPad Landscape */
@media screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
}
http://zsprawl.com/iOS/2012/03/css-for-iphone-ipad-and-retina-displays/
【讨论】:
您可能想尝试this O'Reilly article 的解决方案。
重要的部分是这些 CSS 媒体查询:
<link rel="stylesheet" media="all and (max-device-width: 480px)" href="iphone.css">
<link rel="stylesheet" media="all and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait)" href="ipad-portrait.css">
<link rel="stylesheet" media="all and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape)" href="ipad-landscape.css">
<link rel="stylesheet" media="all and (min-device-width: 1025px)" href="ipad-landscape.css">
【讨论】:
即使在过去五年中,也出现了许多具有不同屏幕尺寸/比例/分辨率的设备,包括新型 iPhone 和 iPad。为每台设备定制一个网站是非常困难的。
同时,device-width、device-height 和 device-aspect-ratio 的媒体查询已被弃用,因此它们可能无法在未来的浏览器版本中使用。 (来源:MDN)
TLDR:基于浏览器宽度而非设备的设计。 Here's a good introduction to this topic.
【讨论】: