Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Switch Between Multiple CSS Stylesheets using JavaScript?
Next article icon

How to Switch Between Multiple CSS Stylesheets using JavaScript?

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 file location of the CSS file. By altering this tag, we can add new CSS to the website. The implementation can be done using any of the following methods.

Example 1: When you want to make a switch or toggle button, to toggle the CSS. It switches between the values depending upon the currently active value.

HTML
<!DOCTYPE html> <html>  <head>     <!-- Add the stylesheet -->     <link id="theme" rel="stylesheet"            type="text/css" href="light.css" /> </head>  <body>     <h2>Changing Style Sheets</h2>     <br />     Click the button below to switch    	between light and dark themes.<br />      <button onclick="toggleTheme()">Switch</button>     <script>         function toggleTheme() {             // Select the <link> element             let theme = document.getElementById('theme');              // Toggle between light.css and dark.css             if (theme.getAttribute('href') == 'light.css') {                 theme.setAttribute('href', 'dark.css');             } else {                 theme.setAttribute('href', 'light.css');             }         }     </script> </body>  </html> 
CSS
/* dark.css */  body {     background-color: black;     color: white; } 
CSS
/* light.css */  body {     background-color: white;     color: black; } 

Output:

How-to-switch-between-multiple-CSS-stylesheets-using-JavaScript

Example 2: When you want to select from multiple style sheets. The value for the "href" attribute is passed to the function call itself.

Prerequisite: Prepare all the style sheets in a folder.

HTML
<!DOCTYPE html> <html>  <head>     <!-- Add the style sheet. -->     <link id="theme" rel="stylesheet" type="text/css" href="light.css" />      <script>         function toggleTheme(value) {              // Obtain the name of stylesheet              // as a parameter and set it              // using href attribute.             let sheets = document                 .getElementsByTagName('link');              sheets[0].href = value;         }     </script> </head>  <body>     <h2>Changing Style Sheets</h2>     <br />     Switch between multiple themes     using the buttons below.<br />      <button onclick="toggleTheme('light.css')">         Light     </button>      <button onclick="toggleTheme('dark.css')">         Dark     </button>      <button onclick="toggleTheme('geeky.css')">         Geeky     </button>      <button onclick="toggleTheme('aquatic.css')">         Aquatic     </button> </body>  </html> 
CSS
/* light.css */  body {     background-color: white;     color: black; } 
CSS
/* dark.css */  body {     background-color: black;     color: white; } 
CSS
/* aquatic.css */  body {     background-color: #4DB6AC;     color: #004D40; } 
CSS
/* geeky.css */  body {     background-color: #282C34;     color: #61DAFB; } 

Output:

How-to-switch-between-multiple-CSS-stylesheets-using-JavaScript-

Note: The corresponding CSS files with required names should be available and the path to them should be passed using the function. The files specified here are placed in the same folder of the HTML file so that the path resembles 'light.css'.


Next Article
How to Switch Between Multiple CSS Stylesheets using JavaScript?

C

chitrankmishra
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

    How to add CSS Rules to the Stylesheet using JavaScript ?
    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
    2 min read
    How to Create Multiple Themes using Tailwind CSS ?
    In this article, we will discuss how to create multiple themes using Tailwind CSS. Tailwind CSS is a popular utility-first CSS framework that provides a lot of pre-designed CSS classes for designing websites. With Tailwind CSS, you can easily create multiple themes for your website. This means that
    3 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 Create Tab Headers using CSS & JavaScript?
    Tab headers are a commonly used user interface element in web development. They provide a way to organize content into distinct sections, allowing users to switch between them easily. Each tab represents a different category or topic, and clicking on a tab displays the corresponding content while hi
    2 min read
    How to check if CSS property is supported in browser using JavaScript ?
    Few CSS properties are not supported in some browsers, thus it is required to check whether a CSS property & its value are supported in the current browser. We can find it using JavaScript using CSS.supports() method. Syntax: supports(propertyName, value) supports(condition) There are two distin
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences