如何使 css 属性无效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5218679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I nullify css property?
提问by Bluemagica
Basically I have two external css in my page.
基本上我的页面中有两个外部 css。
The first Main.css
contains all style rules but I don't have access to it, and hence I cannot modify it. I have access to a second file Template.css
, so I need to override the Main.css
's values in template.css
.
第一个Main.css
包含所有样式规则,但我无权访问它,因此我无法修改它。我有机会获得第二个文件Template.css
,所以我需要重写Main.css
中的价值观template.css
。
This is easy for which I have to change the value, but how do I remove a property entirely?
这很容易,我必须更改值,但是如何完全删除属性?
Like say a class .c1
has height: 40px;
, how do I get rid of this height property?
就像说一个类.c1
有height: 40px;
,我如何摆脱这个高度属性?
回答by thirtydot
You have to reset each individual property back to its default value. It's not great, but it's the only way, given the information you've given us.
您必须将每个单独的属性重置回其默认值。这不是很好,但鉴于您提供给我们的信息,这是唯一的方法。
In your example, you would do:
在您的示例中,您将执行以下操作:
.c1 {
height: auto;
}
You should search for each property here:
您应该在此处搜索每个属性:
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
For example, height
:
对于例如height
:
Initial value :
auto
初始值 :
auto
Another example, max-height
:
另一个例子,max-height
:
Initial value :
none
初始值 :
none
In 2017, there is now another way, the unset
keyword:
2017年,现在还有另外一种方式,unset
关键字:
.c1 {
height: unset;
}
Some documentation: https://developer.mozilla.org/en-US/docs/Web/CSS/unset
一些文档:https: //developer.mozilla.org/en-US/docs/Web/CSS/unset
The unset CSS keyword is the combination of the initial and inherit keywords. Like these two other CSS-wide keywords, it can be applied to any CSS property, including the CSS shorthand all. This keyword resets the property to its inherited value if it inherits from its parent or to its initial value if not. In other words, it behaves like the inherit keyword in the first case and like the initial keyword in the second case.
unset CSS 关键字是initial 和inherit 关键字的组合。像这两个 CSS 范围内的其他关键字一样,它可以应用于任何 CSS 属性,包括所有的 CSS 速记。如果属性从其父级继承,则此关键字将属性重置为其继承的值,否则重置为其初始值。换句话说,它的行为类似于第一种情况下的 inherit 关键字,第二种情况下的行为类似于 initial 关键字。
Browser support is good: http://caniuse.com/css-unset-value
浏览器支持很好:http: //caniuse.com/css-unset-value
回答by simhumileco
.c1 {
height: unset;
}
The unset
value added in CSS3also solves this problem and it's even more universal method than auto
or initial
because it sets to every CSS property its default value and additionally its default behawior relative to its parent.
CSS3 中unset
添加的值也解决了这个问题,它比或因为它为每个 CSS 属性设置其默认值以及相对于其父级的默认行为甚至更通用。auto
initial
Note that initial
value breaks aforementioned behavior.
请注意,initial
value 会破坏上述行为。
From MDN:
来自MDN:
Like these two other CSS-wide keywords, it can be applied to any CSS property, including the CSS shorthand all. This keyword resets the property to its inherited value if it inherits from its parent or to its initial value if not.
像这两个 CSS 范围内的其他关键字一样,它可以应用于任何 CSS 属性,包括 CSS 简写all。如果属性从其父级继承,则此关键字将属性重置为其继承的值,否则重置为其初始值。
回答by Pekka
like say a class .c1 has height:40px; how do I get rid of this height property?
就像说一个类 .c1 有 height:40px; 我如何摆脱这个高度属性?
Sadly, you can't. CSS doesn't have a "default" placeholder.
可悲的是,你不能。CSS 没有“默认”占位符。
In that case, you would reset the property using
在这种情况下,您将使用
height: auto;
as @Ben correctly points out, in some cases, inherit
is the correct way to go, for example when resetting the text colour of an a
element (that property is inherited from the parent element):
正如@Ben 正确指出的那样,在某些情况下,这inherit
是正确的方法,例如在重置a
元素的文本颜色时(该属性是从父元素继承的):
a { color: inherit }
回答by RonyK
回答by Richard H
To get rid of the fixed height property you can set it to the default value:
要摆脱固定高度属性,您可以将其设置为默认值:
height: auto;
回答by Jon
You need to provide a selector with higher specificitythan the one in Main.css
. With that selector, set the values of the properties you want to their default, e.g.
您需要提供一个选择具有高特异性一个比Main.css
。使用该选择器,将您想要的属性值设置为默认值,例如
body .c1 {
height: auto;
}
There is no "default" value that will work for all properties, you need to look up what the default is for each one and use that.
没有适用于所有属性的“默认”值,您需要查找每个属性的默认值并使用它。
回答by Nathalia Xavier
I had an issue that even when I did overwrite "height" to "unset" or "initial", it behaved differently from when I removed the previous setting.
我有一个问题,即使我确实将“高度”覆盖为“未设置”或“初始”,它的行为也与我删除以前的设置时有所不同。
It turned out I needed to remove the min-height property too!
结果我也需要删除 min-height 属性!
height: unset;
min-height: none
Edit: I tested on IE 7 and it doesn't recognize "unset", so "auto" works better".
编辑:我在 IE 7 上进行了测试,它无法识别“未设置”,因此“自动”效果更好”。