纯粹用css检测iPhone/iPad

FLX*_*FLX 37 css iphone ipad

我一直试图通过样式表来检测iPhone或iPad.我试图提供的解决方案在这里,通过使用手持式@media,只有屏幕和(MAX-设备宽度:480像素){.

但是,这似乎不起作用.有任何想法吗?

Oli*_*yen 30

iPhone和iPod touch:

<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="../iphone.css" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

iPhone 4和iPod touch 4G:

<link rel="stylesheet" media="only screen and (-webkit-min-device-pixel-ratio: 2)" type="text/css" href="../iphone4.css" />
Run Code Online (Sandbox Code Playgroud)

iPad的:

<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="../ipad.css" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

  • iPad检测有一个问题:准备通常有1024x768和1024x600屏幕的上网本也会受到这个CSS的影响. (6认同)
  • [从不检测特定设备](http://stackoverflow.com/a/12990647/288906).每月都有新设备问世,这些数字会发生变化. (6认同)

Bar*_*art 24

这就是我处理iPhone(和类似)设备[而不是iPad]的方式:

在我的CSS文件中:

@media only screen and (max-width: 480px), only screen and (max-device-width: 480px) {
   /* CSS overrides for mobile here */
}
Run Code Online (Sandbox Code Playgroud)

在我的HTML文档的头部:

<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
Run Code Online (Sandbox Code Playgroud)

  • 不要使用它,[从不设置`user-scalable = no`](http://www.456bereastreet.com/archive/201106/the_ios_zoom_setting_disables_maximum-scale1_and_user-scalableno/) (8认同)
  • 我对用户可扩展的"从不"部分持不同意见.如果您正在构建类似本机的响应式Web应用程序,那么使用户可扩展只会导致问题.如果您正确设计,则用户不应该希望放大/缩小. (8认同)

Dar*_*ust 14

您可能想尝试这篇O'Reilly文章中的解决方案.

重要的部分是这些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"> 
Run Code Online (Sandbox Code Playgroud)


zSp*_*awl 14

我用这些:

/* 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) {
}
Run Code Online (Sandbox Code Playgroud)

http://zsprawl.com/iOS/2012/03/css-for-iphone-ipad-and-retina-displays/