【问题标题】:CSS Media query (max-height) isn't working but why?CSS 媒体查询(最大高度)不起作用,但为什么?
【发布时间】:2016-09-15 11:57:50
【问题描述】:

我有页面 - http://pgkweb.ru/temp/1/index.html 和它的样式 - http://pgkweb.ru/temp/1/include/style.css。对于移动设备,我需要扩大块之间的边距。所以我尝试了

@media (max-height: 800px)

它不起作用(背景不是金色的)。

如果我这样做了

 @media not screen and (max-height: 800px)

我的 PC 上的背景更改。有没有可能让它发挥作用?

【问题讨论】:

    标签: css media-queries


    【解决方案1】:

    确保您有一个viewport 标签,例如<meta name="viewport" content="width=device-width,initial-scale=1.0">

    【讨论】:

    • 嗯,这有点帮助,谢谢。但我仍然无法理解如何更改最小高度
    【解决方案2】:

    你的 html 页面有 &lt;meta name="viewport" content="width=device-width, initial-scale=0.6"&gt; 应该是 <meta name="viewport" content="width=device-width, initial-scale=1.0">

    将所有这些代码添加到现有 css 表的末尾

    /*
      Based on:
      1. http://stephen.io/mediaqueries
      2. https://css-tricks.com/snippets/css/media-queries-for-standard-devices/
    */
    
    /* iPhone 6 in portrait & landscape */
    @media only screen 
    and (min-device-width : 375px) 
    and (max-device-width : 667px) {
      
    }
    
    /* iPhone 6 in landscape */
    @media only screen 
    and (min-device-width : 375px) 
    and (max-device-width : 667px) 
    and (orientation : landscape) {
      
    }
    
    /* iPhone 6 in portrait */
    @media only screen 
    and (min-device-width : 375px) 
    and (max-device-width : 667px) 
    and (orientation : portrait) {
      
    }
    
    /* iPhone 6 Plus in portrait & landscape */
    @media only screen 
    and (min-device-width : 414px) 
    and (max-device-width : 736px) {
      
    }
    
    /* iPhone 6 Plus in landscape */
    @media only screen 
    and (min-device-width : 414px) 
    and (max-device-width : 736px) 
    and (orientation : landscape) {
      
    }
    
    /* iPhone 6 Plus in portrait */
    @media only screen 
    and (min-device-width : 414px) 
    and (max-device-width : 736px) 
    and (orientation : portrait) {
      
    }
    
    /* iPhone 5 & 5S in portrait & landscape */
    @media only screen 
    and (min-device-width : 320px) 
    and (max-device-width : 568px) {
      
    }
    
    /* iPhone 5 & 5S in landscape */
    @media only screen 
    and (min-device-width : 320px) 
    and (max-device-width : 568px) 
    and (orientation : landscape) {
      
    }
    
    /* iPhone 5 & 5S in portrait */
    @media only screen 
    and (min-device-width : 320px) 
    and (max-device-width : 568px) 
    and (orientation : portrait) {
      
    }
    
    /* 
      iPhone 2G, 3G, 4, 4S Media Queries
      It's noteworthy that these media queries are also the same for iPod Touch generations 1-4.
    */
    
    /* iPhone 2G-4S in portrait & landscape */
    @media only screen 
    and (min-device-width : 320px) 
    and (max-device-width : 480px) {
      
    }
    
    /* iPhone 2G-4S in landscape */
    @media only screen 
    and (min-device-width : 320px) 
    and (max-device-width : 480px) 
    and (orientation : landscape) {
      
    }
    
    /* iPhone 2G-4S in portrait */
    @media only screen 
    and (min-device-width : 320px) 
    and (max-device-width : 480px) 
    and (orientation : portrait) {
      
    }
    
    /* iPad in portrait & landscape */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px)  {
      
    }
    
    /* iPad in landscape */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : landscape) {
      
    }
    
    /* iPad in portrait */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : portrait) {
      
    }
    
    /* Galaxy S3 portrait and landscape */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 2) {
    
    }
    
    /* Galaxy S3 portrait */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 2) 
      and (orientation: portrait) {
    
    }
    
    /* Galaxy S3 landscape */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 2) 
      and (orientation: landscape) {
    
    }
    
    /* Galaxy S4 portrait and landscape */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) {
    
    }
    
    /* Galaxy S4 portrait */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: portrait) {
    
    }
    
    /* Galaxy S4 landscape */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: landscape) {
    
    }
    
    /* Galaxy S5 portrait and landscape */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) {
    
    }
    
    /* Galaxy S5 portrait */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: portrait) {
    
    }
    
    /* Galaxy S5 landscape */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: landscape) {
    
    }
    
    /* HTC One portrait and landscape */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) {
    
    }
    
    /* HTC One portrait */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: portrait) {
    
    }
    
    /* HTC One landscape */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: landscape) {
    
    }
    
    /*
      iPad 3 & 4 Media Queries
      If you're looking to target only 3rd and 4th generation Retina iPads 
      (or tablets with similar resolution) to add @2x graphics,
      or other features for the tablet's Retina display, use the following media queries.
    */
    
    /* Retina iPad in portrait & landscape */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px)
    and (-webkit-min-device-pixel-ratio: 2) {
      
    }
    
    /* Retina iPad in landscape */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : landscape)
    and (-webkit-min-device-pixel-ratio: 2) {
      
    }
    
    /* Retina iPad in portrait */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : portrait)
    and (-webkit-min-device-pixel-ratio: 2) {
      
    }
    
    /*
      iPad 1 & 2 Media Queries
      If you're looking to supply different graphics or choose different typography 
      for the lower resolution iPad display, the media queries below will work 
      like a charm in your responsive design!
    */
    
    /* iPad 1 & 2 in portrait & landscape */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (-webkit-min-device-pixel-ratio: 1) {
      
    }
    
    /* iPad 1 & 2 in landscape */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : landscape)
    and (-webkit-min-device-pixel-ratio: 1) {
      
    }
    
    /* iPad 1 & 2 in portrait */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : portrait) 
    and (-webkit-min-device-pixel-ratio: 1) {
      
    }
    
    /* iPad mini in portrait & landscape */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px)
    and (-webkit-min-device-pixel-ratio: 1) {
      
    }
    
    /* iPad mini in landscape */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : landscape)
    and (-webkit-min-device-pixel-ratio: 1) {
    
    }
    
    /* iPad mini in portrait */
    @media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : portrait)
    and (-webkit-min-device-pixel-ratio: 1) {
      
    }
    
    /* Galaxy Tab 10.1 portrait and landscape */
    @media
      (min-device-width: 800px) 
      and (max-device-width: 1280px) {
    
    }
    
    /* Galaxy Tab 10.1 portrait */
    @media 
      (max-device-width: 800px) 
      and (orientation: portrait) { 
    
    }
    
    /* Galaxy Tab 10.1 landscape */
    @media 
      (max-device-width: 1280px) 
      and (orientation: landscape) { 
    
    }
    
    /* Asus Nexus 7 portrait and landscape */
    @media screen 
      and (device-width: 601px) 
      and (device-height: 906px) 
      and (-webkit-min-device-pixel-ratio: 1.331) 
      and (-webkit-max-device-pixel-ratio: 1.332) {
    
    }
    
    /* Asus Nexus 7 portrait */
    @media screen 
      and (device-width: 601px) 
      and (device-height: 906px) 
      and (-webkit-min-device-pixel-ratio: 1.331) 
      and (-webkit-max-device-pixel-ratio: 1.332) 
      and (orientation: portrait) {
    
    }
    
    /* Asus Nexus 7 landscape */
    @media screen 
      and (device-width: 601px) 
      and (device-height: 906px) 
      and (-webkit-min-device-pixel-ratio: 1.331) 
      and (-webkit-max-device-pixel-ratio: 1.332) 
      and (orientation: landscape) {
    
    }
    
    /* Kindle Fire HD 7" portrait and landscape */
    @media only screen 
      and (min-device-width: 800px) 
      and (max-device-width: 1280px) 
      and (-webkit-min-device-pixel-ratio: 1.5) {
    
    }
    
    /* Kindle Fire HD 7" portrait */
    @media only screen 
      and (min-device-width: 800px) 
      and (max-device-width: 1280px) 
      and (-webkit-min-device-pixel-ratio: 1.5) 
      and (orientation: portrait) {
        
    }
    
    /* Kindle Fire HD 7" landscape */
    @media only screen 
      and (min-device-width: 800px) 
      and (max-device-width: 1280px) 
      and (-webkit-min-device-pixel-ratio: 1.5) 
      and (orientation: landscape) {
    
    }
    
    /* Kindle Fire HD 8.9" portrait and landscape */
    @media only screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 1.5) {
    
    }
    
    /* Kindle Fire HD 8.9" portrait */
    @media only screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 1.5) 
      and (orientation: portrait) {
        
    }
    
    /* Kindle Fire HD 8.9" landscape */
    @media only screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 1.5) 
      and (orientation: landscape) {
    
    }
    
    /* Laptops non-retina screens */
    @media screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 1) {
        
    }
    
    /* Laptops retina screens */
    @media screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 2)
      and (min-resolution: 192dpi) {
        
    }
    
    /* Apple Watch */
    @media
      (max-device-width: 42mm)
      and (min-device-width: 38mm) { 
    
    }
    
    /* Moto 360 Watch */
    @media 
      (max-device-width: 218px)
      and (max-device-height: 281px) { 
    
    }

    【讨论】:

    • 不幸的是它没有帮助 - 移动版本中的文本很大。工商管理硕士我的错是字体大小和边距百分比:font-size:200%;?
    • 不行,没用,手机版内容非常非常大 - pgkweb.ru/temp/1/2/index.html
    【解决方案3】:

    终于用@media (max-width: 600px) 解决了,在我看来,max-height 确实有问题,或者我无法以正确的方式烹饪它。

    【讨论】:

    • 我也有同样的问题。在我的例子中,我追踪到受影响的项目被加载到宽度和高度设置为 100% 的 iFrame 中。如果我在 iFrame 设置 min-height 之外加载页面。如果在 iFrame 中加载,则只有 min-width 有效。
    • @bgx 感谢您的评论 - 一个带有硬编码 height: Xpx; 的 iframe 原来是我的问题。
    【解决方案4】:

    我有一个非常相似的问题。

    对我来说,我定位的 div 是 position: fixed; div 与 height: 100vh;width: 100vw; 的子级。将 vh 和 vw 更改为 % 解决了我的问题。

    iOS 11 似乎存在与上述相关的错误,因为 Chrome 开发工具在渲染 iPhone 6 时响应正确,但手机本身渲染不正确。

    例如

    <div class="fixed-parent">
        <div class="absolute-child" />
    </div>
    
    .fixed-parent {
        // height: 100vh; <--- buggy /w max-height media query on iOS 11
        // width: 100vw;  <--- buggy /w max-height media query on iOS 11
        height: 100%;
        width: 100%;
    
        position: fixed;
        top: 0; left: 0;
    }
    
    .absolute-child {
        position: absolute;
        top: 50%; left: 50%;
        transform: translate(-50%, -50%);
    
        @media screen and (max-height: 800px) {
            top: 45%;
        }
    }
    

    【讨论】:

      【解决方案5】:

      为了解决这个问题,我只需要使用“不缓存刷新”(ctrl + F5), 然后就可以了。

      @media screen 的 CSS 是这样的

      @media screen and (max-height: 800px) {
      
      }
      

      只是分享一下我刚刚解决了我的问题。

      希望对您有所帮助!

      【讨论】:

        猜你喜欢
        • 2016-10-20
        • 2012-07-09
        • 1970-01-01
        • 2015-05-07
        • 2021-03-02
        • 2021-12-29
        • 2016-07-30
        相关资源
        最近更新 更多