How to load CSS files using JavaScript? Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report 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 element using createElement('link') method.Initialize the attributes of the link element.Append the link element to the head.Example 1: This example uses JavaScript to add CSS files to an HTML document. Create CSS file using name style.css: CSS .GFG { color:green; } Use JavaScript to add CSS file in HTML file: HTML <!DOCTYPE html> <html> <head> <title> Load CSS file using JavaScript </title> <script> // Get HTML head element let head = document.getElementsByTagName('HEAD')[0]; // Create new link Element let link = document.createElement('link'); // set the attributes for link element link.rel = 'stylesheet'; link.type = 'text/css'; link.href = 'style.css'; // Append link element to HTML head head.appendChild(link); </script> </head> <body> <h2 class="GFG">GeeksForGeeks</h2> </body> </html> Output: Example 2: This example uses JavaScript to add CSS files in HTML document. Create CSS file using name style.css: CSS .GFG { font-size:24px; font-weight:bold; color:white; background-color:green; padding:10px; text-align:center; } Use JavaScript to add CSS file in HTML file: HTML <!DOCTYPE html> <html> <head> <title> Load CSS file using JavaScript </title> <script> // Create new link Element let link = document.createElement('link'); // set the attributes for link element link.rel = 'stylesheet'; link.type = 'text/css'; link.href = 'style.css'; // Get HTML head element to append // link element to it document.getElementsByTagName('HEAD')[0].appendChild(link); </script> </head> <body> <div class="GFG">GeeksforGeeks</div> </body> </html> Output: CSS is the foundation of webpages, is used for webpage development by styling websites and web apps. JavaScript is best known for web page development but it is also used in a variety of non-browser environments.You can learn more about CSS and Javascript from the links given below: CSS Tutorial and CSS ExamplesJavaScript Tutorial and JavaScript Examples Create Quiz Comment A aman neekhara Follow 1 Improve A aman neekhara Follow 1 Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like