Is there a way to remove all CSS styles from a specific selector using Javascript, without removing the selector itself from the element?
The issue is that I’d like to add my own class to an element to handle the styling, but the element is created by a WordPress plugin and there are already classes on the element that overwrite mine. I cannot remove the original classes because they’re also being used for Javascript.
Normally I’d give up and just stick !important
on everything in my class, which I can still do if there’s no other way, but the original classes have practically EVERYTHING defined, in multiple pseudo states, so I’d have to list out everything from color:
to border-bottom-right-radius:
with !important
after it (including on :focus
and :hover
and everything) in order to overwrite all the original styles.
Basically I have this:
<button class="original-class-1 original-class-2">Button</button>
<style>
.my-styles {
color: #fafafa;
background-color: #090909;
border: 1px solid;
}
.original-class-1 {
color: #000000;
background-color: #ffffff;
border-width: 1px;
border-style: solid;
...
...
etc-with-everything-but-the-kitchen-sink: killme;
}
.original-class-2 {
padding-top: 5px;
padding-right: 5px;
padding-bottom: 5px;
padding-left: 5px;
...
...
etc-with-even-more-stuff: why;
}
</style>
and I want
<button class="original-class-1 original-class-2 my-styles">Button</button>
<style>
.my-styles {
color: #fafafa;
background-color: #090909;
border: 1px solid;
}
.original-class-1 {
}
.original-class-2 {
}
</style>
At the moment I have something like this started:
const buttons = document.querySelectorAll('.original-class-1');
for (const button of buttons ) {
button.style.removeAllProperties(); // Does something like this exist?
button.classList.add('my-styles');
}
To be clear, the original classes are adding styles from an external stylesheet, I’ve only put them in <style>
tags above for illustration purposes.
4
Answers
The button elements in the example provided are styled using classes, not the element’s "style" attribute. Since the button elements are styled using classes, you could try modifying the "class" attribute on the buttons.
As you can see here, the button element now has your .my-styles and does not have either of the original classes.
I’m unfamiliar with WordPress, this may be better approached through their tooling rather than manipulating your DOM in this way. Best of luck.
ref: Element.setAttribute()
EDIT:
If you want to get really hacky, you could always do something interesting like the following: ( I don’t recommend you do this 🙂 )
rewriting the stylesheet with js
This is an unconventional approach that may have unforeseen effects but it uses javascript to re-write the relevant style sheet leaving empty rule sets for all selectors except those you have added (which must be identifiable by containing some constant label as part of their names, a
.my-
prefix in this example).Style sheets are html elements like any other and can be accessed, and have their content manipulated, using javascript.
You will have to do some exploratory work by (reporting to console) each of the elements in the collection of style elements extracted in order to focus on the relevant sheet(s). Here, the collection relevant to the snippet tools style panel was easily found to be index [0], hence its use in the example.
If the steps are not clear, leave a comment and I’ll explain each part’s purpose.
You would uncomment the assignment back to the stylesheet in the last line when done.
Using Window.getComputedStyle() which returns a CSSStyleDeclaration Object, you could simply iterate the key/value pairs setting all to "initial".
You could try experimenting with
all: revert
, orall: initial
in CSS to reset styles from within the CSS rules of a custom class, before presenting actual rules for the class.This snippet experiments with
revert
rather thaninitial
(go with what works), using exaggerated styles to make changes obvious.Pseudo element selectors didn’t appear to respond to wild cards after the colon, say to specify rules for a mythical "
.my-style:*
" selector, hence the explicit duplication of.my-style
rules for the.my-style:hover
selector.