{"id":7215,"date":"2024-09-20T06:36:57","date_gmt":"2024-09-20T06:36:57","guid":{"rendered":"https:\/\/impulsebyte.com\/blogs\/?p=7215"},"modified":"2024-11-06T06:14:06","modified_gmt":"2024-11-06T06:14:06","slug":"how-to-create-a-toggle-dark-mode-on-your-website-using-css-and-javascript","status":"publish","type":"post","link":"https:\/\/impulsebyte.com\/blogs\/web-development\/how-to-create-a-toggle-dark-mode-on-your-website-using-css-and-javascript\/","title":{"rendered":"How to Create a Toggle Dark Mode on Your Website Using CSS and JavaScript"},"content":{"rendered":"<p>Dark mode is becoming increasingly popular among users for its aesthetic appeal and reduced eye strain. In this blog post, we\u2019ll explore how to implement a toggle switch for dark mode on your website using CSS and JavaScript.<\/p>\n<h3>Step 1: Setting Up the HTML<\/h3>\n<p>First, we need to create a simple HTML structure. This will include a toggle button for switching between light and dark modes.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html lang=&quot;en&quot;&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;UTF-8&quot;&gt;\r\n&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\r\n&lt;title&gt;Dark Mode Toggle&lt;\/title&gt;\r\n&lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot;&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div class=&quot;container&quot;&gt;\r\n&lt;h1&gt;Toggle Dark Mode Example&lt;\/h1&gt;\r\n&lt;label class=&quot;switch&quot;&gt;\r\n&lt;input type=&quot;checkbox&quot; id=&quot;toggle&quot;&gt;\r\n&lt;span class=&quot;slider&quot;&gt;&lt;\/span&gt;\r\n&lt;\/label&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;script src=&quot;script.js&quot;&gt;&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Explanation:<\/strong><br \/>\n&#8211; We created a container with a heading and a label element that contains an input checkbox.<br \/>\n&#8211; The checkbox will act as our toggle switch for switching between light and dark modes.<\/p>\n<h3>Step 2: Adding CSS Styles<\/h3>\n<p>Now, let\u2019s style our page and create the dark mode styles. We\u2019ll also style the toggle switch.<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n\r\n\/* styles.css *\/\r\n\r\nbody {\r\nmargin: 0;\r\nfont-family: Arial, sans-serif;\r\ntransition: background-color 0.3s, color 0.3s;\r\n}\r\n\r\n.container {\r\ntext-align: center;\r\npadding: 50px;\r\n}\r\n\r\n.switch {\r\nposition: relative;\r\ndisplay: inline-block;\r\nwidth: 60px;\r\nheight: 34px;\r\n}\r\n\r\n.switch input {\r\nopacity: 0;\r\nwidth: 0;\r\nheight: 0;\r\n}\r\n\r\n.slider {\r\nposition: absolute;\r\ncursor: pointer;\r\ntop: 0;\r\nleft: 0;\r\nright: 0;\r\nbottom: 0;\r\nbackground-color: #ccc;\r\ntransition: .4s;\r\nborder-radius: 34px;\r\n}\r\n\r\n.slider:before {\r\nposition: absolute;\r\ncontent: &quot;&quot;;\r\nheight: 26px;\r\nwidth: 26px;\r\nleft: 4px;\r\nbottom: 4px;\r\nbackground-color: white;\r\ntransition: .4s;\r\nborder-radius: 50%;\r\n}\r\n\r\ninput:checked + .slider {\r\nbackground-color: #66bb6a;\r\n}\r\n\r\ninput:checked + .slider:before {\r\ntransform: translateX(26px);\r\n}\r\n\r\n\/* Dark Mode Styles *\/\r\nbody.dark-mode {\r\nbackground-color: #121212;\r\ncolor: #ffffff;\r\n}\r\n<\/pre>\n<p><strong>Explanation:<\/strong><br \/>\n&#8211; The `body` styles include a smooth transition for background color and text color.<br \/>\n&#8211; We created a toggle switch using CSS with a slider effect. The `.slider` class represents the visual part of the toggle.<br \/>\n&#8211; When the checkbox is checked, the background color of the slider changes, and the toggle moves to the right.<br \/>\n&#8211; The `.dark-mode` class changes the background color and text color when dark mode is enabled.<\/p>\n<h3>Step 3: Implementing JavaScript for Toggle Functionality<\/h3>\n<p>Now, let\u2019s add the JavaScript that will handle the toggle functionality.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n\/\/ script.js\r\n\r\nconst toggle = document.getElementById('toggle');\r\n\r\ntoggle.addEventListener('change', () =&gt; {\r\ndocument.body.classList.toggle('dark-mode');\r\n});\r\n<\/pre>\n<p><strong>Explanation:<\/strong><br \/>\n&#8211; We select the toggle checkbox using `document.getElementById`.<br \/>\n&#8211; We add an event listener for the `change` event. When the checkbox is toggled, we toggle the `dark-mode` class on the `body` element.<\/p>\n<h3>Step 4: Testing the Dark Mode Toggle<\/h3>\n<p>Now that we have completed the HTML, CSS, and JavaScript, it\u2019s time to test our dark mode toggle. Open your HTML file in a web browser, and you should see a toggle switch. When you click it, the website should switch between light and dark modes smoothly.<\/p>\n<h3>Step 5: Optional &#8211; Saving User Preference<\/h3>\n<p>To enhance the user experience, you might want to save the user&#8217;s preference for dark mode in `localStorage`. This way, the selected mode will persist even after the page is refreshed.<\/p>\n<p>Update your JavaScript as follows:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n\/\/ script.js\r\n\r\nconst toggle = document.getElementById('toggle');\r\n\r\n\/\/ Check for saved user preference in localStorage\r\nif (localStorage.getItem('dark-mode') === 'enabled') {\r\ndocument.body.classList.add('dark-mode');\r\ntoggle.checked = true; \/\/ Set the toggle to checked\r\n}\r\n\r\n\/\/ Toggle dark mode and save preference\r\ntoggle.addEventListener('change', () =&gt; {\r\ndocument.body.classList.toggle('dark-mode');\r\n\r\n\/\/ Save the user's preference\r\nif (document.body.classList.contains('dark-mode')) {\r\nlocalStorage.setItem('dark-mode', 'enabled');\r\n} else {\r\nlocalStorage.removeItem('dark-mode');\r\n}\r\n});\r\n<\/pre>\n<p><strong>Explanation:<\/strong><br \/>\n&#8211; We check if the user has previously enabled dark mode by retrieving the value from `localStorage`.<br \/>\n&#8211; If dark mode is enabled, we add the `dark-mode` class to the body and check the toggle.<br \/>\n&#8211; When the toggle is switched, we save the user&#8217;s preference in localStorage.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>Congratulations! You have successfully implemented a toggle for dark mode on your website using CSS and JavaScript. This feature not only enhances the user experience but also allows users to customize their browsing environment. Feel free to expand upon this basic implementation by adding more styles or functionalities!<\/p>\n<p>Now you can share this dark mode feature in your projects and make your websites more user-friendly. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dark mode is becoming increasingly popular among users for its aesthetic appeal and reduced eye strain. In this blog post, we\u2019ll explore how to implement a toggle switch for dark &hellip; <a title=\"How to Create a Toggle Dark Mode on Your Website Using CSS and JavaScript\" class=\"hm-read-more\" href=\"https:\/\/impulsebyte.com\/blogs\/web-development\/how-to-create-a-toggle-dark-mode-on-your-website-using-css-and-javascript\/\"><span class=\"screen-reader-text\">How to Create a Toggle Dark Mode on Your Website Using CSS and JavaScript<\/span>Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":7237,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[132,16,37],"tags":[],"class_list":["post-7215","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","category-shopify","category-wordpress"],"_links":{"self":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/comments?post=7215"}],"version-history":[{"count":7,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7215\/revisions"}],"predecessor-version":[{"id":7223,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7215\/revisions\/7223"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/media\/7237"}],"wp:attachment":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/media?parent=7215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/categories?post=7215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/tags?post=7215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}