All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Dynamically Change CSS Class With JavaScript

Last Updated : Mar 11, 2024

Dynamically Change CSS Class With JavaScript

In this article we will show you the solution of dynamically change css class with javascript, the Cascading Style Sheet properties or CSS are used to add style to the HTML file.

We can also add CSS dynamically to an HTML file by using JavaScript.

In this tutorial, we will see three different methods of JavaScript to achieve that - Style method, className method and classList method

Step By Step Guide On Dynamically Change CSS Class With JavaScript :-

Let us see the step-by-step guide of all the methods to change CSS class dynamically.

Method 1 - Style method

The first method we are going to use is the style method.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> dynamically change CSS class with JavaScript </title>
    <script>
        var element ;
        document.querySelector( " #h1 " ).style.color = " green " ;
        document.querySelector( " #h1 " ).style.backgroundColor = " white " ;
        element = document.querySelector( " #h1 " ).style.color ;
        console.log( element ) ;
    </script>
</head>
<body>
    <h1 id="h1">
        TalkersCode
    </h1>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here. We gave it an id.
  7. Within the <head> tag we have created the<script> tag to add JavaScript.
  8. For the JS we first created a variable named element
  9. We used document.querySelector().style then add style to the element.
  10. Using console.log() to display.

Method 2 - className

The second method we are going to use is the className method.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF-8 " >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge ">
    <meta name = " viewport" content = " width=device-width, initial-scale=1.0">
    <title> dynamically change CSS class with JavaScript </title>
    <script>
        var element ;
        document.querySelector( " #div1 " ).className = " div " ;
        element = document.querySelector( " div1 " ).className ;
        console.log( element ) ;
    </script>
</head >
<style >
    .div {
        color : blue ;
        background-color : antiquewhite ;
        font-size : medium ;
    }
</style>
<body>
    <div id="div1" >
        this is div.
    </div >
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively. Also, create a <style> tag, and into the style tag create some class with style.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <div> tag is created and We gave it an id also.
  7. Within the <head> tag we have created the<script> tag to add JavaScript.
  8. For the JS we first created a variable named element
  9. We used document.querySelector().className to add style by adding the class name created into the <style> tag.
  10. Using console.log() to display.

Method 3 - classList

The last method we’re going to use is the classList method.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF-8 " >
    <meta http-equiv = " X-UA-Compatible " content = " IE = edge " >
    <meta name = " viewport " content = " width=device-width , initial-scale=1.0">
    <title> dynamically change CSS class with JavaScript </title>
<script>
let el = Document.querySelector( " #div1 " )
el.classList.add( " class1 ")
el.classList.add( " class2 ")
el.classList.add( " class3 ")
console.log(el) ;
</script>
<style>
    .class1 {
        color : blue ;
    }
    .class2{
        background-color : antiquewhite ;
    }
    .class3 {
        font-weight : bolder ;
    }
</style>
</head>
<body>
    <h1> TalkersCode </h1>
    <div id = " div1 " >
        this is a div
    </div>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively. Also, create a <style> tag, and into the style tag create some class with style.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <div> tag is created and We gave it an id also.
  7. Within the <head> tag we have created the<script> tag to add JavaScript.
  8. For the JS we first created a variable named el.
  9. We used document.querySelector().classList.add to add style by adding the class name created into the <style> tag.
  10. We can also remove style by document.querySelector().classList.remove
  11. Using console.log() to display.

Conclusion :-

Last here in conclusion, here we can say that with the help of this article we are able to dynamically change CSS property using JavaScript.

I hope this article on dynamically change css class with javascript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪