951

I'm changing CSS with jQuery and I wish to remove the styling I'm adding based on the input value:

if(color != '000000') $("body").css("background-color", color); else // remove style ?

How can I do this?
Note that the line above runs whenever a color is selected using a color picker (ie. when the mouse moves over a color wheel).

2nd note: I can't do this with css("background-color", "none") because it will remove the default styling from the CSS files.
I just want to remove the background-color inline style added by jQuery.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
Alex
  • 66,732
  • 177
  • 439
  • 641
  • 1
    Possible duplicate of [How do I unset an element's CSS attribute using jQuery?](http://stackoverflow.com/questions/490910/how-do-i-unset-an-elements-css-attribute-using-jquery) – Chris Moschini Mar 29 '16 at 23:28
  • better try addClass / removeClass based on your fcuntionality. This wont affect other styling. – Prabhakaran Jul 26 '17 at 13:12
  • 2 ways to remove style in jquery with demo https://codepedia.info/jquery-remove-inline-style – Satinder singh May 11 '21 at 13:22

21 Answers21

1429

Changing the property to an empty string appears to do the job:

$.css("background-color", "");
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 35
    I have to side with @baacke here. Our customer still has an official minimum requirement of IE6(!!). Problem is, their customers are all over the world, including less developed countries. The "customer's customers" may be using *very* old computers to do business with our customer. So old browser must be at least minimally supported or they risk losing business... – user1429080 Jun 27 '14 at 09:59
  • 11
    You guys don't get to set the requirements of any solution you develop for a client. If that client wants or needs support for an older browser, it;s your job to provide it, period. You guys talking about "who cares" give true engineers a bad name. – Ed DeGagne Dec 29 '14 at 14:49
584

The accepted answer works but leaves an empty style attribute on the DOM in my tests. No big deal, but this removes it all:

removeAttr( 'style' );

This assumes you want to remove all dynamic styling and return back to the stylesheet styling.

Community
  • 1
  • 1
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
183

There are several ways to remove a CSS property using jQuery:

1. Setting the CSS property to its default (initial) value

.css("background-color", "transparent")

See the initial value for the CSS property at MDN. Here the default value is transparent. You can also use inherit for several CSS properties to inherite the attribute from its parent. In CSS3/CSS4, you may also use initial, revert or unset but these keywords may have limited browser support.

2. Removing the CSS property

An empty string removes the CSS property, i.e.

.css("background-color","")

But beware, as specified in jQuery .css() documentation, this removes the property but it has compatibilty issues with IE8 for certain CSS shorthand properties, including background.

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 element. Warning: one notable exception is that, for IE 8 and below, removing a shorthand property such as border or background will remove that style entirely from the element, regardless of what is set in a stylesheet or element.

3. Removing the whole style of the element

.removeAttr("style")
KrisWebDev
  • 9,342
  • 4
  • 39
  • 59
55

I got the way to remove a style attribute with pure JavaScript just to let you know the way of pure JavaScript

var bodyStyle = document.body.style;
if (bodyStyle.removeAttribute)
    bodyStyle.removeAttribute('background-color');
else        
    bodyStyle.removeProperty('background-color');
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Marwan
  • 2,362
  • 1
  • 20
  • 35
  • 36
    because jquery isn't every thing , because she or perhaps i should say every one of us as developers should know what compatibility means and how to do such a stuff with core languages as JavaScript, perhaps this is my theory i created already my own framework from my knowledge of core JavaScript i refuses to use others things i have my own concept and my way of thinking :) and by the way thanks for your ( - ) i just needed to be value add :) and also by the way the guy isnt a guy she is a she named Alex – Marwan Jun 13 '12 at 14:22
26

This will remove complete tag :

  $("body").removeAttr("style");
greuze
  • 4,250
  • 5
  • 43
  • 62
19

either of these jQuery functions should work:

$("#element").removeAttr("style");
$("#element").removeAttr("background-color") 
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
Mahesh Gaikwad
  • 587
  • 1
  • 5
  • 11
  • 24
    To remove only one css property: `var cssObject = $('selector').prop('style'); cssObject.removeProperty('background-color');` – ilgaar Oct 06 '13 at 19:51
11

Just using:

$('.tag-class').removeAttr('style');

or

$('#tag-id').removeAttr('style');
greuze
  • 4,250
  • 5
  • 43
  • 62
Alberto Cerqueira
  • 1,339
  • 14
  • 18
10

2018

there is native API for that

element.style.removeProperty(propery)
pery mimon
  • 7,713
  • 6
  • 52
  • 57
7

How about something like:

var myCss = $(element).attr('css');
myCss = myCss.replace('background-color: '+$(element).css('background-color')+';', '');
if(myCss == '') {
  $(element).removeAttr('css');
} else {
  $(element).attr('css', myCss);
}
nclu
  • 1,057
  • 1
  • 8
  • 19
7

If you use CSS style, you can use:

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

and then replace with:

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

If you don't use CSS style and you have attribute in HTML element, you can use:

$("#element").attr("style.background-color",color);
toro2k
  • 19,020
  • 7
  • 64
  • 71
Luis
  • 71
  • 1
  • 1
7

Use my Plugin :

$.fn.removeCss=function(all){
        if(all===true){
            $(this).removeAttr('class');
        }
        return $(this).removeAttr('style')
    }

For your case ,Use it as following :

$(<mySelector>).removeCss();

or

$(<mySelector>).removeCss(false);

if you want to remove also CSS defined in its classes :

$(<mySelector>).removeCss(true);
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
7

Try this:

$('#divID').css({"background":"none"});// remove existing

$('#divID').css({"background":"#bada55"});// add new color here.

Thanks

VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
uihelp
  • 198
  • 2
  • 7
4

This one also work!!

$elem.attr('style','');
dazzafact
  • 2,570
  • 3
  • 30
  • 49
3

Why not make the style you wish to remove a CSS class? Now you can use: .removeClass(). This also opens up the possibility of using: .toggleClass()

(remove the class if it's present, and add it if it's not.)

Adding / removing a class is also less confusing to change / troubleshoot when dealing with layout issues (as opposed to trying to figure out why a particular style disappeared.)

Neil Girardi
  • 4,533
  • 1
  • 28
  • 45
2
let el = document.querySelector(element)
let styles = el.getAttribute('style')

el.setAttribute('style', styles.replace('width: 100%', ''))
Kamil Ismagilov
  • 576
  • 4
  • 11
2

you remove style using

removeAttr( 'style' );

Syntle
  • 5,168
  • 3
  • 13
  • 34
1

Simple is cheap in web development. I recommend using empty string when removing a particular style

$(element).style.attr = '  ';
Pang
  • 9,564
  • 146
  • 81
  • 122
Victor Michael
  • 223
  • 3
  • 7
1

This is more complex than some other solutions, but may offer more flexibility in scenarios:

1) Make a class definition to isolate (encapsulate) the styling you want to apply/remove selectively. It can be empty (and for this case, probably should be):

.myColor {}

2) use this code, based on http://jsfiddle.net/kdp5V/167/ from this answer by gilly3:

function changeCSSRule(styleSelector,property,value) {
    for (var ssIdx = 0; ssIdx < document.styleSheets.length; ssIdx++) {
        var ss = document.styleSheets[ssIdx];
        var rules = ss.cssRules || ss.rules;
        if(rules){
            for (var ruleIdx = 0; ruleIdx < rules.length; ruleIdx++) {
                var rule = rules[ruleIdx];
                if (rule.selectorText == styleSelector) {
                    if(typeof value == 'undefined' || !value){
                        rule.style.removeProperty(property);
                    } else {
                        rule.style.setProperty(property,value);
                    }
                    return; // stops at FIRST occurrence of this styleSelector
                }
            }
        }
    }
}

Usage example: http://jsfiddle.net/qvkwhtow/

Caveats:

  • Not extensively tested.
  • Can't include !important or other directives in the new value. Any such existing directives will be lost through this manipulation.
  • Only changes first found occurrence of a styleSelector. Doesn't add or remove entire styles, but this could be done with something more elaborate.
  • Any invalid/unusable values will be ignored or throw error.
  • In Chrome (at least), non-local (as in cross-site) CSS rules are not exposed through document.styleSheets object, so this won't work on them. One would have to add a local overrides and manipulate that, keeping in mind the "first found" behavior of this code.
  • document.styleSheets is not particularly friendly to manipulation in general, don't expect this to work for aggressive use.

Isolating the styling this way is what CSS is all about, even if manipulating it isn't. Manipulating CSS rules is NOT what jQuery is all about, jQuery manipulates DOM elements, and uses CSS selectors to do it.

Community
  • 1
  • 1
dkloke
  • 436
  • 5
  • 12
1

Try This

$(".ClassName").css('color','');
Or 
$("#Idname").css('color','');
Basant Rules
  • 785
  • 11
  • 8
0

You can use:

 $("#eslimi").removeAttr("style").hide();
Mojtaba Nava
  • 858
  • 7
  • 17
0

Try

document.body.style=''

$("body").css("background-color", 'red');

function clean() {
  document.body.style=''
}
body { background-color: yellow; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="clean()">Remove style</button>
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345