@Media min-width&max-width

ral*_*len 207 css mobile media-queries

我有这个@media设置:

HTML:

<head>
  <meta name="viewport" content="width=device-width, user-scalable=no" />
</head>
Run Code Online (Sandbox Code Playgroud)

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 */
}
Run Code Online (Sandbox Code Playgroud)

使用此设置它可以在iPhone上运行,但它在浏览器中不起作用.

是因为我已经device在meta中了,也许还有max-width:480px

sou*_*rri 315

我发现最好的方法是为旧浏览器编写默认的CSS,因为旧的浏览器包括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>
Run Code Online (Sandbox Code Playgroud)

但你可以用@media做任何你喜欢的事情,这只是我为所有浏览器构建样式时最适合我的一个例子.

iPad CSS规格.

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

  • 为什么要放'mac'屏幕?它不仅是"惊人的"Mac,可以拥有大型高分辨率屏幕! (13认同)

jpo*_*ign 35

根本问题是使用max-device-widthvs plain old max-width.

使用"device"关键字定位屏幕的物理尺寸,而不是浏览器窗口的宽度.

例如:

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

@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.  */
}
Run Code Online (Sandbox Code Playgroud)


小智 9

content属性的正确值应包括initial-scale:

<meta name="viewport" content="width=device-width, initial-scale=1">
                                                   ^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

  • 如果您可以解释原因并添加引用,那么这将使这个答案更有用. (2认同)
  • @Michael_B最初是'user-scalable = no'。这说明了我的全部想法。 (2认同)

Pan*_*att 7

如果您想在浏览器中同时包含最小和最大宽度以实现响应能力,则可以使用以下命令:

 @media (min-width: 768px) and (max-width: 992px){...}
 @media (min-width: 480px) and (max-width: 767px) {...}
Run Code Online (Sandbox Code Playgroud)


Roh*_*hik 5

如果网站在小型设备上的行为(例如桌面屏幕),那么您必须将此meta标记放在标题之前

<meta name="viewport" content="width=device-width, initial-scale=1">
Run Code Online (Sandbox Code Playgroud)

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

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

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

    }
Run Code Online (Sandbox Code Playgroud)

对于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 iPad portrait width //

    }
Run Code Online (Sandbox Code Playgroud)

如果要为横向模式添加CSS,则可以添加

和(方向:风景)

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

        }
Run Code Online (Sandbox Code Playgroud)