<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Md Abdul Awal Sajib</title>
    <description>The latest articles on DEV Community by Md Abdul Awal Sajib (@mdsajib201).</description>
    <link>https://dev.to/mdsajib201</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2286646%2F771eacd9-d06d-4d51-9532-6ba3be90f5f5.png</url>
      <title>DEV Community: Md Abdul Awal Sajib</title>
      <link>https://dev.to/mdsajib201</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mdsajib201"/>
    <language>en</language>
    <item>
      <title>Add a Simple Dark Mode Toggle with HTML, CSS, and JavaScript</title>
      <dc:creator>Md Abdul Awal Sajib</dc:creator>
      <pubDate>Thu, 14 Nov 2024 14:40:29 +0000</pubDate>
      <link>https://dev.to/mdsajib201/add-a-simple-dark-mode-toggle-with-html-css-and-javascript-42jk</link>
      <guid>https://dev.to/mdsajib201/add-a-simple-dark-mode-toggle-with-html-css-and-javascript-42jk</guid>
      <description>&lt;h2&gt;
  
  
  Quick Guide to Building a Simple Dark Mode Feature
&lt;/h2&gt;

&lt;p&gt;Follow these steps to create a basic dark mode toggle switch using HTML, CSS, and JavaScript. This implementation also saves the user’s theme preference with localStorage, so the dark mode persists across sessions.&lt;/p&gt;

&lt;p&gt;preview:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs71h38cdcz7tv318vdid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs71h38cdcz7tv318vdid.png" alt="Image description" width="800" height="128"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Step1: create index.html . you can copy the html file from my github
&lt;/h4&gt;

&lt;p&gt;. Start by setting up a simple HTML structure. You can also find this code on my GitHub. Here’s the main toggle button:&lt;/p&gt;

&lt;p&gt;index.html&lt;br&gt;
`&lt;/p&gt;
&lt;br&gt;
     &lt;br&gt;
        &lt;br&gt;
    
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="./script.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;`&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 2: Style the Dark Mode Toggle with CSS
&lt;/h4&gt;

&lt;p&gt;Define the base styles for the toggle switch and the circle indicator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#dark{

display: inline-block;

width: 50px;

border: 1px  solid  #f0cff0;

border-radius: 25px;

}

#circle{

width: 20px;

height: 20px;

border-radius: 100%;

background-color: #ffcc00;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, create additional classes to toggle the circle position and apply&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dark mode styling:
.toggle{
 margin-left: 50%;

}

.dark_mode{
 background: #252526  !important;
 color: #fff;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 3: JavaScript for Theme Persistence
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const  circle  =  document.getElementById("circle");
const  dark_button=  document.getElementById("dark")
const  body  =  document.getElementById("_body")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Step 4: Understand the Theme Handling Logic&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here if theme key/variable === dark we’ll run darkMode function other wise we’ll execute lightMode function&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Step 5: Create the Dark and Light Mode Functions
&lt;/h4&gt;

&lt;p&gt;Define the functions to add or remove dark mode styling based on the theme setting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const  theme  =  localStorage.getItem("theme")
theme  ===  "dark"  ?  darkMode() :  lightMode();

 function darkMode() {

     body.classList.add("dark_mode")
     circle.classList.add("toggle")
     circle.style.background = "#E2EAF4";

     localStorage.setItem("theme", "dark")
}

function lightMode() {

  body.classList.remove("dark_mode");
  circle.classList.remove("toggle")
  circle.style.background = "#524b38";
  localStorage.setItem("theme", "light")

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 6: Add Toggle Functionality to the Button
&lt;/h4&gt;

&lt;p&gt;Finally, set up an event listener on the button to switch themes and update localStorage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dark_button.addEventListener("click", () =&amp;gt; {

circle.classList.toggle("toggle");

if (circle.classList.contains("toggle")) {

localStorage.setItem("theme", "dark")

}else{

localStorage.setItem("theme", "light")

}

  const  theme  =  localStorage.getItem("theme")

theme  ===  "dark"  ?  darkMode() :  lightMode()

})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this guide helps! Icon Follow me on &lt;a href="https://www.facebook.com/mdsajib201/" rel="noopener noreferrer"&gt;Facebook:&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
