How do I unset an element's CSS attribute using jQuery?

JqueryCss

Jquery Problem Overview


If I set a CSS value on a specific element using:

$('#element').css('background-color', '#ccc');

I want to be able to unset that element-specific value and use the cascaded value, along the lines of:

$('#element').css('background-color', null);

But that syntax doesn't seem to work – is this possible using another syntax?

Edit: The value isn't inherited from the parent element – the original values comes from an element-level selector. Sorry for any confusion!

Jquery Solutions


Solution 1 - Jquery

From the jQuery docs:

> Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or <style> element.

Solution 2 - Jquery

I think you can also do:

$('#element').css('background-color', '');

That's what I used to do a long time ago for the display property in plain-old-javascript to show/hide a field.

Solution 3 - Jquery

Actually, the syntax I thought was incorrect (with null) seems to be working -- my selector was just improperly formed, and thus yielding no elements. D'oh!

Solution 4 - Jquery

Try this:

$('#element').css('background-color', 'inherit');

Solution 5 - Jquery

For what it's worth this appears not to work in IE 8 for 'filter,' I had to use this (in fadeIn callback):

$(this).attr('style', $(this).attr('style').replace(/\bfilter:\s*;*/, ''));

Obviously, I had need to remove an empty inline filter declaration so the CSS could take effect; there were other inline rules so I couldn't just remove the style attribute.

Solution 6 - Jquery

If it can help, I add a problem with double quote :

$('#element').css("border-color", "");

does not work while

$('#element').css('border-color', '');

works

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
QuestioncdlearyView Question on Stackoverflow
Solution 1 - JqueryGlenn MossView Answer on Stackoverflow
Solution 2 - JqueryFryGuyView Answer on Stackoverflow
Solution 3 - JquerycdlearyView Answer on Stackoverflow
Solution 4 - JqueryHectorMacView Answer on Stackoverflow
Solution 5 - JqueryBrandon HillView Answer on Stackoverflow
Solution 6 - JquerymerlintintinView Answer on Stackoverflow