Add Remove Class to Body - Toggle Class
To add or remove a class from the body element depending on some condition or user action can be useful for changing the appearance or behavior of the page dynamically. In this tutorial, we will explore how to toggle a class on the body element using three popular frameworks: JavaScript, React, and Vue.
Thanks for your feedback!
Your contributions will help us to improve service.
Example 1: Javascript Add Remove Class to Body (Toggle Class)
In this example, we will learn how to add or remove a class from the body. For Example, you can use it to switch between light and dark themes, show or hide a sidebar, or apply different animations. We will demonstrate how to toggle a class on the body element using JavaScript.
Output of Javascript add class to body tag dynamically
Example 2 : React Add Class to Body
In this second example of this tutorial, we will learn how to add and remove classes to the body element using React JS. We will utilize the `className` attribute of the body element and the `useState` hook to dynamically change the class name.
Output of React Js add class to body tag dynamically
Example 3 : Vue Add Class to Body
In this third example, we will learn how to toggle a class on the body element using Vue.js.