首页 > 其他分享 >css44 CSS The !important Rule

css44 CSS The !important Rule

时间:2024-06-01 17:54:44浏览次数:25  
标签:color button some Rule important background css44 CSS

https://www.w3schools.com/css/css_important.asp

 

What is !important?

The !important rule in CSS is used to add more importance to a property/value than normal.

In fact, if you use the !important rule, it will override ALL previous styling rules for that specific property on that element!

Let us look at an example:

Example

#myid {
  background-color: blue;
}

.myclass {
  background-color: gray;
}

p {
  background-color: red !important;
}
<!DOCTYPE html>
<html>
<head>
<style>
#myid {
  background-color: blue;
}

.myclass {
  background-color: gray;
}

p {
  background-color: red !important;
}
</style>
</head>
<body>

<p>This is some text in a paragraph.</p>

<p class="myclass">This is some text in a paragraph.</p>

<p id="myid">This is some text in a paragraph.</p>

</body>
</html>

 

Example Explained

In the example above. all three paragraphs will get a red background color, even though the ID selector and the class selector have a higher specificity. The !important rule overrides the background-color property in both cases.


Important About !important

The only way to override an !important rule is to include another !important rule on a declaration with the same (or higher) specificity in the source code - and here the problem starts! This makes the CSS code confusing and the debugging will be hard, especially if you have a large style sheet!

Here we have created a simple example. It is not very clear, when you look at the CSS source code, which color is considered most important:

Example

#myid {
  background-color: blue !important;
}

.myclass {
  background-color: gray !important;
}

p {
  background-color: red !important;
}
<!DOCTYPE html>
<html>
<head>
<style>
#myid {
  background-color: blue !important;
}

.myclass {
  background-color: gray !important;
}

p {
  background-color: red !important;
}
</style>
</head>
<body>

<p>This is some text in a paragraph.</p>

<p class="myclass">This is some text in a paragraph.</p>

<p id="myid">This is some text in a paragraph.</p>

</body>
</html>

 

Tip: It is good to know about the !important rule. You might see it in some CSS source code. However, do not use it unless you absolutely have to.



Maybe One or Two Fair Uses of !important

One way to use !important is if you have to override a style that cannot be overridden in any other way. This could be if you are working on a Content Management System (CMS) and cannot edit the CSS code. Then you can set some custom styles to override some of the CMS styles.

Another way to use !important is: Assume you want a special look for all buttons on a page. Here, buttons are styled with a gray background color, white text, and some padding and border:

Example

.button {
  background-color: #8c8c8c;
  color: white;
  padding: 5px;
  border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #8c8c8c; 
  color: white;
  padding: 5px;
  border: 1px solid black; 
}
</style>
</head>
<body>

<p>Standard button: <a class="button" href="default.asp">CSS Tutorial</a></p>

<p>Standard button: <a class="button" href="/html/">HTML Tutorial</a></p>

</body>
</html>

 

The look of a button can sometimes change if we put it inside another element with higher specificity, and the properties get in conflict. Here is an example of this:

Example

.button {
  background-color: #8c8c8c;
  color: white;
  padding: 5px;
  border: 1px solid black;
}

#myDiv a {
  color: red;
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #8c8c8c; 
  color: white;
  padding: 5px;
  border: 1px solid black; 
}

#myDiv a {
  color: red;
  background-color: yellow;  
}
</style>
</head>
<body>

<p>Standard button: <a class="button" href="default.asp">CSS Tutorial</a></p>

<div id="myDiv">
<p>A link text inside myDiv: <a href="/html/">HTML Tutorial</a></p>
<p>A link button inside myDiv: <a href="/html/" class="button">HTML Tutorial</a></p>
</div>

</body>
</html>

 

 

To "force" all buttons to have the same look, no matter what, we can add the !important rule to the properties of the button, like this:

Example

.button {
  background-color: #8c8c8c !important;
  color: white !important;
  padding: 5px !important;
  border: 1px solid black !important;
}

#myDiv a {
  color: red;
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #8c8c8c !important; 
  color: white !important;
  padding: 5px !important;
  border: 1px solid black !important; 
}

#myDiv a {
  color: red;
  background-color: yellow;  
}
</style>
</head>
<body>

<p>Standard button: <a class="button" href="default.asp">CSS Tutorial</a></p>

<div id="myDiv">
<p>A link text inside myDiv: <a href="/html/">HTML Tutorial</a></p>
<p>A link button inside myDiv: <a href="/html/" class="button">HTML Tutorial</a></p>
</div>

</body>
</html>

 

 

标签:color,button,some,Rule,important,background,css44,CSS
From: https://www.cnblogs.com/emanlee/p/18226223

相关文章

  • at-rule or selector expectedcss(css-ruleorselectorexpected)
    问题如图所示,在使用scss和less是使用//注释就会出现红色报错,这个不影响运行,但是看着很不舒服。解决没有找到什么好的解决办法,一般的解决方法是,把验证关闭,然后重启。但是后面我开启这个设置,再进行注释,没有报错,感觉还是重启的作用。先放着不管吧。......
  • Three Locks To Rule Them All(三把锁统治一切)
    ThreeLocksToRuleThemAll(三把锁统治一切)【英文原文】为了确保线程安全,特别是在服务器端,我们通常使用临界区(criticalsections)或锁(locks)来保护代码。在最近的Delphi版本中,我们引入了TMonitor特性,但我更倾向于信任操作系统提供的锁机制,这些锁是通过Windows临界区或POSIXfute......
  • hidet使用rule based调度
    定义computation整体流程类似于tvm的计算描述定义输入、输出tensor,指定名称、数据类型和shapea=tensor_input('a',dtype='float32',shape=[10])b=tensor_input('b',dtype='float32',shape=[])b=tensor_input('data',dtype='float16&......
  • vue给元素添加校验rules
    1.使用validator添加校验规则:在Elemengplus(Vue3版本的ElementPlus)框架中,给el-dialog中的input框添加日期格式验证,可以使用el-form和el-form-item组件来配合实现,并通过el-input组件的v-model绑定数据,结合el-form的验证规则rules实现。以下是一个简单的例子,演示如何给日期输入框......
  • WPF ValidatesOnDataErrors IDataErrorInfo ValidationRule
    //xaml<Windowx:Class="WpfApp91.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • Vue中form表单常用rules校验规则
    是否合法IP地址constcheckIPCode=(rule,value,callback)=>{ if(/^(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/ .test(value......
  • 实时动态规则(55)规则发布平台后端开发(5) 规则模型开发(4)rulemodel_03_涉及事件时间
    0涉及架构 注意:以下代码,都是根据一个特定规则模型: rulemodel_03_caculator 来进行开发的不同的规则模型,如下功能代码需要进行不同的开发RuleModel_03 这个规则模型的特点是:拥有事件间隔时间1规则参数结构规范{"ruleModelId":"3","ruleId":"m3-r01",......
  • 为什么自动驾驶领域发论文都是用强化学习算法,但是实际公司里却没有一家使用强化学习算
    为什么自动驾驶领域发论文都是用强化学习算法,但是实际公司里却没有一家使用强化学习算法?——(特斯拉今年年初宣布推出实际上第一款纯端到端的自动驾驶系统,全部使用强化算法,替换掉原有的30万行C++的rule-based代码)给出一个自己比较认可的答案:https://www.zhihu.com/question/54......
  • Rust所有权__Ownership Rules
    First,let’stakealookattheownershiprules.Keeptheserulesinmindaswethroughtheexamplesthatillustratethem:     EachvalueinRusthasanowner.     Therecanonlybeoneowneratatime.     Whentheownergoesoutofsc......
  • WDS+MDT网络启动自动部署windows(六)rules bootstrip.ini
    简介这就是自动化的关键一步。自动化规则,启动脚本。关键是设置共享文件夹属性中的Rules标签。设置rulesbootstrap.ini官方示例分步-使用MDT在测试实验室中部署Windows10-Windows10|Microsoft学习fully-automated-lti-deployment-for-a-refresh-computer-scenari......