Home > Web Front-end > JS Tutorial > How to remove CSS properties using JavaScript?

How to remove CSS properties using JavaScript?

王林
Release: 2023-09-10 18:09:03
forward
1436 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.

<html>
<body>
    <h3> Using the <i> removeProperty() method </i> to remove the CSS property from HTML element </h3>
    <div class = "ele" style = "width: 200px; height: 200px; background-color: red; border: 2px solid black;">
    </div>  <br>
    <button onclick = "removeColor()">
        Remove background color </button>
    <script>
        function removeColor() {
            var ele = document.querySelector(".ele");
            ele.style.removeProperty("background-color");
        }
    </script>
</html>
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.

<html>
<body>
    <h3> Using the <i> setProperty() method </i> to remove the CSS property from HTML element </h3>
    <div class = "ele" style = "width: 200px; height: 200px;background-color: blue; border: 10px solid green;">
    </div>    <br>
    <button onclick = "removeBorder()">
        Remove border </button>
    <script>
        function removeBorder() {
            var ele = document.querySelector(".ele");
            ele.style.setProperty("border", "");
        }
    </script>
</html>
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.

<html>
<body>
    <h3> Setting the <i> null values to CSS proeprties </i> to remove the CSS property from HTML element </h3>
    <div style = "font-size: 3rem;">
        Hello World! How are you?
    </div>
    </div> <br>
    <button onclick = "removeSize()">  Remove font-size </button>
    <script>
        function removeSize() {
            let div = document.querySelector('div');
            div.style.fontSize = null;
        }
    </script>
</html>
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.


<script> function removeStyle() { var ele = document.getElementsByClassName("para")[0]; ele.removeAttribute(&quot;style&quot;); } </script>
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template