Media Queries overriding each other [duplicate]

丶灬走出姿态 提交于 2020-06-03 05:04:27

问题


I have a general question about media queries. The site I am working on has two responsive viewports:

  • max-width: 414 px (iPhone) and
  • max-width: 770px (iPad)

The style tags are written in that order in the css document. Because they are cascading, my iPad queries keep overriding my iPhone queries (and vice versa).

Is there a simple solution to this? It seems like I'm missing something really obvious.


回答1:


When using the max width media query, order must be from largest to smallest. The opposite is true for min-width.

You'll find a detailed explanation of this here.

It's a nested layout, it should look something like this:

@media (max-width: 770px) {
  .css-rule {
  }
}

@media (max-width: 414px) {
  .css-rule {
  }
}

You can also try specifying a min width to the iPad size, which will make sure that the css contained in it will not fire below that width, like this:

@media (min-width: 415px) and (max-width:770px) {}

If you are loading external CSS files they may be overriding your file. Make sure that you put your most important, usually your custom CSS file, as the LAST file.

Check these guys out for media queries, they usually know what's up.

Let us know if it helped!



来源:https://stackoverflow.com/questions/41620499/media-queries-overriding-each-other

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!