css – 媒体查询ipad vs iphone4

前端之家收集整理的这篇文章主要介绍了css – 媒体查询ipad vs iphone4前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用css中的媒体查询来区分 iphone和ipad

这是我的代码

  1. /* iphone 3 */
  2. @media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation:portrait) { ... }
  3.  
  4. /* iphone 4 */
  5. @media only screen and (min-device-width : 640px) and (max-device-width : 960px) and (orientation:portrait) { ... }
  6.  
  7. /*iPad styles*/
  8. @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation:portrait) { ... }
  9.  
  10. /* i have the same for landscape */

现在我有一个解决冲突,iphone 4使用与ipad相同的分辨率,反之亦然.

如何解决这个问题?

解决方法

修改您的iPhone 4媒体查询以定位高密度像素显示(retina = iPhone4)
  1. @media screen and (max-device-width: 640px),screen and (-webkit-min-device-pixel-ratio: 2) and (orientation:portrait) { ... }

没有注意到您通过扩展重新打开了这个问题,所以这里是一个重做的答案,目标是iphones(3和4)和ipads.

你应该期待的细分:

>在常规浏览器上,您应该得到蓝绿色背景颜色.
>在ipad(景观)上的橙色.
>黑色的ipad(肖像)
>红色在iphone 4(肖像)
>粉红色的iphone 4(景观)
常规智能手机上的绿色,例如Android(景观)
>常规智能手机上的紫色(肖像)

CSS

  1. body {
  2. background-color:teal;
  3. -webkit-transition: background-color 1000ms linear;
  4. -moz-transition: background-color 1000ms linear;
  5. -o-transition: background-color 1000ms linear;
  6. -ms-transition: background-color 1000ms linear;
  7. transition: background-color 1000ms linear;
  8. }
  9.  
  10. /* iPads (portrait and landscape) ----------- */
  11. @media only screen
  12. and (min-device-width : 768px)
  13. and (max-device-width : 1024px) {
  14. body {
  15. background-color:yellow;
  16. }
  17. }
  18.  
  19. /* iPads (landscape) ----------- */
  20. @media only screen
  21. and (min-device-width : 768px)
  22. and (max-device-width : 1024px)
  23. and (orientation : landscape) {
  24. body {
  25. background-color:orange;
  26. }
  27. }
  28.  
  29. /* iPads (portrait) ----------- */
  30. @media only screen
  31. and (min-device-width : 768px)
  32. and (max-device-width : 1024px)
  33. and (orientation : portrait) {
  34. body {
  35. background-color:black;
  36. }
  37. }
  38.  
  39. /* iPhone 4 - (portrait) ---------- */
  40. @media
  41. only screen and (-webkit-min-device-pixel-ratio : 2) and (orientation:portrait),only screen and (min-device-pixel-ratio : 2) and (orientation:portrait){
  42. body {
  43. background-color:red;
  44. }
  45. }
  46.  
  47. /* iPhone 4 - (landscape) ---------- */
  48. @media screen and (-webkit-min-device-pixel-ratio : 2) and (orientation:landscape),screen and (min-device-pixel-ratio : 2) and (orientation:landscape){
  49. body {
  50. background-color:pink;
  51. }
  52. }
  53.  
  54. /* Smartphones (landscape) ----------- */
  55. @media only screen
  56. and (min-width : 321px)
  57. and (max-width: 480px) {
  58. body {
  59. background-color:green;
  60. }
  61. }
  62.  
  63. /* Smartphones (portrait) ----------- */
  64. @media only screen
  65. and (max-width : 320px) {
  66. body {
  67. background-color:purple;
  68. }
  69. }`<!-- language-all: lang-css -->

我重新格式化了这个精美的article中找到的@media查询,通过CSS技巧来符合一些iphone4特定的位,但是这个媒体查询集合总体上应该涵盖两个iphone(3和4个独立的媒体查询)和ipads.

这是您可以在i设备中尝试的演示.

http://jsfiddle.net/andresilich/SpbC3/4/show/

您也可以在http://quirktools.com/screenfly/尝试查询,看看它们是如何堆叠的.有一件事,screenfly网站没有区分iphone 3和4,因为普通浏览器只跳过webkit -webkit-min-device-pixel-ratio:1.5像素比例数,所以你会得到更好的结果测试在你的实际设备.

猜你在找的CSS相关文章