How to Create Dark/Light Mode for Website using JavaScript/jQuery? Last Updated : 14 May, 2024 Comments Improve Suggest changes Like Article Like Report Creating Dark/Light Mode for a website involves toggling between light and dark color schemes. This feature allows users to switch between the two schemes based on their preferences. JavaScript/jQuery is utilized to dynamically adjust CSS styles, enhancing accessibility and user experience according to user or system settings. Steps to Create Dark/Light ModeCreate an HTML Document.Create CSS for the document file as well as for dark mode.Add a switch/toggler to toggle between light and dark modes.Add functionality to the switch/toggler to toggle between light and dark mode using JavaScript or jQuery code.Example 1: The following example demonstrates switching between light and dark modes using JQuery code. It basically works by using functions hasClass(), addClass(), and removeClass() methods. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dark Mode</title> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <style> body{ padding:10% 3% 10% 3%; text-align:center; } img{ height:140px; width:140px; } h1{ color: #32a852; } .mode { float:right; } .change { cursor: pointer; border: 1px solid #555; border-radius: 40%; width: 20px; text-align: center; padding: 5px; margin-left: 8px; } .dark{ background-color: #222; color: #e6e6e6; } </style> </head> <body> <div class="mode"> Dark mode: <span class="change">OFF</span> </div> <div> <h1>GeeksforGeeks</h1> <p><i>A Computer Science Portal for Geeks</i></p> <h3>Light and Dark Mode</h3> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20200122115631/GeeksforGeeks210.png"> <p> Click on the switch on top-right to move to dark mode. </p> </div> <script> $( ".change" ).on("click", function() { if( $( "body" ).hasClass( "dark" )) { $( "body" ).removeClass( "dark" ); $( ".change" ).text( "OFF" ); } else { $( "body" ).addClass( "dark" ); $( ".change" ).text( "ON" ); } }); </script> </body> </html> Output: Dark/Light Mode for Website using JavaScript/jQuery Example Output Example 2: The following example demonstrates switching between light and dark mode by using toggle() function in javascript code. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Dark Mode</title> <style> body{ padding:0% 3% 10% 3%; text-align:center; } h1{ color: #32a852; margin-top:30px; } button{ cursor: pointer; border: 1px solid #555; text-align: center; padding: 5px; margin-left: 8px; } .dark{ background-color: #222; color: #e6e6e6; } </style> </head> <body> <h1>GeeksforGeeks</h1> <p><i>A Computer Science Portal for Geeks</i></p> <h3>Light and Dark Mode</h3> <button onclick="myFunction()">Switch mode</button> <script> function myFunction() { let element = document.body; element.classList.toggle("dark"); } </script> </body> </html> Output: Dark/Light Mode for Website using JavaScript/jQuery Example Output Night mode/Dark mode besides adding extra functionality to the website also enhances user experience and accessibility. It is useful for websites that publish long content and requires users to focus on the screen for a larger period of time. Comment More infoAdvertise with us Next Article How to Create Dark/Light Mode for Website using JavaScript/jQuery? G g_ragini Follow Improve Article Tags : JavaScript Web Technologies JQuery JavaScript-Questions jQuery-Questions +1 More Similar Reads Creating a Dark Mode for Websites using HTML Bootstrap & JavaScript Dark Mode for websites enhances user experience by providing a visually comfortable environment, especially in low-light conditions. Implemented with HTML, Bootstrap, and JavaScript, it offers a sleek interface that reduces eye strain and promotes readability, offering users greater accessibility. P 3 min read Implement Dark Mode in Websites Using HTML CSS & JavaScript Dark and light modes let users switch between a dark background with light text and a light background with dark text, making websites more comfortable and accessible based on their preferences or surroundings.What Weâre Going to CreateA button allows users to switch between dark and light themes dy 5 min read How to Create dark theme using Slider in CSS ? In this article, we will learn to create a dark theme using the slider in CSS. In modern websites, we see a wonderful feature to change the website themes just by checking a slider. To check this awesome feature go to the geeksforgeeks.org website where you can change the theme as per your preferenc 3 min read Light/Dark Mode User Profile Card using HTML CSS and JavaScript This article will show how to make an Animated User Profile Card using HTML, CSS, and JavaScript. We generally see these effects in portfolios of professionals. In this, we have created a code that generates a personalized card with the user's name, image, and description. Additionally, we've implem 3 min read How to Create Image Lightbox Gallery using HTML CSS and JavaScript ? A lightbox gallery is basically used to view the gallery images in detail specifically. You can code the JavaScript to do so but also we can use some downloaded JS and CSS. In this article, we will download the lightbox and attach the JS and CSS files into our code. For our own design satisfaction, 3 min read How to create a Color Input using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Color Input Area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ h 1 min read How to create Dark Mode in ReactJS using Material UI ? Over the past few years, dark mode has gained widespread popularity as a feature in various applications and websites. It provides a visually pleasing and more comfortable viewing experience, particularly in low-light environments. If you're using ReactJS and Material UI, incorporating dark mode fun 3 min read How to create dark mode using prefer-color-scheme media query ? In this article, we are creating a dark mode interface using the prefer-color-scheme media query. The prefer-color-scheme is a new media query that is used to detect the current theme of your system and provide a feature that helps in creating your web page theme according to your system preference. 3 min read How to add fade-out effect using pure JavaScript ? The fade effect is described as the gradual increase and decrease in the opacity of the selected portion. In other words, the fade effect is termed as the increase and decrease in opacity with respect to time. When this effect is applied with a gradual decrease in the opacity it is known as the fade 4 min read How to create Theme Disable flip toggle switch using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Theme Disable flip toggle switch using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link 1 min read Like