【问题标题】:@Media min-width & max-width@Media 最小宽度和最大宽度
【发布时间】:2012-11-13 02:02:47
【问题描述】:

我有这个@media 设置:

HTML

<head>
  <meta name="viewport" content="width=device-width, user-scalable=no" />
</head>

CSS

@media screen and (min-width: 769px) {
    /* STYLES HERE */
}

@media screen and (min-device-width: 481px) and (max-device-width: 768px) { 
    /* STYLES HERE */
}

@media only screen and (max-device-width: 480px) {
    /* STYLES HERE */
}

通过此设置,它可以在 iPhone 上运行,但不能在浏览器中运行。

是不是因为我已经在元数据中有device,而可能有max-width:480px

【问题讨论】:

  • 这是什么问题 - 默认样式将适用于宽度超过 769 像素的屏幕。
  • 只需删除常规浏览器样式周围的@media screen and (min-width:769px){
  • Zoltan 是什么意思?我想我可能会解决它。首先,问题是当我使用 max-device 调整浏览器的大小时,它可以在手机上运行,​​但不能在浏览器中运行,如果没有“设备”,它在两者都可以运行。
  • 所以基本上没有“设备”,它可以在移动/浏览器中使用,但是当我添加设备时,我在浏览器中看不到它\
  • 960px 更好,我记得读过你应该为一些播放不好的浏览器上的垂直滚动条节省 20px。

标签: css mobile media-queries


【解决方案1】:

我发现最好的方法是为旧版浏览器编写默认 CSS,因为旧版浏览器(包括 IE 5.5、6、7 和 8)无法读取 @media。当我使用@media时,我是这样使用的:

<style type="text/css">
    /* default styles here for older browsers. 
       I tend to go for a 600px - 960px width max but using percentages
    */
    @media only screen and (min-width: 960px) {
        /* styles for browsers larger than 960px; */
    }
    @media only screen and (min-width: 1440px) {
        /* styles for browsers larger than 1440px; */
    }
    @media only screen and (min-width: 2000px) {
        /* for sumo sized (mac) screens */
    }
    @media only screen and (max-device-width: 480px) {
       /* styles for mobile browsers smaller than 480px; (iPhone) */
    }
    @media only screen and (device-width: 768px) {
       /* default iPad screens */
    }
    /* different techniques for iPad screening */
    @media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
      /* For portrait layouts only */
    }

    @media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
      /* For landscape layouts only */
    }
</style>

但是您可以使用@media 做任何您想做的事情。这只是我在为所有浏览器构建样式时发现的最适合我的一个示例。

iPad CSS specifications.

还有!如果您正在寻找可打印性,您可以使用@media print{}

【讨论】:

  • 为什么要放置“mac”屏幕?拥有高分辨率大屏幕的不仅仅是“惊人”的 Mac!
【解决方案2】:

根本问题是使用max-device-width 与普通的旧max-width

使用“设备”关键字的目标是屏幕的物理尺寸,而不是浏览器窗口的宽度。

例如:

@media only screen and (max-device-width: 480px) {
    /* STYLES HERE for DEVICES with physical max-screen width of 480px */
}

对比

@media only screen and (max-width: 480px) {
    /* STYLES HERE for BROWSER WINDOWS with a max-width of 480px. 
       This will work on desktops when the window is narrowed.  */
}

【讨论】:

    【解决方案3】:

    如果网站在小型设备上的行为像桌面屏幕,那么您必须在此之前将此元标记放入标题中

    <meta name="viewport" content="width=device-width, initial-scale=1">
    

    对于媒体查询,您可以将其设置为

    这将涵盖您所有的手机/手机宽度

     @media only screen and (min-width: 200px) and (max-width: 767px)  {
        //Put your CSS here for 200px to 767px width devices (cover all width between 200px to 767px //
       
        }
    

    对于 iPad 和 iPad pro,您必须使用

      @media only screen and (min-width: 768px) and (max-width: 1024px)  {
            //Put your CSS here for 768px to 1024px width devices(covers all width between 768px to 1024px //   
      }
    

    如果你想为横向模式添加 css,你可以添加这个

    和(方向:横向)

      @media only screen and (min-width: 200px) and (max-width: 767px) and (orientation : portrait) {
            //Put your CSS here for 200px to 767px width devices (cover all mobile portrait width //        
      }
    

    【讨论】:

      【解决方案4】:

      content 属性的正确值应改为包含initial-scale

      <meta name="viewport" content="width=device-width, initial-scale=1">
                                                         ^^^^^^^^^^^^^^^

      【讨论】:

      • 如果你能解释原因并添加参考资料,那将使这个答案更有用。
      • @Michael_B 最初是 'user-scalable=no'。这就是我想的全部。
      【解决方案5】:

      如果您想在浏览器中同时包含最小和最大宽度以实现响应,那么您可以使用以下内容:

       @media (min-width: 768px) and (max-width: 992px){...}
       @media (min-width: 480px) and (max-width: 767px) {...}
      

      【讨论】:

        【解决方案6】:

        对于某些 iPhone,您必须像这样放置视口

        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, shrink-to-fit=no, user-scalable=0" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-14
          • 2016-01-07
          • 1970-01-01
          • 2011-10-18
          • 1970-01-01
          • 1970-01-01
          • 2020-10-25
          • 1970-01-01
          相关资源
          最近更新 更多