如何避免使用!important? [英] How do I avoid using !important?

查看:135
本文介绍了如何避免使用!important?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的网站具有响应能力.但是,当我使用其他媒体查询时,很多事情将无法正常工作.例如:

I am trying to make my website responsive. However, when I use another media query, a lot of things won't work. For example:

(普通CSS)

div#divName
{
    font-size:1em;
}

(媒体查询代码)

@media screen and (max-width: 320px) 
{
    div#divName
    {
        font-size:.5em;
    }
}

这不起作用.仅当我在其后面使用!important"时,它才有效.但是我不知道那是正确的还是错误的".谁能告诉我该如何解决?

This doesn't work. It only works when I use "!important" behind it. But I don't know if that is correct or "wrong". Could anyone tell me how I can fix this?

推荐答案

所有!important所做的只是增加特异性.为了避免使用!important,您需要做的就是提高特异性.

All !important does is increase specificity. To avoid using !important, all you need to do is increase specificity.

在您的情况下,两个选择器都具有相同的特异性.该问题很可能是由于将媒体查询放置在普通CSS"之前而被覆盖的.

In your case, both of your selectors have identical specificity. The issue is most likely caused by your media query being placed before your "Normal CSS", and thus getting overridden.

  1. 如果它们位于同一CSS文件中,请确保在媒体查询之前放置普通CSS".
  2. 如果它们位于不同的CSS文件中,请确保将包含您的媒体查询的文件包含在HTML文档的" 之后常规CSS"中.

这篇关于如何避免使用!important?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆