Question
How can I apply a CSS property with !important using jQuery? For example, this does not apply any inline width style:
$("#elem").css("width", "100px !important");
I have an external stylesheet rule that sets the width with !important, and I need to override it with a computed inline value. Is there a jQuery-style approach that does not require replacing and parsing the element's entire cssText value?
Short Answer
You will learn why jQuery.css() rejects values containing !important, how the CSS cascade decides which rule wins, and how to set an inline important declaration safely with style.setProperty().
Concept
!important is not part of a CSS property's value. It is a priority flag attached to a CSS declaration.
For example, CSS treats this as three separate parts:
width: 100px !important;
- Property name:
width - Value:
100px - Priority:
important
jQuery's .css() method is designed to set the property value:
$("#elem").css("width", "100px");
When given "100px !important", it passes an invalid value for width to the browser. The browser rejects it, so no inline declaration is added.
Use the browser's DOM style API when you need to set the priority:
document.querySelector("#elem").style.setProperty("width", "100px", "important");
This changes only the width declaration. It does not replace or parse style.cssText.
In the normal author stylesheet cascade, an inline !important declaration outranks an !important declaration from an external stylesheet. However, a running CSS transition can temporarily control the displayed value, and user-origin important styles can outrank author styles.
Mental Model
Think of a CSS declaration as a note attached to an element:
- The property is the label:
width. - The value is the instruction:
100px. !importantis a red high-priority stamp on that note.
.css() lets you write the instruction, but it does not provide a place to add the priority stamp. setProperty() accepts all three pieces separately, including the priority.
Syntax and Examples
Use setProperty(propertyName, value, priority) on the element's style object:
const element = document.querySelector("#elem");
element.style.setProperty("width", "100px", "important");
With a jQuery selection, get the first matching DOM element:
$("#elem")[0].style.setProperty("width", "100px", "important");
Or apply it to every matched element:
$(".panel").each(function () {
this.style.setProperty("width", "100px", "important");
});
The third argument must be "important", without the exclamation mark.
To remove only the inline width declaration later:
$("#elem")[0].style.removeProperty("width");
After removal, the stylesheet rule applies again.
Step by Step Execution
Consider this stylesheet and script:
#elem {
width: 250px !important;
}
const element = $("#elem")[0];
const calculatedWidth = 100;
element.style.setProperty("width", `${calculatedWidth}px`, "important");
Execution steps:
-
The stylesheet supplies
width: 250px !important. -
JavaScript calculates
calculatedWidthas100. -
The template literal creates the value
"100px". -
setProperty()adds this inline declaration:<div id="elem" style="width: 100px !important"></div> -
Both declarations are important author declarations.
-
The inline important declaration wins over the external stylesheet's important declaration, so the used width is
100px.
You can inspect the final cascade in browser DevTools under the element's Styles or Computed panel.
Real World Use Cases
- Resizable panels: Calculate a width from pointer movement and force it over a legacy stylesheet rule.
- Embedded widgets: Override CSS from a host page when a widget must maintain a required layout.
- Print or display modes: Apply a computed size or visibility rule when a mode is enabled.
- Compatibility code: Temporarily override older CSS that cannot yet be changed.
- UI libraries: Apply a short-lived style while integrating with third-party components that use overly specific or important rules.
Use !important sparingly. If you own the stylesheet, changing the selector structure or class-based state is usually easier to maintain.
Real Codebase Usage
In production code, avoid scattering direct important declarations throughout event handlers. Put the operation in a small helper so its purpose and cleanup are clear:
function setImportantStyle(element, property, value) {
element.style.setProperty(property, value, "important");
}
function clearInlineStyle(element, property) {
element.style.removeProperty(property);
}
const panel = document.querySelector("#panel");
const width = Math.max(240, window.innerWidth - 320);
setImportantStyle(panel, "width", `${width}px`);
For state that is not computed, prefer a class because CSS remains in the stylesheet:
.panel--expanded {
width: 100% !important;
}
$("#panel").addClass("panel--expanded");
For validation and error handling, ensure that computed values are valid CSS before applying them:
function setPanelWidth(element, pixels) {
if (!Number.isFinite(pixels) || pixels < 0) {
return;
}
element.style.setProperty("width", `${pixels}px`, "important");
}
When possible, fix the original CSS rule instead of escalating priority. Repeated !important usage makes later overrides harder.
Common Mistakes
Putting !important in the value
This is rejected because !important is not part of a width value:
// Incorrect
$("#elem").css("width", "100px !important");
Use setProperty() instead:
$("#elem")[0].style.setProperty("width", "100px", "important");
Including ! in the priority argument
The API expects "important", not "!important":
// Incorrect
element.style.setProperty("width", "100px", "!important");
// Correct
element.style.setProperty("width", "100px", "important");
Using camelCase property names with setProperty()
setProperty() expects CSS property names, usually kebab-case:
// Incorrect
element.style.setProperty("backgroundColor", "tomato", "important");
// Correct
element.style.setProperty("background-color", "tomato", "important");
Assuming every jQuery selection contains an element
$("#missing")[0] is undefined, which causes an error when accessing .style:
const element = $("#elem")[0];
if (element) {
element.style.setProperty("width", "100px", "important");
}
Replacing cssText
Avoid this unless you intentionally want to replace all inline styles:
// Risky: replaces every existing inline declaration
element.style.cssText = "width: 100px !important";
setProperty() changes one property without disturbing the others.
Forgetting other sources of visible styling
If the value still appears unchanged, check for:
- A CSS transition, which can override even important declarations while it runs.
- A different property affecting layout, such as
max-width,min-width, or flex/grid sizing. - A user stylesheet with an important declaration.
- JavaScript code that changes the style again afterward.
Comparisons
| Approach | Can set !important? | Best use |
|---|---|---|
$(element).css("width", "100px") | No | Ordinary inline styles |
element.style.width = "100px" | No | Ordinary inline styles without jQuery |
element.style.setProperty("width", "100px", "important") | Yes | A computed inline important declaration |
element.style.cssText = "..." | Yes | Replacing all inline styles intentionally; usually avoid for one property |
| Add/remove a CSS class | Yes, if the class rule uses it | Reusable, stylesheet-managed UI states |
setProperty() is the appropriate direct API when the value is computed at runtime and must receive important priority. A class is generally preferable when the possible values are known ahead of time.
Cheat Sheet
// Set a normal inline style
$("#elem").css("width", "100px");
// Set an inline important style
$("#elem")[0].style.setProperty("width", "100px", "important");
// Computed value
const width = 100;
$("#elem")[0].style.setProperty("width", `${width}px`, "important");
// Apply to all jQuery matches
$(".item").each(function () {
this.style.setProperty("display", "none", "important");
});
// Read the inline value and priority
const element = $("#elem")[0];
element.style.getPropertyValue("width"); // "100px"
element.style.getPropertyPriority("width"); // "important"
// Remove the inline declaration
$("#elem")[0].style.removeProperty("width");
Rules:
- Pass
"important"without!. - Use CSS names such as
background-color, notbackgroundColor. - Do not put
!importantinside the value passed to.css(). - Prefer classes or corrected stylesheet rules when the value does not need runtime calculation.
FAQ
Why does jQuery .css() not accept !important?
.css() sets a CSS value, while !important is declaration priority rather than part of that value. Browsers reject values such as "100px !important" for width.
How do I set !important with jQuery?
Get the underlying DOM element and call setProperty():
$("#elem")[0].style.setProperty("width", "100px", "important");
Does inline !important override an external stylesheet's !important rule?
Usually yes. Within the same author origin, an inline important declaration outranks an important declaration from an external stylesheet.
Can I use .width() instead of .css()?
No. .width() sets a normal inline width and cannot add important priority.
How do I remove an inline important style?
Remove the property entirely:
element.style.removeProperty("width");
The stylesheet can then control the width again.
Why is my important width still not visibly changing?
Inspect DevTools for a transition, min-width or max-width, flex or grid constraints, a later JavaScript update, or a higher-precedence stylesheet origin.
Should I use !important for normal UI state changes?
Usually not. For normal states, toggling a class is more maintainable. Use setProperty(..., "important") when a runtime-computed override is genuinely necessary.
Mini Project
Description
Build a small responsive sidebar controller. It calculates a sidebar width from the browser viewport and applies that width as an inline !important style so it can override a legacy stylesheet rule.
Goal
Update the sidebar width when the page loads and whenever the browser window is resized.
Requirements
Create a sidebar element with a legacy stylesheet width marked !important.
Calculate a sidebar width from window.innerWidth.
Keep the computed width between 220px and 420px.
Apply the computed width as an inline important declaration.
Update the width after a browser resize event.
Keep learning
Related questions
Add a Favicon to a Static HTML Page
Learn how to add a favicon to static HTML pages, use correct paths and icon formats, and troubleshoot browser caching and local-file issues.
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
Bootstrap 3 sr-only Class: Screen Reader Accessibility
Learn how Bootstrap 3's sr-only class adds screen-reader labels to icon-only controls without changing the visible page layout.