How to Create Dark/Light Mode for Website using JavaScript/jQuery?
Light-on-dark color scheme, also called dark mode, is a supplemental mode that uses a color scheme in which content of a webpage is displayed on a dark background. Such a color scheme reduces the light emitted by screens and enhances readability. Switching to dark mode allows website users to move to an eye-friendly and resource-saving design whenever they want.
Steps to Create Dark/Light mode:
- Create 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 mode.
- Add functionality to the switch/toggler to toggle between light and dark mode using javascript or jQuery code.
Example 1: The following example demonstrate switching between light and dark mode using JQuery code. It bascially works by using functions hasClass(), addClass() and removeClass() method.
<!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= </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= <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:
- Light mode:

- Dark mode:

Example 2: The following example demonstrate switching between light and dark mode by using toggle() function in javascript code.
<!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() { var element = document.body; element.classList.toggle("dark"); } </script> </body> </html> |
Output:
- Light mode:

- Dark mode:

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.
Recommended Posts:
- Create a Website Alarm Using Python
- How to create custom arrows for your website using HTML and CSS?
- How to create a responsive Modal Sign-Up form for a Website?
- How to create responsive website zoomed out to full width on mobile using Bootstrap?
- Mean, Median and Mode in R Programming
- CSS | writing-mode Property
- JavaScript | Strict Mode
- Strict mode in JavaScript
- CSS mix-blend-mode Property
- CSS | Website Layout
- How cookies are used in a website?
- CSS | background-blend-mode Property
- CSS | animation-fill-mode Property
- Difference between Website and Blog
- What is the Difference between Website and Web Portal
- Static vs Dynamic Website
- Ways to track a Website
- Making a QR code for a website
- Difference Between Web application and Website
- Website Blocker Using Python
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.


