How can I nullify css property?

Css

Css Problem Overview


Basically I have two external css in my page.

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.

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?

Css Solutions


Solution 1 - Css

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

Solution 2 - Css

.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.

Solution 3 - Css

> 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 }

Solution 4 - Css

An initial keyword is being added in CSS3 to allow authors to explicitly specify this initial value.

Solution 5 - Css

To get rid of the fixed height property you can set it to the default value:

height: auto;

Solution 6 - Css

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.

Solution 7 - Css

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".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBluemagicaView Question on Stackoverflow
Solution 1 - CssthirtydotView Answer on Stackoverflow
Solution 2 - CsssimhumilecoView Answer on Stackoverflow
Solution 3 - CssPekkaView Answer on Stackoverflow
Solution 4 - CssRonyKView Answer on Stackoverflow
Solution 5 - CssRichard HView Answer on Stackoverflow
Solution 6 - CssJonView Answer on Stackoverflow
Solution 7 - CssNathalia XavierView Answer on Stackoverflow