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 file in the HTML document. JavaScript can also be used to load a CSS file in the HTML document.
Approach:
- Use document.getElementsByTagName() method to get HTML head element.
- Create new link element using createElement(‘link’) method.
- Initialize the attributes of link element.
- Append link element to the head.
Example 1: This example uses JavaScript to add CSS file in HTML document.
- Create CSS file using name style.css:
.GFG {color:green;}chevron_rightfilter_none - Use JavaScript to add CSS file:
<!DOCTYPE html><html><head><title>Load CSS file using JavaScript</title><script>// Get HTML head elementvarhead = document.getElementsByTagName('HEAD')[0];// Create new link Elementvarlink = document.createElement('link');// set the attributes for link elementlink.rel ='stylesheet';link.type ='text/css';link.href ='style.css';// Append link element to HTML headhead.appendChild(link);</script></head><body><h2 class="GFG">GeeksForGeeks</h2></body></html>chevron_rightfilter_none
Output:

Example 2: This example uses JavaScript to add CSS file in HTML document.
- Create CSS file using name style.css:
.GFG {font-size:24px;font-weight:bold;color:white;background-color:green;padding:10px;text-align:center;}chevron_rightfilter_none - Use JavaScript to add CSS file:
<!DOCTYPE html><html><head><title>Load CSS file using JavaScript</title><script>// Create new link Elementvarlink = document.createElement('link');// set the attributes for link elementlink.rel ='stylesheet';link.type ='text/css';link.href ='style.css';// Get HTML head element to append// link element to itdocument.getElementsByTagName('HEAD')[0].appendChild(link);</script></head><body><div class="GFG">GeeksforGeeks</div></body></html>chevron_rightfilter_none
Output:

Recommended Posts:
- Atomics.load( ) In JavaScript
- jQuery | load() with Examples
- HTML | DOM Video load( ) Method
- How to create fade-in effect on page load using CSS ?
- JQuery | Set focus on a form input text field on page load
- How to open a PDF files in web browser using PHP?
- How to include() all PHP files from a directory ?
- Deleting all files from a folder using PHP
- Perl | Reading Excel Files
- Creating a dataframe using Excel files
- How to upload files asynchronously using jQuery?
- Perl | Creating Excel Files
- HTML | DOM Input FileUpload files Property
- How to select and upload multiple files with HTML and PHP, using HTTP POST?
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



