How to add CSS Rules to the Stylesheet using JavaScript ? Last Updated : 06 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In this example, we will see how to add CSS rules to the stylesheet element using JavaScript. First, we will create an HTML document and then add some CSS rules using JavaScript. Syntax: let styleText = document.getElementById('GFG').sheet; let st = `.box { width: 100px; height: 100px; }`; styleText.insertRule(st, 0);Approach: First, we select the stylesheet element using any query selector, and then assign the CSS styles to a variable. After that, use the insertRule() property to add the CSS rules to the stylesheet using JavaScript. Example 1: In this example, we will add the CSS Rules to the stylesheet of the div element. HTML <!DOCTYPE html> <html lang="en"> <head> <title> How to add CSS Rules to the Stylesheet using JavaScript? </title> <style type="text/css" id="GFG"> body { text-align: center; } h1 { color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h3> How to add CSS Rules to the Stylesheet using JavaScript </h3> <div class="box"></div> <script> let styleText = document.getElementById('GFG').sheet; let stl = `.box { width: 100px; height: 100px; background-color: green; display: block; margin-left: auto; margin-right: auto; }`; styleText.insertRule(stl, 0); </script> </body> </html> Output: Example 2: In this example, we will add the CSS Rules to the stylesheet of the div element. Here, we will merge all the CSS styles as a string and apply them to the div element. HTML <!DOCTYPE html> <html lang="en"> <head> <title> How to add CSS Rules to the Stylesheet using JavaScript? </title> <style type="text/css" id="GFG"> body { text-align: center; } h1 { color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h3> How to add CSS Rules to the Stylesheet using JavaScript? </h3> <div class="box"></div> <script> let styleText = document.getElementById('GFG').sheet let stl = '.box {'; stl += 'width: 100px;'; stl += 'height: 100px;'; stl += 'background-color: green;'; stl += 'display: block;'; stl += 'margin-left: auto;'; stl += 'margin-right: auto;'; stl += 'animation: GFG 5s infinite linear;'; stl += '}'; styleText.insertRule(stl, 0); </script> </body> </html> Output: Comment More infoAdvertise with us V vkash8574 Follow Improve Article Tags : JavaScript CSS-Properties CSS-Questions JavaScript-Questions Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at 7 min read JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an 7 min read CSS Interview Questions and Answers CSS (Cascading Style Sheets) is the language that styles and organizes web pages. It makes websites visually appealing and user-friendly. Mastering CSS is crucial whether you're a beginner or a seasoned developer. This guide presents 60+ CSS interview questions and answers, categorized to help you p 15+ min read Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie 15+ min read JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav 15+ min read Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex 4 min read Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax 5 min read JavaScript Exercises, Practice Questions and Solutions JavaScript Exercise covers interactive quizzes, tracks progress, and enhances coding skills with our engaging portal. Ideal for beginners and experienced developers, Level up your JavaScript proficiency at your own pace. Start coding now! A step-by-step JavaScript practice guide for beginner to adva 3 min read Like