Apply CSS dynamically with JavaScript

Article by: Manish Methani

Last Updated: September 10, 2021 at 8:04am IST
8 min 13 sec


Cascading Style Sheets (CSS) are a fundamental part of web development, allowing you to control the presentation and styling of your web pages. While CSS is typically applied statically, you can also apply it dynamically using JavaScript. This dynamic approach is particularly useful when you need to change styles based on user interactions, data, or other conditions. In this article, we will walk through how to apply CSS dynamically with JavaScript, complete with examples and a step-by-step guide.

Final Output

Table of Contents:

1. Prerequisites

Before we dive into the dynamic application of CSS, ensure you have a basic understanding of HTML, CSS, and JavaScript. You should have a code editor and a web browser to experiment with the examples.

2. Setting Up the HTML Structure

Start by creating the HTML structure. You can use a basic HTML template, as follows:

<!DOCTYPE html>
    <title>Dynamic CSS Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
    <button id="toggleButton">Change Text Color</button>
    <p id="changeMe">This is a dynamic text example.</p>
    <script src="script.js"></script>

This code includes a button, a paragraph, and references to an external CSS file ("styles.css") and a JavaScript file ("script.js").

3. Adding CSS Rules

In your "styles.css" file, you can define the CSS rules you want to apply dynamically. For instance:

p {
    font-size: 20px;
    color: black;

.changed {
    color: red;

Here, we've defined the initial styles for the paragraph and a CSS class named "changed" with red text color.

4. Writing JavaScript to Apply Dynamic CSS

Now, let's create a JavaScript file ("script.js") to apply dynamic CSS. Start by selecting the elements you want to manipulate:

const toggleButton = document.getElementById('toggleButton');
const changeMe = document.getElementById('changeMe');

5. Example: Changing Text Color on Button Click

We'll use an event listener to change the text color when the button is clicked:

toggleButton.addEventListener('click', function() {

In this code, we add a click event listener to the button. When clicked, it toggles the "changed" class on the paragraph, changing the text color.

6. Advanced Techniques for Dynamic Styling

You can go beyond this basic example to dynamically adjust a wide range of CSS properties, such as width, height, background color, and more. You can also use JavaScript to conditionally apply styles based on user inputs, API responses, or other variables.

Learn about my Course:

I packaged my knowledge of the Full Stack Web Development Course in this 7-hour course. You will get all the future updates & and benefits in the same course. You should definitely check it out. The course is available on Udemy and Codzify Website as well.

📌 Master Full Stack Web Development with Manish Methani:

📌 Codzify Course Link:

If you are interested in learning, I am currently running a Diwali offer. You can learn and develop your skills with a little investment of just 449 Rs 0r $5 only.

What You'll Learn in 'Full Stack Web Development Course'

In this course, you will get to learn how to build full-fledged web apps from scratch using Angular, Firebase, HTML, CSS, Bootstrap & Angular Material

  • You'll get to learn Angular 16 Framework from Scratch along with Firebase for the backend.

  • You will get to learn about HTML, CSS & Bootstrap as well if you are a complete beginner in the world of Websites.

  • We will create 1 Full-fledged Online Learning Web App from scratch.

  • Everything you need to know about Git version control.

  • An experience of how developers work collaboratively using GIT.

  • Practical use of HTML Tags.

  • Practical use of CSS properties.

  • How to use Bootstrap 5 classes?

  • Typescript

  • Introduction to Firebase.

  • CRUD Operations in Firebase.

  • Google Sign-In, Phone Number Authentication

  • Complex Tasks like how to add CSS programmatically using Angular 16?

Final Output


1. What is dynamic CSS application?

Dynamic CSS application refers to the process of modifying the styles of HTML elements on a web page using JavaScript. This allows developers to change the appearance of elements, such as altering colors, fonts, and sizes, in response to user interactions or other events.

2. Why would you apply CSS dynamically with JavaScript?

Applying CSS dynamically with JavaScript offers flexibility and interactivity in web development. It allows developers to create responsive and engaging user interfaces by changing styles based on user actions, data, or conditions. This is commonly used for animations, form validation, and enhancing user experience.

3. How can I apply CSS dynamically in my web project?

To apply CSS dynamically in your web project, you can use JavaScript to select HTML elements and change their styles. You can do this by modifying the elements `style` attribute, adding or removing CSS classes, or even injecting CSS rules directly into the documents stylesheet. There are various JavaScript methods and libraries available for dynamic CSS application.

4. Are there any potential challenges when applying CSS dynamically?

Yes, dynamic CSS application can introduce complexities, such as maintaining code readability and ensuring cross-browser compatibility. It is essential to organize your code effectively, test for different browsers, and consider the performance implications, as frequent style changes can impact page load times.


Applying CSS dynamically with JavaScript provides the flexibility to enhance your web applications. Whether you're changing styles based on user interactions or customizing the user experience, this technique is a valuable addition to your web development skills.

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer