什么时候应该使用CSS!important规则?

news/2024/7/5 0:49:41

The !important rule in CSS gives you the ability to override styles, but is this power too much for a lone software developer? Read this article to find out what it is, and when it’s a good idea to use it!

CSS中的!important规则使您能够覆盖样式,但是对于一个孤独的软件开发人员来说,此功能太大了吗? 阅读本文以了解它是什么,以及什么时候使用它是一个好主意!

The !important rule is special because it has the ability to override the natural cascade of CSS styles.

!important规则很特殊,因为它可以覆盖CSS样式的自然级联 。

Consider the following HTML/CSS code… What color do you think the text will be?

考虑下面HTML / CSS代码...您认为文本是什么颜色?

<div>
  <h1 id="title">Sir Chompsalot</h1>
</div>
div h1 {
  color: blue !important;
}

div h1 {
  color: green;
}

Normally, if we have two CSS rules with identical specificity the latter rule would win. In this case, the earlier CSS rule beats any latter rules simply because it has the powerful !important rule.

通常,如果我们有两个CSS规则具有相同的特异性,则后一个规则将获胜。 在这种情况下,较早CSS规则仅因为它具有强大的!important规则而击败了所有后继规则。

The text is blue!

文字为蓝色!

Using the same HTML markup, what if we got even more specific by targeting the body tag and h1#title?

使用相同HTML标记,如果我们通过将body标签和h1#title定位为更具体的内容怎么办?

div h1 {
  color: blue !important;
}

body div h1#title {
  color: green;
}

Will this have the ability to override the !important rule?

这是否有能力覆盖!important规则?

Nope! Wow, the !important rule is almost too powerful for its own good.

不! 哇, !important规则本身就太强大了。



💪🤖💪 (💪🤖💪)

Since !important contradicts the expected behavior of CSS, it’s generally recommended to avoid using it. If you rely on !important too often it can cause a lot of unexpected behavior down the road, and new developers will have a hard time debugging their CSS. Why isn’t this text changing color! 😂

由于!important与CSS的预期行为相矛盾,因此通常建议避免使用它。 如果过分依赖!important ,可能会导致很多意外行为,并且新开​​发人员将很难调试CSS。 为什么文本没有变色! 😂

Does this mean you should never use it?

这是否意味着您永远不要使用它?

重要使用场合!重要 (Occasions to Use !important)

As time has passed since !important was introduced to CSS there seems to be some consensus that it’s really only useful for a single job: dealing with foreign CSS.

自从!important被引入CSS以来,随着时间的流逝,似乎已经达成共识,即它实际上仅对一项工作有用:处理外来CSS 。

Foreign CSS is essentially any CSS that you don’t have direct ability to change or improve yourself. Two practical instances of foreign CSS are:

外国CSS本质上是您没有直接能力来改变或改善自己的任何CSS。 国外CSS的两个实际实例是:

  • JavaScript Frameworks & External Libraries: This applies to popular libraries like Bootstrap, or Normalize. Since you can’t edit their CSS styles directly, sometimes your only option is override their CSS with your own !important rules.

    JavaScript框架和外部库 :这适用于流行的库,例如Bootstrap或Normalize 。 由于您不能直接编辑其CSS样式,因此有时唯一的选择是使用自己的!important规则覆盖它们CSS。

  • User Styles: This used to be very popular years ago. User Styles provides a way for you to create your own CSS to inject on websites owned by other people/companies. For example, this dark theme for instagram.com

    用户风格 :几年前非常流行。 用户样式为您提供了一种创建自己CSS的方式,以注入到其他人/公司拥有的网站上。 例如, instagram.com的这个深色主题

Since foreign CSS can’t be changed by you (unless you take a job at Instagram to rectify their serious lack of a dark mode), the !important rule is really your only, and best option.

由于您无法更改外国CSS(除非您在Instagram上工作以纠正他们严重缺乏的黑暗模式),所以!important规则确实是您唯一的也是最佳选择。

The folks at Mozilla Developer Network and CSS-Tricks seem to agree that !important is really only useful for dealing with foreign CSS

Mozilla开发人员网络和CSS-Tricks的人们似乎都同意!important实际上仅对处理外国CSS有用。

结论 (Conclusion)

If you are really tempted to use !important, try reflecting on architectural decisions that you can make. Not just your CSS either. This could mean adding new HTML tags, or applying new classes/ids. Engaging in this architecture-centric practice results in high quality code that’s a joy to maintain! 🤓

如果您真的很想使用!important ,请尝试思考可以做出的体系结构决策。 不仅是CSS。 这可能意味着添加新HTML标记或应用新的类/标识。 从事这种以架构为中心的实践,可以得到高质量的代码,这是维护的乐趣! 🤓

Have you found other appropriate uses for !important? Tweet us at @alligatorio and share it with us!

您找到!important其他合适用法了吗? 通过@alligatorio发推 ,与我们分享!

翻译自: https://www.digitalocean.com/community/tutorials/css-important


http://www.niftyadmin.cn/n/3648904.html

相关文章

自定义view——水平进度条

做之前需要了解一些知识 文字宽高获取:一般有以下几种方式 ①&#xff1a;设置文字矩形&#xff0c;Paint.getTextBounds方法得到宽高 String test "Android世界"; Rect rect new Rect(); mPaint.getTextBounds(text, 0, test.length(), rect); int width rect…

Android属性allowBackup安全风险浅析

1.allowBackup安全风险描述 Android API Level 8及其以上Android系统提供了为应用程序数据的备份和恢复功能&#xff0c;此功能的开关决定于该应用程序中AndroidManifest.xml文件中的allowBackup属性值[1] &#xff0c;其属性值默认是True。当allowBackup标志为true时&#xff…

软件测试管理--目录

前 言第一部分 基础篇第1章 测试管理概论1.1 三个基础测试概念1.2 软件测试基本流程1.3 软件测试管理攻略1.3.1测试管理主要内容1.3.2本书测试管理攻略第2章 软件测试背景2.1 测试案例2.2 软件测试发展历史2.3 国内IT企业测试行业分析第二部分 管理篇第3 章管理团队第4 章测试…

如何在Ubuntu 18.04上使用Docker Compose打包Laravel应用程序进行开发

介绍 (Introduction) To containerize an application refers to the process of adapting an application and its components in order to be able to run it in lightweight environments known as containers. Such environments are isolated and disposable, and can be …

自定义view-实现计步器的效果

首先看下效果图 在做这个项目之前先了解下文字获取 我之前也写过一篇文章是自定义view——自定义圆环进度条:http://blog.csdn.net/qq_24675479/article/details/78880078 今天详细讲解一下baseLine 基线&#xff08;参考文章&#xff1a;文淑大神的自定义View之绘图篇&#x…

马克吐温刷墙与碗底的诱惑

马克吐温刷墙马克.吐温小时侯&#xff0c;有一天因为逃学&#xff0c;被妈妈罚着去刷围墙。围墙有30码长&#xff0c;比他的头顶还高出很多。他把刷子蘸上灰浆&#xff0c;刷了几下。看着这么长的围墙&#xff0c;他灰心丧气地坐了下来。伙伴桑迪提着水桶走过来。“桑迪&#x…

Android 解决APN无权限问题

在ICS40以前的版本中&#xff0c;如果程序需要设置APN&#xff0c;只需要在AndroidManifest文件中声明这个权限即可。在40的机器上运行则会抛出以下异常&#xff1a;java.lang.SecurityException: No permission to write APN settings: Neither user *** nor current process …

Android:No permission to write APN settings(沒有写入 APN 设置的权限)

如果你想读Android 4.2以及以上版本的APN&#xff0c;我觉得你改变一下方法即可。我试了&#xff0c;而且可以实现。在Android 4.1 以及以下版本 &#xff1a;Cursor c getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"…