html - 媒体查询规则不优先

标签 html css media-queries

所以我在一个网站上工作,遇到一个奇怪的媒体查询问题,一些规则被覆盖了。它用于模板,因此有一个基本的“框架”CSS,然后是另一个特定于模板的 CSS 文件。为清楚起见,这是我的 HTML 文件中的顺序

<link href="/themes/Base/base.min.css" rel="stylesheet" type="text/css"> <!-- Contains all of the base rules, including the base mobile rules-->
<link href="/themes/MyTheme/theme.min.css" rel="stylesheet" type="text/css"> <!-- Contains all of the theme specific overrides-->

base.min.css 我有这些规则:

@media all and (max-width: 800px) {
....
#menu {
position: relative;
width: 100%;
min-height: auto;
margin: 0px;
padding: 0px;
top: 0px;
right: 0px;
text-align: center;
border-bottom-width: 1px;
border-bottom-style: solid;
border-top-width: 1px;
border-top-style: solid;
background-image: none;
}
....

然后在 theme.min.css 文件中,在 HTML 文件之后,我有这些规则

....
#menu {
box-sizing: border-box;
text-align: left;
padding: 10px;
min-height: 320px;
width: 300px;
box-shadow: 0 0 5px #000000;
position: relative;
margin: -20px 0 0 0;
z-index: 100;
background-position: bottom right;
background-repeat: no-repeat;
background-image: url(/themes/global/images/watermarks/watermark9.png);
}
....

请注意,第二组规则不包含在媒体查询中。尽管如此,其中的大部分规则都凌驾于之前的规则之上。高度、宽度、背景图像等。我假设如果其他一切都相同(尽管稍后会出现),媒体查询将具有优先权,因为它增加了特异性。我的意思是,如果需要的话,我可以在上面放一堆 !important;,但这看起来像是创可贴,而不是解决方案。

最佳答案

本质上,您的第二组 CSS 是“所有内容”的媒体查询,并且由于它位于最大宽度规则之后,因此它也将应用于最大宽度 800 像素的 View 。

我认为这些规则中的一些不应该在 800 像素以下应用是正确的吗?如果是这样,将它们放在最小宽度规则中,然后它们将不会被使用。例如在 theme.css 中:

#menu {

    /*Put rules for any width in here*/

}    

@media all and (min-width: 801px) {
    #menu {
        /*Put rules you don't want inherited by max 800 in here*/
        min-height: 320px;
        width: 300px;
        ...
    }
}

关于html - 媒体查询规则不优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36166728/

相关文章:

html - 如何让页面元素在屏幕尺寸不同时清晰显示固定到顶部的流体图像?

css - 有没有办法让 2 或 3 或 4 div 共享容器 div 的相同空间

javascript - jQuery show() 方法不适用于 Chrome 和 IE 浏览器

html - 在 div 内的 css 中将内容对齐?

html - 悬停时布局更改

css - 如何使用 CSS 隐藏 title 属性?

html - CSS @import 导致媒体查询类不起作用

javascript - Firefox 和 Chrome 页面底部的空白

javascript - 响应式图像 - 在不同的屏幕尺寸上调整图像大小

jquery - 通过提供 url 更改页面背景 - jquery