CSS媒体查询只针对iPad和iPad?

时间:2011-11-25 15:55:24

标签: css ipad css3 webkit media-queries

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

  • iPad - 1024 x 768
  • LG Pad - 1280 x 768
  • Galaxy Tab - 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用于样式,任何想法?

8 个答案:

答案 0 :(得分:124)

最后找到了一个解决方案: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 */
}

希望这有帮助。

其他参考资料:

答案 1 :(得分:5)

我回答这个问题有点迟,但以上都没有为我工作。

这对我有用

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

答案 2 :(得分:3)

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

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文件

答案 3 :(得分:3)

您需要使用某个脚本通过其用户代理定位设备。 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 :(得分: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>

答案 5 :(得分:0)

这几天,您可以使用Media Queries Level 4功能来检查设备是否具有to 'hover' over elements功能。

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

由于ipad没有“悬停”状态,因此您可以有效地针对ipad等触摸设备。

答案 6 :(得分:0)

这是给ipad的

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

这是给 ipad pro 的

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

答案 7 :(得分:-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.