HTML | DOM className Property
In the HTML document, the className property is used to set or return the value of an element’s class attribute. Using this property, the user can change the class of an element to the desired class.
Syntax:
HTMLElementObject.className;
HTMLElementObject.className = class;
The className specifies the class name of an element. To apply multiple classes, separate them using spaces. As an example, if an element has two classes than we specify them as “classname1 classname2” where classname1 and classname2 are the names of two different classes. The className property returns a string or a space-separated list of classes of an element.
Example-1: This example set the class for <div> element.
<!DOCTYPE html> <html> <head> <title> HTML | DOM className Property </title> <style> .do_style { width: 600px; height: 100px; background-color: lightgreen; text-align: center; font-size: 25px; color: green; margin-bottom: 10px; } </style> </head> <body> <p>Click the button to set a class for div.</p> <div id="div1"> GeeksforGeeks </div> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("div1").className = "do_style"; } </script> </body> </html> |
Output:
Before Click on The Button:

Initially
After Click on The Button:

After clicking try it button
Example-2: This example get the classes of < div> element.
<!DOCTYPE html> <html> <head> <title> HTML | DOM className Property </title> <style> .do_style { width: 600px; height: 100px; background-color: lightgreen; text-align: center; font-size: 25px; color: green; margin-bottom: 10px; } </style> </head> <body> <p>Click the button to set a class for div.</p> <div id="div1"> GeeksforGeeks </div> <button onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("div1").className = "do_style"; } </script> </body> </html> |
Output:
Before Click on The Button:

Initially
After Click on The Button:

After pressing try it button
Example-3: This example overwrites the existing class names.
<!DOCTYPE html> <html> <head> <title> HTML | DOM className Property </title> <style> .oldstyle { width: 300px; height: 50px; background-color: lightgreen; color: green; margin-bottom: 10px; } .newstyle { width: 400px; height: 100px; background-color: white; text-align: center; font-size: 25px; color: green; margin-bottom: 10px; } </style> </head> <body> <p> Click the button to change the value of the class attribute of div to "newstyle". </p> <div id="div1" class="oldstyle"> GeeksforGeeks </div> <button onclick="myFunction()"> Try it </button> <script> function myFunction() { document.getElementById("div1").className = "newstyle"; } </script> </body> </html> |
Output:
Before Click on The Button:

Initially
After Click on The Button:

After pressing try it
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- HTML | DOM specified Property
- HTML | DOM id Property
- HTML | DOM Bdo dir Property
- HTML | DOM value Property
- HTML | DOM li value Property
- HTML | DOM name Property
- HTML | DOM URL Property
- HTML | DOM dir Property
- HTML | DOM ownerDocument Property
- HTML | DOM childNodes Property
- HTML | DOM documentElement Property
- HTML | DOM classList Property
- HTML | DOM Button name Property
- HTML | DOM domain Property
- HTML | DOM defaultView Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



