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 Next Article How to add CSS Rules to the Stylesheet using JavaScript ? V vkash8574 Follow Improve Article Tags : JavaScript CSS-Properties CSS-Questions JavaScript-Questions Similar Reads How to Switch Between Multiple CSS Stylesheets using JavaScript? To switch between multiple CSS stylesheets using JavaScript, the href attribute of the <link> element can be changed dynamically in the HTML document that links to the stylesheets. <link id="theme" rel="stylesheet" type="text/css" href="light.css" /> The "href" attribute specifies the fi 2 min read How to Create a Style Tag using JavaScript? To create a <style> tag using JavaScript, use the document.createElement() method to create the tag, which allows you to dynamically add elements to the DOM.Syntaxdocument.createElement('style')Example 1: This HTML code dynamically styles a <div> element with a green background and white 1 min read How to remove CSS property using JavaScript? To remove CSS property using JavaScript, we have different methods. In this article, we will learn how to remove CSS property using JavaScript. Below are the Methods used to remove CSS property using JavaScript: Table of Content Using CSS removePropertyUsing the setProperty methodMethod 1: Using CSS 2 min read How to Remove and add Style with .css() Function using JavaScript? There are many cases, especially as the content gets more interactive, where the developer wants styles to dynamically kick in based on user input, some code having run in the background, and more. In these sorts of scenarios, the CSS model involving style rules or inline styles doesn't help. The so 3 min read How to add content in <head> section using jQuery/JavaScript? Any content can be added to the <head> section using the following two approaches:Using the document.head propertySelecting the head element using jQueryMethod 1: Using the document.head property.The head property of the document returns the head element of the document. Any new content can be 3 min read Like