9

When appending the .css() function in jQuery 1.4.4 will the .css() override each of the preceding css, or will it append it?

e.g.

$('.GhostIcon').css('opacity', '0.25').css('filter', 'alpha(opacity=25)').css('-khtml-opacity', '0.25').css('-moz-opacity', '0.25');

And secondly, does this have the same behaviour?

$('.GhostIcon').css('opacity', '0.25');
$('.GhostIcon').css('filter', 'alpha(opacity=25)');
$('.GhostIcon').css('-khtml-opacity', '0.25');
$('.GhostIcon').css('-moz-opacity', '0.25');
2
  • 1
    You can chain your function calls, but there's a better shortcut for css, use an object: $('.GhostIcon').css({'opacity':'0.25', 'filter':'alpha(opacity=25)',...}); Commented Dec 6, 2010 at 6:14
  • you may also want to consider adding and removing classes rather than putting css directly on the element, as it will be easier to manage in the future, for example, if you decide that the opacity level needs to change Commented Dec 2, 2013 at 13:43

3 Answers 3

12

Setting a CSS property on an element will not remove all the other properties it might already have. So yes, it's cumulative. Of course setting a property that's already set will change the value, since you can't have the same property twice.

Edit: Also, you only need to set opacity. jQuery will take care of setting things for lesser browsers.

Sign up to request clarification or add additional context in comments.

1 Comment

"Setting a CSS property on an element will not remove all the other properties" - This is not necessarily true, if CSS is set in another framework, such as angular, it will be overwritten.
2

These styles are applied so that it will work cross browser way. Also you can append the CSS in a single go

$('.GhostIcon').css({'opacity': '0.25', 'filter': 'alpha(opacity=25)', '-khtml-opacity': '0.25', '-moz-opacity': '0.25'}); 

Comments

1

@JD , if it same value , it will definetly overwrite , if not there it will appened

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.