Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

Change CSS Using Javascript (3 Methods)

Nikita Arora by Nikita Arora
April 9, 2025
Reading Time: 6 mins read
Change CSS using Javascript
Follow us on Google News   Subscribe to our newsletter

CSS stands for cascading style sheet and it plays a vital role in the field of web development. CSS helps us place our HTML elements correctly and make our whole application presentable and user-appealing. Not only this, but CSS also helps us to make animations and responsive designs. In this article, we will learn how to change CSS using Javascript and why we need to change CSS using Javascript.

How can we change CSS using JavaScript?

In the web domain, we need to make our web application user-interactive, presentable, and animated. Thus, to achieve this, we need to change CSS using Javascript on every user input so that our application becomes interactive and visually appealing.

Various methods are commonly used to change CSS using javascript and we will discuss them one by one in this article.

The methods we will be discussing in this article are as follows:

  1. Using the ‘style’ property
  2. Using the ‘classList’ property
  3. Using the ‘setAttribute’ method

There are various other methods too like using ’setInterval’ and ‘setTimeout’ to make animations by manipulating the element’s CSS properties.

1) Changing CSS Using the ‘Style’ Property

The style property is used to change inline CSS. It gives us direct access to elements in DOM and we can directly change its inline CSS easily.

Here is a demonstration to use the ‘style’ property:

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Character encoding and viewport setup -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Title of the document -->
    <title>FavTutor</title>

</head>

<body>
    <!-- A div element with class 'box' and id 'myBox' -->
    <div class="box" id="myBox"></div>

    <script>
        // Access the element with id 'myBox'
        var boxElement = document.getElementById('myBox');
        
        // Manipulate CSS properties using the style property
        // Changing the background color to red
        boxElement.style.backgroundColor = 'red';
        // Modifying width to 150px
        boxElement.style.width = '150px';
        // Modifying height to 150px
        boxElement.style.height = '150px';
    </script>

</body>
</html>

In the code above, we have made a div with class “box” and id “myBox” and we have defined inline CSS in a style tag.

In our script, we have simply selected that div using document.getElementById()  and passing ‘myBox’ as id in it. After selecting our div we simply changed its backgroundColor and height by using boxElement.style.backgroundColor and boxElement.style.height respectively.

Thus in this way, we can simply use the style property to change CSS.

2) Change using the ‘classList’ property

This property is very easy to use and it helps to change the CSS of any element. It offers several methods for adding, removing, toggling, and checking classes, simplifying common styling tasks.

Here is a demonstration to use ‘classList’ property:

<!DOCTYPE html>

<html lang="en">

<head>

    <!-- Character encoding and viewport setup -->

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Title of the document -->

    <title>Favtutor</title>

    <!-- Internal CSS styling -->

    <style>

        /* Styling for elements with class 'highlight' */

        .highlight {

            background-color: yellow;

        }

    </style>

</head>

<body>

    <!-- A div element with id 'myDiv' -->

    <div id="myDiv">FavTutor</div>

    <!-- Button to toggle highlighting -->

    <button onclick="toggleHighlight()">Toggle Highlight</button>

    <script>

        // Function to toggle the 'highlight' class

        function toggleHighlight() {

            // Accessing the div element with id 'myDiv'

            var divElement = document.getElementById('myDiv');

            // Toggling the 'highlight' class on the div element

            divElement.classList.toggle('highlight');

        }

    </script>

</body>

</html>

In the above code, we created a div element with id ‘myDiv’ and a button to toggle highlighting. Inside the button, we have used an onClick event listener which listens to the user’s click and executes the toggleHighlight() function on every click. 

The toggleHighlight() function is written inside our script which simply accesses our div element using document.getElementById('myDiv') and then we use classList.toggle('highlight') to toggle the ‘highlight’ class written inside the style tag inside the head of our code.

Thus in this way, we can simply use the classList property to add, remove, and toggle classes.

3) Changing CSS Using The ‘setAttribute’ Method

Using the ‘setAttribute’ method, we can easily set or modify any attribute of a DOM element (for example, style property). It takes two arguments:

  • attributeName (string): The name of the attribute we want to set or modify.
  • value (string): The value we want to assign to the attribute.

Here is a demonstration to use the ‘setAttribute’ method:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>FavTutor</title>

</head>

<body>

    <!-- Div element with id "myDiv" and initial style color: blue -->

    <div id="myDiv" style="color: blue;">FavTutor</div>

    <!-- Button triggering the changeColor function -->

    <button onclick="changeColor()">Change Color</button>

    <script>

        // JavaScript function to change the color of the div element

        function changeColor() {

            // Selecting the div element with id "myDiv"

            var divElement = document.getElementById('myDiv');

            // Setting the style attribute of the div element to change its color to red

            divElement.setAttribute('style', 'color: red;');

        }

    </script>

</body>

</html>

In the above code, we created a div element with the id  ‘myDiv’ and an initial style color of blue, along with a button to change color. Inside the button, we have used an onClick event listener which listens to the user’s click and executes the changeColor() function on every click. 

The changeColor() function is written inside our script, which simply accesses our div element using document.getElementById('myDiv'), and then we use setAttribute('style', 'color: red;'), which sets the color to red. Inside the setAttribute method, we have passed the attributeName as ‘style’ and the value as ‘color: red;’.

Thus in this way, we can use the setAttribute method by passing attributeName and value to change our CSS.

Conclusion

In this article, we learned why we need CSS in our web applications and why is a need to change the CSS using Javascript. Moving ahead we also explored some of the commonly used properties and methods that are used to change CSS based on some user interaction or any event.

ShareTweetShareSendSend
Nikita Arora

Nikita Arora

I'm Nikita, a B.Tech Computer Engineering student. My passion lies in web development, data structures, and algorithms. Committed to continuous learning, I find immense joy in sharing insights and fostering collaboration among like-minded peers in web development. Excited about exploring interesting opportunities in technology.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.