How can I nullify css property?
Basically I have two external css in my page.
The first Main.css
contains al开发者_如何学编程l 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
.
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?
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
For example, height
:
Initial value :
auto
Another example, max-height
:
Initial value :
none
In 2017, there is now another way, the unset
keyword:
.c1 {
height: unset;
}
Some documentation: 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.
Browser support is good: http://caniuse.com/css-unset-value
.c1 {
height: unset;
}
The unset
value added in CSS3 also 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.
Note that initial
value breaks aforementioned behavior.
From 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.
like say a class .c1 has height:40px; how do I get rid of this height property?
Sadly, you can't. CSS doesn't have a "default" placeholder.
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):
a { color: inherit }
An initial
keyword is being added in CSS3 to allow authors to explicitly specify this initial value.
To get rid of the fixed height property you can set it to the default value:
height: auto;
You need to provide a selector with higher specificity than the one in Main.css
. With that selector, set the values of the properties you want to their default, e.g.
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.
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!
height: unset;
min-height: none
Edit: I tested on IE 7 and it doesn't recognize "unset", so "auto" works better".
精彩评论