How to add CSS Rules to the Stylesheet using JavaScript ? Last Updated : 06 Sep, 2022 Comments Improve Suggest changes 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 Web Technologies CSS CSS-Properties CSS-Questions JavaScript-Questions +2 More 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 How to load CSS files using JavaScript? The CSS file is used to describe how HTML elements will be displayed. There are various ways to add CSS files to the HTML document. JavaScript can also be used to load a CSS file in the HTML document. Approach:Use the document.getElementsByTagName() method to get HTML head element.Create a new link 2 min read How to change style/class of an element using JavaScript ? In this article, we will learn how we can change the style/class of an element. If you want to build a cool website or app then UI plays an important role. We can change, add or remove any CSS property from an HTML element on the occurrence of any event with the help of JavaScript. There are two com 4 min read How to set the style of the bottom border with JavaScript ? In this article, we will see how to set the style of the bottom border using JavaScript. To set the border-bottom of an element using JavaScript, we will use the borderBottomStyle property. The style borderBottomStyle property is used to set or return the style of the bottom border of an element. Ap 2 min read How to read CSS rule values with JavaScript? DOM (Document Object Model) object can read and manipulate CSS rules. We can use the following approaches to read all the Embedded CSS rules using JavaScript. Using getElementsByTagName() MethodUsing window.getComputedStyle() MethodApproach 1: Using the getElementsByTagName() MethodUse document.getE 3 min read How to get all CSS styles that are applied directly to an element using JavaScript ? In JavaScript, the getComputedStyle() function allows us to retrieve the CSS properties applied to a particular HTML element. This function returns an object that contains all the computed styles, including values from external stylesheets, inline styles, and browser defaults. Using the getPropertyV 3 min read Like