html

How to Remove CSS From a Div Using jQuery?

Some interns or new developers add too many styling properties to build a design. If the design is loaded with too many styles and the project manager wants to see only HTML, here jQuery can remove all styles by writing its 4 to 5 lines of code. It is a very efficient and effective method by removing styles and seeing the structure of that div or the HTML of the page.

This article demonstrates the different methods to remove CSS from a div using jQuery.

How to Remove CSS From a Div Using jQuery?

To remove CSS styles from the div, use the jQuery built-in attributes. There are two methods through which users can add or remove styles, inline and using classes.

Method 1: Remove Inline CSS From a Div

To remove inline styles that are applied to the HTML element, the “removeAttr()” method is utilized.

It is used when little styling of an element is needed. Follow the below steps to deal with it:

Step 1: Create a Structure
In the HTML file, create a “<div>” tag and add multiple HTML elements inside it. For instance, “<h1>”, “<h2>” and “<p>” tags are used in the below code:

<div>
 <h1>Hello Linuxhint Users</h1>
 <h2>Linuxhint is the place of heaven</h2>
 <p>queries related to programming languages.</p>
</div>
<button>Style Remover for Div</button>

After executing the above code, the webpage looks like this:

The output shows the HTML structure of the div and a button.

Step 2: Add Inline Styling
Inside the div opening tag, utilizes the “style” attribute and apply some CSS properties to make elements prominent and appealing:

<div style="
 color:darkmagenta;
 background-color: mediumaquamarine;
 margin: 20px;
 padding: 30px;"
>
 <h1>Hello Linuxhint Users</h1>
 <h2>Linuxhint is the place of heaven</h2>
 <p>queries related to programming languages.</p>
</div>
 <br>
<button>Style Remover for Div</button>

The output of the above code is:

The output confirms that the inline styles are applied to the div element only.

Step 3: Using jQuery to Remove Inline CSS
To remove style attributes, the parent function calls when the “document” is “ready”. In the below code, the inner function calls when user clicked the “button”. After that, this function selects all div elements that reside on the page and use the “remove.Attr()” method:

<script>
 $(document).ready(function() {
  $("button").click(function() {
   $("div").removeAttr("style");
  });
 });
</script>

After adding the jQuery code, the webpage works like this:

The above “gif” illustrates that the applied styles on the div are removed by clicking on the button.

Method 2: Remove Class CSS from a Div

The second way of styling the HTML element is by assigning them a “class”. Then, add CSS in that class portion inside the “<style>” tag or in a separate “CSS file”. These styles can also be removed using jQuery. Follow the below steps:

Step 1: Assign a Class to Div Element
In the HTML file, assign a class attribute to the “<div>” element. Also, assign an id of “remover” to the “<button>” tag:

<div class="styling">
 <h1>Hello Linuxhint Users</h1>
 <h2>Linuxhint is the place of heaven</h2>
 <p>queries related to programming languages.</p>
</div>
<button id="remover">Style Remover </button>

Next, go to the “<style>” tag and select the div class name “styling” and type CSS properties that are applied to the div element:

<style>
 .styling{
  color:goldenrod;
  background-color: seagreen;
  margin: 20px;
  padding: 30px;
 }
</style>

After executing the above code, the webpage looks like this:

The output displays that the styles are applied on the div element.

Step 2: Add jQuery To Remove CSS Styling
In the “<script>” tag add jQuery code same as stated in the above method. The jQuery “removeClass()” method removes the “styling” class that is assigned with the “div” element when the button is clicked:

<script>
$(document).ready(function() {
 $("button").click(function() {
  $("div").removeClass("styling");
 });
});
</script>

After adding the above code, the webpage works like this:

The above “gif” illustrates that the styles typed in the class are removed with a button click.

Conclusion

To remove the CSS from a div, the “remove.Attr()” and “removeClass()” methods can be used. The “remove.Attr()” removes the “style” attribute from the element which is selected. On the other hand, the “removeClass()” removes the selected element class that was being utilized for applying styles on that element. This article has successfully demonstrated how to remove CSS from a div using jQuery.

About the author

Abdul Moeed

I'm a versatile technical author who thrives on adaptive problem-solving. I have a talent for breaking down complex concepts into understandable terms and enjoy sharing my knowledge and experience with readers of all levels. I'm always eager to help others expand their understanding of technology.