Question
I am changing CSS with jQuery and want to remove styling that I previously added based on the input value.
if (color !== '000000') {
$('body').css('background-color', color);
} else {
// How do I remove the style?
}
How can I do this?
This code runs whenever a color is selected using a color picker, such as while the mouse moves over a color wheel.
I cannot use:
$('body').css('background-color', 'none');
because that would not restore the original stylesheet behavior correctly. I only want to remove the inline background-color style that was added by jQuery, so the default CSS from the stylesheet applies again.
Short Answer
By the end of this page, you will understand the difference between inline styles and stylesheet rules, how jQuery's .css() method sets styles, and how to remove an inline style so the browser falls back to the original CSS rules.
Concept
When you use jQuery's .css() method, it usually writes a style directly into the element's style attribute.
For example:
$('body').css('background-color', '#ff0000');
produces inline CSS similar to:
<body style="background-color: #ff0000;">
Inline styles have high priority, so they override many stylesheet rules.
If you later want the original CSS file's value to apply again, you should remove the inline style property, not replace it with another value like none or transparent unless that is the visual effect you actually want.
In jQuery, the common way to remove an inline CSS property is:
$('body').css('background-color', '');
Setting the value to an empty string removes that specific inline style property. Then the browser recalculates the style and uses whatever rule comes from your CSS files or browser defaults.
This matters because real applications often temporarily apply styles in JavaScript for previews, validation states, animations, or theme changes. If you do not remove those styles correctly, your app can get stuck showing temporary styles that should no longer be active.
Mental Model
Think of an element's final appearance like layers of instructions:
- Your CSS file is the normal instruction manual.
- An inline style is a sticky note placed directly on the element.
- The browser usually follows the sticky note first.
Using jQuery .css() adds or changes that sticky note.
If you want to go back to the normal instruction manual, you do not write a new sticky note saying none. You remove the sticky note for that property.
That is why this works:
$('body').css('background-color', '');
You are not forcing a new color. You are removing the override.
Syntax and Examples
The basic syntax is:
$(selector).css(propertyName, value);
Set a style
$('body').css('background-color', '#00ff00');
This adds an inline background-color style.
Remove a style
$('body').css('background-color', '');
This removes the inline background-color property so stylesheet rules can apply again.
Example based on your case
if (color !== '000000') {
$('body').css('background-color', '#' + color);
} else {
$('body').css('background-color', '');
}
Why this works
Step by Step Execution
Consider this example:
let color = 'ff0000';
if (color !== '000000') {
$('body').css('background-color', '#' + color);
} else {
$('body').css('background-color', '');
}
Step by step
-
coloris'ff0000'. -
The condition
color !== '000000'is true. -
jQuery runs:
$('body').css('background-color', '#ff0000'); -
The browser adds an inline style to the
body. -
The page background becomes red.
Now imagine the picker later produces:
color = '000000';
Real World Use Cases
Removing inline styles correctly is useful in many common situations:
Live color preview
A user moves over a color picker and the page previews the selected color. If they cancel or choose a reset value, the temporary inline style should be removed.
Form validation
JavaScript may add:
$('input').css('border-color', 'red');
When the field becomes valid, you can remove the inline border color so the normal form theme returns.
Temporary highlights
An app may briefly highlight a changed row in a table. After the effect ends, removing the inline style restores the standard row styling.
Theme previews
A settings page may preview a theme without saving it. Removing the inline overrides returns the page to the stored stylesheet-based theme.
Drag-and-drop feedback
During dragging, JavaScript may apply temporary colors or outlines. When dragging ends, removing the inline style avoids leaving visual leftovers.
Real Codebase Usage
In real projects, developers often avoid leaving permanent inline styles unless the style is truly dynamic.
Common pattern: temporary override
function previewColor(color) {
if (color) {
$('body').css('background-color', color);
} else {
$('body').css('background-color', '');
}
}
This is a simple guard pattern:
- if there is a valid color, apply it
- otherwise, remove the override
Common pattern: reset to stylesheet defaults
function clearPreview() {
$('body').css('background-color', '');
}
This keeps the stylesheet as the source of truth.
Common pattern: classes instead of inline styles
In larger codebases, developers often prefer toggling classes:
$('body').addClass('preview-mode');
$('body').();
Common Mistakes
Mistake 1: Using 'none' for background-color
Broken code:
$('body').css('background-color', 'none');
Why it is a problem:
noneis not the right way to remove an inlinebackground-coloroverride.- It does not mean "go back to the stylesheet value".
Use this instead:
$('body').css('background-color', '');
Mistake 2: Using 'transparent' when you mean reset
$('body').css('background-color', 'transparent');
This sets a real value. It does not remove the inline style. The browser still sees an inline override.
Mistake 3: Forgetting the # in a hex color
Broken code:
Comparisons
| Approach | What it does | Good for | Limitation |
|---|---|---|---|
$(el).css('background-color', '#ff0000') | Sets an inline style | Dynamic temporary styling | Overrides stylesheet rules |
$(el).css('background-color', '') | Removes that inline property | Restoring stylesheet defaults | Only removes one property |
$(el).removeAttr('style') | Removes the full style attribute | Rare full reset cases | Can remove unrelated inline styles |
$(el).addClass('my-class') | Applies class-based styling | Maintainable project code | Requires CSS class definitions |
Cheat Sheet
// Set inline background color
$('body').css('background-color', '#ff0000');
// Remove inline background color
$('body').css('background-color', '');
// Risky: removes all inline styles
$('body').removeAttr('style');
Rules to remember
.css(property, value)sets an inline style..css(property, '')removes that inline property.- Removing an inline property allows stylesheet rules to apply again.
noneis not the same as removing a style.transparentis a real value, not a reset.- Use
removeAttr('style')only if you want to remove every inline style.
Useful pattern
if (color !== '000000') {
$('body').css('background-color', '#' + color);
} else {
$('body').(, );
}
FAQ
How do I remove a CSS style set with jQuery?
Use an empty string for that property:
$('body').css('background-color', '');
Does .css('property', '') remove the inline style or set it to empty?
In practice, it removes that inline property so normal stylesheet rules can take effect again.
Why not use none for background-color?
Because none does not mean "restore the stylesheet value" for this purpose. You want to remove the inline override, not assign a new value.
What is the difference between removing a style and setting it to transparent?
transparent is still an inline background-color value. Removing the style means there is no inline override at all.
Can I remove all inline styles from an element?
Yes:
$('body').removeAttr('style');
But this removes every inline style, not just one property.
Is using classes better than .css()?
Mini Project
Description
Build a small background color preview tool that lets a user preview a selected color on the page and reset back to the original stylesheet background. This demonstrates how to add an inline style with jQuery and then remove only that inline property without affecting other CSS rules.
Goal
Create a live color preview that applies a selected background color and restores the default CSS background when the reset value is chosen.
Requirements
- Create a page with a default
bodybackground color defined in CSS. - Add an input for a hex color value such as
ff0000. - Apply the chosen color to the page background using jQuery.
- If the user enters
000000or clears the field, remove the inlinebackground-colorstyle. - Do not remove other inline styles from the page.
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.