- Published on
Easy Way to Remove Inline Styles with JavaScript
- Authors

- Name
- Mehdi Akiki
Reference
Sometimes, third-party libraries add inline styles to elements that are difficult to override using standard CSS. One simple approach is to remove these inline styles entirely using JavaScript. Here's how:
// Replace '.container' with the target element's selector
const container = document.querySelector(".container");
const elements = container.querySelectorAll("*");
elements.forEach((element) => {
element.removeAttribute("style");
});
This code removes all style attributes from the targeted container's child elements, helping avoid CSS specificity issues without resorting to !important.
Why This Method Works
By removing inline styles directly from the DOM elements, you regain control over the styles using your external or internal CSS files. This is particularly useful when dealing with third-party libraries or frameworks that may add unwanted inline styles.
Benefits
- No More
!important: You avoid using !important to fight CSS specificity wars. - Cleaner Styles: External or internal CSS rules are applied as intended.
- Flexibility: You can use this method to target specific elements or containers selectively.
This quick fix can simplify handling of tricky third-party styles, giving you cleaner and more maintainable code.