仅针对 iPad 和 iPad 的 CSS 媒体查询?

问题描述 投票:0回答:9

您好,我正在使用多个平板设备、iPad、Galaxy Tab、Acer Iconia、LG 3D Pad 等。

  • iPad - 1024 x 768
  • LG 垫 - 1280 x 768
  • 银河标签 - 1280 x 800

我只想使用 CSS3 媒体查询定位 iPad。因为,LG 和 iPad 的设备宽度是相同的 768px - 我无法分离每个设备。

我试过按照以下方式分开,但似乎没有用:

@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation : portrait) /* applied to lg also */
@media only screen and (min-resolution: 132dpi) and (max-device-width: 1024px) and (orientation : portrait) /* applies to lg also */
@media only screen and (device-aspect-ratio: 1024/768) and (orientation : portrait) /* does not work on iPad or LG */

我不知道 -webkit-device-pixel-ratio 和其他 -webkit* 选项及其针对 iPad 的值。我不想将 JavaScript 用于样式,有什么想法吗?

ipad css webkit media-queries
9个回答
159
投票

终于找到解决方案:Detect different device platforms using CSS

<link rel="stylesheet" media="all and (device-width: 768px) and (device-height: 1024px) and (orientation:portrait)" href="ipad-portrait.css" />
<link rel="stylesheet" media="all and (device-width: 768px) and (device-height: 1024px) and (orientation:landscape)" href="ipad-landscape.css" />

为了减少 HTTP 调用,这也可以在您现有的通用 CSS 文件中使用:

@media all and (device-width: 768px) and (device-height: 1024px) and (orientation:portrait) {
  .ipad-portrait { color: red; } /* your css rules for ipad portrait */
}
@media all and (device-width: 1024px) and (device-height: 768px) and (orientation:landscape) {
  .ipad-landscape { color: blue; } /* your css rules for ipad landscape */
}

其他参考:


12
投票

我回答这个问题有点晚了,但以上都不适合我。

这对我有用

@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
    //your styles here
   }

6
投票

您需要使用一些脚本通过其用户代理来定位设备。 iPad 的用户代理是:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

4
投票

完全相同的问题,也有答案 =)

http://css-tricks.com/forums/discussion/12708/target-ipad-ipad-only./p1

@media only screen and (device-width: 768px) ...
@media only screen and (max-device-width: 1024px) ...

我目前无法测试所以请测试它=)

还发现了一些:

http://perishablepress.com/press/2010/10/20/target-iphone-and-ipad-with-css3-media-queries/

或者您使用一些 javascript 检查导航器并使用 javascript 生成/添加一个 css 文件


2
投票
<html>
<head>
    <title>orientation and device detection in css3</title>

    <link rel="stylesheet" media="all and (max-device-width: 480px) and (orientation:portrait)" href="iphone-portrait.css" />
    <link rel="stylesheet" media="all and (max-device-width: 480px) and (orientation:landscape)" href="iphone-landscape.css" />
    <link rel="stylesheet" media="all and (device-width: 768px) and (device-height: 1024px) and (orientation:portrait)" href="ipad-portrait.css" />
    <link rel="stylesheet" media="all and (device-width: 768px) and (device-height: 1024px) and (orientation:landscape)" href="ipad-landscape.css" />
    <link rel="stylesheet" media="all and (device-width: 800px) and (device-height: 1184px) and (orientation:portrait)" href="htcdesire-portrait.css" />
    <link rel="stylesheet" media="all and (device-width: 800px) and (device-height: 390px) and (orientation:landscape)" href="htcdesire-landscape.css" />
    <link rel="stylesheet" media="all and (min-device-width: 1025px)" href="desktop.css" />

</head>
<body>
    <div id="iphonelandscape">iphone landscape</div>
    <div id="iphoneportrait">iphone portrait</div>
    <div id="ipadlandscape">ipad landscape</div>
    <div id="ipadportrait">ipad portrait</div>
    <div id="htcdesirelandscape">htc desire landscape</div>
    <div id="htcdesireportrait">htc desire portrait</div>
    <div id="desktop">desktop</div>
    <script type="text/javascript">
        function res() { document.write(screen.width + ', ' + screen.height); }
        res();
    </script>
</body>
</html>

1
投票

如今,您可以使用 Media Queries Level 4 功能来检查设备是否具有 “悬停”在元素上 的能力。

@media (hover: hover) { ... }

由于 ipad 没有“悬停”状态,您可以有效地定位像 ipad 这样的触摸设备。


1
投票
/*working only in ipad portrait device*/
@media only screen and (width: 768px) and (height: 1024px) and (orientation:portrait) {
  body{
    background: red !important;
  }  
}
/*working only in ipad landscape device*/
@media all and (width: 1024px) and (height: 768px) and (orientation:landscape){
  body{
    background: green !important;
  }   
}

In the media query of specific devices, please use '!important' keyword to override the default CSS. Otherwise that does not change your webpage view on that particular devices.

1
投票

这是 ipad

@media all and (device-width: 768px) {
  
}

这是为 ipad pro 设计的

@media all and (device-width: 1024px){
  
}

0
投票

针对 iPad 9 和 iPad 10 的 CSS:

/* iPad 9th Gen */
@media only screen 
    and (min-device-width: 810px) 
    and (max-device-width: 1080px)
    and (min-device-pixel-ratio: 2) {
        /* rules here */
    }

/* iPad 10th Gen */
@media only screen 
    and (min-device-pixel-ratio: 2) {
        /* rules here */
}
© www.soinside.com 2019 - 2024. All rights reserved.