简体   繁体   English

如何阻止移动CSS被主要CSS覆盖

[英]How to stop mobile CSS from getting overridden by main CSS

My CSS contains the following code for the footer: 我的CSS包含以下用于页脚的代码:

footer
{
text-align:center;
font-size:small;
    /* Part 3/3 of Sticky Footer code */
position: absolute;
left: 0;
bottom: 0;
height: 30px;
width: 100%;
overflow: hidden;
}

The footer text was getting cut off on mobile, so I added the following line: 页脚文本在移动设备上已被删除,因此我添加了以下行:

@media only screen and (orientation: portrait) {
footer
{
height: 40px;
}
}

The 40px height rule gets overridden by the 30px rule. 40px高度规则被30px规则覆盖。 It only works with !important so I am a bit confused. 它仅与!important所以我有点困惑。 Which CSS rules? 哪些CSS规则? Shouldn't media-specific queries follow their own rules in case of a conflict? 在发生冲突的情况下,特定于媒体的查询不应该遵循自己的规则吗? Isn't that the whole point of having separate CSS rules for different media types? 为不同的媒体类型设置单独的CSS规则不是全部意思吗?

Check what is the placement of your CSS. 检查CSS的位置。

If CSS class in one file make sure that mobile CSS class is written after main CSS. 如果将CSS类放在一个文件中,请确保将移动CSS类写在主CSS之后。

If they are in multiple files then mobile CSS should be added after main CSS. 如果它们在多个文件中,则应在主CSS之后添加移动CSS。

This is not how mediaqueries work. 这不是媒体查询的工作方式。 CSS still follows the same rule of specifity as always. CSS仍然遵循与以往相同的指定规则。 If you write footer {...} and then 10 lines after this statement you write footer { ... } again the styles in the second footer overwrite the one from the previous line. 如果先写footer {...} ,然后在此语句后写10行,则再次写footer { ... } ,第二个页脚中的样式将覆盖前一行中的样式。

The same is true for styles with @media rule, they just get evaluated on certain screen sizes. 具有@media规则的样式也是如此,它们只是在某些屏幕尺寸上进行评估。 You don't say "this is how this must look like for this resolution". 您不必说“这就是此决议的外观”。 Otherwise you would have to write lots of duplicate CSS. 否则,您将不得不编写大量重复的CSS。 You can overwrite styles, but you also have to take care about the order in which you place your styles. 您可以覆盖样式,但也必须注意放置样式的顺序。

You wrote that the mobile footer rule is at the top of the file. 您写道,移动页脚规则位于文件的顶部。 If you want to overwrite the rule for this devices you should move all your mobile CSS to the bottom of the file. 如果要覆盖此设备的规则,则应将所有移动CSS移至文件底部。

I alwayss follow css calc method 我总是遵循CSS Calc方法
the problem was found on your footer height so to override this u can use calc method on CSS this is useful. 在您的页脚高度上发现了问题,因此可以在CSS上使用calc方法来覆盖此问题,这一点很有用。

@media only screen and (orientation: portrait) {
footer{
    height: calc( 100% + 40px );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM