Categories
JavaScript Answers

How to Remove a CSS Property Using JavaScript?

Spread the love

Sometimes, we may want to remove a CSS property from an HTML element using JavaScript.

In this article, we’ll look at how to remove a CSS property using JavaScript.

Use the style.removeProperty Method

We can use the removeProperty method to remove a CSS property from an element.

For instance, if we have the following HTML:

<div style='background-color: green'>  
  hello  
</div>

We can remove the background-color property by writing:

const el = document.querySelector('div')  
el.style.removeProperty('background-color');

Setting a CSS Property to an Empty String

Another way to remove a CSS property from an HTML element is to set its value to an empty string.

For instance, we can write:

const el = document.querySelector('div')  
el.style.backgroundColor = ''

to remove the value of the CSS background-color property.

Conclusion

We can remove CSS properties from an object by setting to an empty string or calling the removeProperty method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *