【问题标题】:Targetting iPhone 4 and 5 separately with media queries使用媒体查询分别针对 iPhone 4 和 5
【发布时间】:2014-01-10 05:10:14
【问题描述】:

我有这些媒体查询来为 iPhone 4 和 iPhone 5 应用不同的样式

/* iPhone 4 (landscape) */
@media only screen and (min-width:320px) and (max-width:480px) and (orientation:landscape) { 
    .background img {
      height: 5px;
    }
}
/* iPhone 4 (portrait) */
@media only screen and (min-width:320px) and (max-width:480px) and (orientation:portrait) { 
    .background img {
      height: 10px;
    }
}
/* iPhone 5 (landscape) */
@media only screen and (min-width:320px) and (max-width:568px) and (orientation:landscape) { 
    .background img { 
      height: 245px;
    }
    .logo img {
        height: 205px;
        width: 205px;
    }
}
/* iPhone 5 (portrait) */
@media only screen and (min-width:320px) and (max-width:568px) and (orientation:portrait) { 
    .background img {
      height: 210px;
    }
    .logo img {
        height: 170px;
        width: 170px;
    }
    .top-content h2 {
        font-size: 1.8em;
        line-height: 120%;
        margin-bottom: 20px;
    }
    .main-container {
        margin-top: 30px;
        margin-left: 15px;
        margin-right: 15px;
    }
}

问题是在 iPhone 5 上,也应用了 iPhone 4 的样式。我怎样才能防止这种情况发生?

【问题讨论】:

  • 你好严,欢迎来到 Stack Overflow。以后请在编辑器中将代码示例缩进并标记为代码,以便语法高亮和更容易理解。谢谢!
  • 您不应该需要单独的媒体查询来说明不同的垂直视口大小。例如,考虑使用fixed 定位。
  • 你能举个例子吗?

标签: css twitter-bootstrap media-queries


【解决方案1】:

特定 iPhone 4 的媒体屏幕如下:

@media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-device-pixel-ratio: 2) and (device-aspect-ratio: 2/3)
{
    ...
}

【讨论】:

    【解决方案2】:

    除了 Adam 的有用回答之外,我还进一步扩展了这一点,以尝试在我的案例中将我的 CSS 推送到 iPhone 和 iPad 的两个方向。以下内容可能对查看此问题的任何人有用:

    /* iPhone 5/5S Retina Display Portrait */
    @media screen and (device-aspect-ratio: 40/71) and (max-device-width: 640px) and (orientation:portrait) {}
    
    /* iPhone 5/5S Retina Display Landscape */
    @media screen and (device-aspect-ratio: 40/71) and (max-device-width: 640px) and (orientation:landscape) {}
    
    /* iPad 3/4/Air Retina Display Portrait */
    @media screen and (device-aspect-ratio: 3/4) and (max-device-width: 1536px) and (orientation:portrait) {}
    
    /* iPad 3/4/Air Retina Display Landscape */
    @media screen and (device-aspect-ratio: 3/4) and (max-device-width: 1536px) and (orientation:landscape) {}
    

    【讨论】:

      【解决方案3】:

      另一个有用的媒体功能是设备纵横比。

      请注意,iPhone 5 没有 16:9 的纵横比。实际上是 40:71。

      iPhone < 5:
      @media screen and (device-aspect-ratio: 2/3) {}
      
      iPhone 5:
      @media screen and (device-aspect-ratio: 40/71) {}
      
      iPad:
      @media screen and (device-aspect-ratio: 3/4) {}
      

      参考:Media Queries @ W3C

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-21
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        • 2012-09-14
        • 2012-11-30
        • 2018-01-14
        • 2013-01-03
        相关资源
        最近更新 更多