Home > Web Front-end > JS Tutorial > body text

How to remove CSS properties using JavaScript?

王林
Release: 2023-09-10 18:09:03
forward
1286 people have browsed it

如何使用 JavaScript 删除 CSS 属性?

Sometimes, we need to remove CSS properties from HTML elements after performing certain tasks. For example, the user has not paid the subscription fee for your app. Therefore, you will have the "Payment Due" message in "red" in the header section. After the user clears the payment, you can change the content of the header and remove the "color: red" attribute to set the initial color.

There are many use cases that require removing CSS properties from HTML elements. Here we will learn different ways to remove CSS properties.

Use removeProperty() method

The first method uses the removeProperty() method. It is used to remove any CSS attribute from an HTML element and takes the attribute name as parameter.

grammar

Users can use the removeProperty() method to remove CSS properties from HTML elements according to the following syntax.

ele.style.removeProperty("property-name");
Copy after login

In the above syntax, "ele" is an HTML element from which we need to remove the CSS attributes.

Example

In the example below, we have created a div element that contains different CSS properties such as "width", "height", "border" and "background color".

Every time the user clicks the button, we will execute the removeColor() function. In the removeColor() function, we access the div element using the class name. After that, we use the removeProeprty() method to remove the background color from the div element by passing "background-color" as parameter.

In the output, the user can click the button and observe that the background color will be removed.



    

Using the removeProperty() method to remove the CSS property from HTML element


Copy after login

Use setProperty() method

The second way to remove CSS properties from HTML elements is to use the setProperty() method. The setProperty() method is used to set CSS properties of an HTML element, but when we set an empty string for any CSS property, we can remove the CSS property from the element.

grammar

Users can use the setProperty() method to remove CSS properties from HTML elements according to the following syntax.

ele.style.setProperty(css property, "");
Copy after login

In the above syntax, we pass the property name as the first parameter and the empty string as the second parameter.

Example

In the example below, we create a div element as in the first example containing some CSS properties. In the removeBorder() function, we access the div element using the class name and the setProperty() method to set the border's empty string.

In the output we can initially observe the green border and when we click on the button it removes the border.



    

Using the setProperty() method to remove the CSS property from HTML element


Copy after login

Remove CSS properties by setting "null" value

Another way to remove CSS properties from HTML elements is to set a null value for a specific CSS property. We can also set null value for any specific CSS property using JavaScript’s setProperty() method and JQuery’s CSS() method. Here we will directly access the CSS property and set a null value for it.

grammar

Users can remove CSS properties from HTML elements by setting a null value for the CSS property by following the following syntax.

element.style.css_property = null;
Copy after login

In the above syntax, users need to replace "element" and "css_property" with specific HTML element and CSS property names respectively.

Example

In the example below, the div element contains some text and we set the font size to 3rem. In the removeSize() function, we access the "style" object of the div element and set the "fontSize" property of the style object to null.

In the output, the user can click a button to set the initial font size of the div element text.



    

Setting the null values to CSS proeprties to remove the CSS property from HTML element

Hello World! How are you?

Copy after login

Use removeAttribute() method

The fourth way to remove CSS attributes from HTML elements is to use the removeAttribute() method. JavaScript's removeAttribute() method is used to remove a specific HTML attribute from JavaScript. In our case, we can remove the "style" attribute, which will remove all styles from the HTML element.

grammar

Users can use the removeAttribute() method to remove CSS attributes from HTML elements according to the following syntax.

ele.removeAttribute("style");
Copy after login

In the above syntax, we pass "style" as parameter of remvoeAttribute() method to remove all CSS attributes.

Example

In the example below, we create a "

" element that contains text and multiple CSS properties.

removeStyle() function removes the "style" attribute from the "

" element using the removeAttribute() method.



    

Using the removeAttribute() method to remove the CSS property from HTML element

Welcome to the tutorials point, CSS tutorial.


Copy after login

in conclusion

We learned four different ways to remove CSS properties from HTML elements. In the first method, we have used the removeProperty() method, which is one of the best ways to remove CSS properties. The second and third method are almost similar in setting null value for CSS properties, but it does not remove CSS properties from HTML elements.

In the last method, we used the removeAttribute() method to remove the "style" attribute, but it should be used only when we need to remove all styles from the HTML element.

All the above methods only work for removing inline CSS properties.

The above is the detailed content of How to remove CSS properties using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!