CSS @Media 最小宽度和最大宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13550541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:52:20  来源:igfitidea点击:

@Media min-width & max-width

cssmobilemedia-queries

提问by rallybilen

I have this @mediasetup:

我有这个@media设置:

HTML:

HTML:

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

CSS:

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 */
}

With this setup it works on the iPhone but it does not work in the browser.

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

Is it because I already have devicein the meta, and maybe have max-width:480pxinstead?

是不是因为我已经device在元数据中了,也许已经有了max-width:480px

回答by bashleigh

I've found the best method is to write your default CSS for the older browsers, as older browsers including i.e. 5.5, 6, 7 and 8. Can't read @media. When I use @media I use it like this:

我发现最好的方法是为旧版浏览器编写默认 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>

But you can do whatever you like with your @media, This is just an example of what I've found best for me when building styles for all browsers.

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

iPad CSS specifications.

iPad CSS 规范。

Also! If you're looking for printability you can use @media print{}

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

回答by jpostdesign

The underlying issue is using max-device-widthvs plain old max-width.

潜在的问题是使用max-device-widthvs plain old max-width

Using the "device" keyword targets physical dimension of the screen, not the width of the browser window.

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

For example:

例如:

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

Versus

相对

@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.  */
}

回答by Rohit Kaushik

If website on small devices behavior like desktop screen then you have to put this meta tag into header before

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

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

For media queries you can set this as

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

this will cover your all mobile/cellphone widths

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

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

    }

For iPad and iPad pro you have to use

对于 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 //

    }

If you want to add css for Landscape mode you can add this

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

and (orientation : landscape)

和(方向:风景)

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

        }

回答by Ksenia Titova

The correct value for the contentattribute should include initial-scaleinstead:

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

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

回答by NuggaN85

for some iPhone you have to put your viewport like this

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

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