CSS
Web Development
JavaScript
Programming
jQuery

How can I remove a style added with .css() function?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When jQuery's .css() sets a style, it usually writes an inline style onto the element. To remove that style later, the usual answer is to set that property back to an empty string or to remove the whole style attribute if you truly want to clear everything.

The best choice depends on whether you want to remove one property or all inline styles. In many cases, the cleaner long-term answer is to stop toggling inline styles and use classes instead.

Remove One Inline Property

If you added one property with .css(), remove just that property by assigning an empty string:

javascript
1$("#box").css("color", "red");
2
3// Later:
4$("#box").css("color", "");

That removes the inline color value and lets the browser fall back to whatever stylesheet rule or default value applies next.

This is usually the safest answer when only one property needs to be reverted.

Remove All Inline Styles

If you want to wipe out every inline style on the element, remove the whole attribute:

javascript
$("#box").removeAttr("style");

Be careful with this approach. It removes everything in the inline style attribute, not just the part that one script added.

If another part of the page also wrote an inline width, transform, or position, those disappear too.

Native DOM Style Removal

The same idea exists in plain DOM APIs:

javascript
1const box = document.getElementById("box");
2box.style.color = "red";
3
4// Later:
5box.style.removeProperty("color");

That is useful to understand because jQuery is essentially wrapping the same inline-style mechanics underneath.

Why Classes Are Often Better

Inline styles are fine for quick one-off changes, but they become hard to manage when the same visual state is toggled in several places. Classes scale better:

css
1.highlighted {
2  color: red;
3  background: #fff4c2;
4}
javascript
1$("#box").addClass("highlighted");
2
3// Later:
4$("#box").removeClass("highlighted");

This is usually easier to maintain because:

  • the styling stays in CSS
  • JavaScript only manages state
  • removing the state is explicit and reversible

The Cascade Still Matters

Removing an inline style does not mean "remove the look entirely." It means "stop forcing this inline value." After that, normal CSS cascade rules apply again.

Example:

css
#box {
  color: blue;
}
javascript
$("#box").css("color", "red");
$("#box").css("color", "");

After the second line, the text becomes blue again, not unstyled, because the stylesheet rule is still there.

When !important Enters the Picture

Inline styles are already high-specificity. If your stylesheet uses !important, the actual result can be less obvious. Likewise, if you injected an inline style with !important through a different API, simply clearing one property may not behave the way you expect.

So if the visual result seems wrong after removal, inspect the element in the browser devtools and look at the computed styles, not just the HTML.

Common Pitfalls

The biggest pitfall is using removeAttr("style") when you only meant to remove one property. That can accidentally wipe out unrelated inline styles.

Another common problem is forgetting that removing an inline style reveals the next matching rule in the cascade. The element may still look styled because external CSS is still active.

Teams also overuse .css() for state changes that really belong in classes. That makes style logic harder to reason about over time.

Finally, do not assume jQuery is the source of every inline style. Other scripts, animations, or libraries may be writing to the same style attribute.

Summary

  • Remove one .css()-added property with .css("property", "").
  • Remove all inline styles with .removeAttr("style"), but use that carefully.
  • Inline-style removal reveals the next applicable rule in the CSS cascade.
  • For repeatable UI states, prefer adding and removing classes instead of inline styles.
  • Use browser devtools when the visible result does not match what you expected to remove.

Course illustration
Course illustration

All Rights Reserved.