Implement Dark Mode in Websites Using HTML CSS & JavaScript Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 dynamically.Background, text colors, and other visual elements change based on the selected mode.Handles the theme switch, ensuring a smooth transition for a personalized user experience.Project PreviewHow to Make Dark Mode for Websites using HTML CSS & JavaScript ?Implement Dark Mode in Websites -HTML and CSS codeThis is the basic structure of the webpage. The page will feature a button to toggle between dark and light modes. HTML <html> <head> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; transition: background-color 0.3s, color 0.3s; } .container { text-align: center; padding: 30px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); max-width: 600px; background-color: white; } button { padding: 10px 20px; font-size: 16px; border: none; border-radius: 5px; cursor: pointer; background-color: #007bff; color: white; transition: background-color 0.3s; } button:hover { background-color: #0056b3; } body.light-mode { background-color: #f4f4f4; color: #333; } body.dark-mode { background-color: #333; color: brown; } h1 { margin-bottom: 20px; } p { font-size: 18px; line-height: 1.6; margin: 10px 0; } ul { text-align: left; padding-left: 20px; margin-top: 20px; } li { margin: 8px 0; } a { color: #007bff; text-decoration: none; } a:hover { text-decoration: underline; } </style> </head> <body> <div class="container"> <h1>Welcome to Dark/Light Mode Toggle</h1> <p> This page demonstrates a simple yet powerful way to switch between <strong>dark</strong> and <strong>light</strong> modes for a better user experience. </p> <p> Switching to dark mode can reduce eye strain, especially in low-light environments, while light mode is better for bright settings. </p> <button id="theme-toggle">Switch to Dark Mode</button> <h2>Benefits of Dark/Light Mode</h2> <ul> <li>Improves accessibility for users with visual impairments.</li> <li>Reduces eye strain during prolonged usage.</li> <li>Personalizes the user experience.</li> <li>Saves device battery life on OLED and AMOLED screens.</li> </ul> <p> Learn more about web accessibility and user experience on the <a href="https://www.w3.org/WAI/" target="_blank"> W3C Accessibility Guidelines</a>. </p> </div> </body> </html> In this exampleThe dark/light mode toggle provides a seamless transition with a 0.3s animation, reducing abrupt changes and improving readability.The contrast adjustments in dark and light modes help users with visual impairments while reducing eye strain in different lighting conditions.The centralized layout, responsive button, and clear typography ensure an intuitive and engaging experience for all users.Implement Dark Mode in Websites - JavaScript codeThis JavaScript enables dark/light mode toggling by dynamically switching classes and updating the button text using classList.replace(). JavaScript const toggleButton = document.getElementById('theme-toggle'); const body = document.body; body.classList.add('light-mode'); toggleButton.addEventListener('click', () => { if (body.classList.contains('light-mode')) { body.classList.replace('light-mode', 'dark-mode'); toggleButton.textContent = 'Switch to Light Mode'; } else { body.classList.replace('dark-mode', 'light-mode'); toggleButton.textContent = 'Switch to Dark Mode'; } }); In this exampleThe script starts by selecting the toggle button and the body of the page.The light-mode class is added to the body to start with the light theme by default.An event listener is attached to the button to listen for clicks and toggle between light and dark modes.If the light-mode class is found on the body, it gets replaced with the dark-mode class, and the button text is updated accordingly.Similarly, when in dark mode, clicking the button switches back to light mode and updates the button text to reflect the change.Complete code HTML <html> <head> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; transition: background-color 0.3s, color 0.3s; } .container { text-align: center; padding: 30px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); max-width: 600px; background-color: white; } button { padding: 10px 20px; font-size: 16px; border: none; border-radius: 5px; cursor: pointer; background-color: #007bff; color: white; transition: background-color 0.3s; } button:hover { background-color: #0056b3; } body.light-mode { background-color: #f4f4f4; color: #333; } body.dark-mode { background-color: #333; color: brown; } h1 { margin-bottom: 20px; } p { font-size: 18px; line-height: 1.6; margin: 10px 0; } ul { text-align: left; padding-left: 20px; margin-top: 20px; } li { margin: 8px 0; } a { color: #007bff; text-decoration: none; } a:hover { text-decoration: underline; } </style> </head> <body> <div class="container"> <h1>Welcome to Dark/Light Mode Toggle</h1> <p> This page demonstrates a simple yet powerful way to switch between <strong>dark</strong> and <strong>light</strong> modes for a better user experience. </p> <p> Switching to dark mode can reduce eye strain, especially in low-light environments, while light mode is better for bright settings. </p> <button id="theme-toggle">Switch to Dark Mode</button> <h2>Benefits of Dark/Light Mode</h2> <ul> <li>Improves accessibility for users with visual impairments.</li> <li>Reduces eye strain during prolonged usage.</li> <li>Personalizes the user experience.</li> <li>Saves device battery life on OLED and AMOLED screens.</li> </ul> <p> Learn more about web accessibility and user experience on the <a href="https://www.w3.org/WAI/" target="_blank">W3C Accessibility Guidelines</a>. </p> </div> <script> const toggleButton = document.getElementById('theme-toggle'); const body = document.body; body.classList.add('light-mode'); toggleButton.addEventListener('click', () => { if (body.classList.contains('light-mode')) { body.classList.replace('light-mode', 'dark-mode'); toggleButton.textContent = 'Switch to Light Mode'; } else { body.classList.replace('dark-mode', 'light-mode'); toggleButton.textContent = 'Switch to Dark Mode'; } }); </script> </body> </html> How to Make Dark Mode for Websites using HTML CSS & JavaScript ? Comment More info S shantanujain18 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Projects Explore JavaScript BasicsIntroduction to JavaScript4 min readJavaScript Versions2 min readHow to Add JavaScript in HTML Document?3 min readJavaScript Syntax6 min readJavaScript Output4 min readJavaScript Comments2 min readVariables & DatatypesVariables and Datatypes in JavaScript6 min readGlobal and Local variables in JavaScript4 min readJavaScript Let6 min readJavaScript const5 min readJavaScript Var Statement7 min readOperatorsJavaScript Operators5 min readOperator precedence in JavaScript2 min readJavaScript Arithmetic Operators5 min readJavaScript Assignment Operators5 min readJavaScript Comparison Operators5 min readJavaScript Logical Operators5 min readJavaScript Bitwise Operators5 min readJavaScript Ternary Operator4 min readJavaScript Comma Operator2 min readJavaScript Unary Operators4 min readJavaScript in and instanceof operators3 min readJavaScript String Operators3 min readStatementsJavaScript Statements4 min readJavaScript if-else3 min readJavaScript switch Statement4 min readJavaScript Break Statement2 min readJavaScript Continue Statement1 min readJavaScript Return Statement4 min readLoopsJavaScript Loops3 min readJavaScript For Loop4 min readJavaScript While Loop3 min readJavaScript For In Loop3 min readJavaScript for...of Loop3 min readJavaScript do...while Loop4 min readPerformance & DebuggingJavaScript | Performance4 min readDebugging in JavaScript4 min readJavaScript Errors Throw and Try to Catch2 min readObjectsObjects in Javascript4 min readObject Oriented Programming in JavaScript3 min readJavaScript Objects6 min readCreating objects in JavaScript5 min readJavaScript JSON Objects3 min readJavaScript Object Reference4 min readFunctionsFunctions in JavaScript5 min readHow to write a function in JavaScript ?4 min readJavaScript Function Call2 min readDifferent ways of writing functions in JavaScript3 min readDifference between Methods and Functions in JavaScript3 min readExplain the Different Function States in JavaScript3 min readJavaScript Function Complete Reference3 min readArraysJavaScript Arrays7 min readJavaScript Array Methods7 min readBest-Known JavaScript Array Methods6 min readImportant Array Methods of JavaScript7 min readJavaScript Array Reference4 min readStringJavaScript Strings6 min readJavaScript String Methods9 min readJavaScript String Reference4 min read Like