Create a Dark/Light Mode Switch with HTML, CSS and Jquery (2024)

Creating a toggle button for dark and light mode is easy with our guide for beginner web developers. Learn how to implement it using HTML, CSS, and jQuery.

Create a Dark/Light Mode Switch with HTML, CSS and Jquery (1)

Read AlsoStep-by-step Guide: Creating a Customizable Star Rating Concept using HTML and CSS

Table of Contents

  1. Project Introduction
  2. HTML Code
  3. CSS Code
  4. JavaScript Code
  5. Preview
  6. Conclusion

Before tackling this topic, I would like to tell you why a dark mode isnecessary on your website. Dark mode became popular in the last 3-5 years ascompanies started rolling out these modes to benefit night users and those withbright screens. You know, dark mode is everywhere on iOS, Android, Windows. Ifit's everywhere, that doesn't mean you should have one, so why does it matter.let's say if a user visits your website at night to read an article, lightercolors will strain the user's eyes. so I think the dark mode is an essentialfeature for every website.

Here are the benefits of using a dark theme:

  • Improve text readability
  • Better Contrast
  • Reduce eye strain
  • Less flickering
  • Less blue light
  • Less likely to cause photophobia
  • Can save a small amount of electricity

A dark/light mode switch allows website visitors to choose between a dark and light theme for the website. In this tutorial, we'll guide you through the process of creating a dark/light mode switch for your website using HTML, CSS, and jQuery. Not only is a dark/light mode switch a popular design trend, but it also provides users with a better browsing experience, especially when browsing at night or in low-light environments.

Join My Telegram Channel to Download the Project: Click Here

Prerequisites:

Before starting this tutorial, you should have a basic understanding of HTML, CSS, and JavaScript. Additionally, you will need a code editor such as Visual Studio Code or Sublime Text to write and save your code.

Source Code

Step 1 (HTML Code):

To get started, we will first need to create a basic HTML file. In this file, I'm going to provide you with a simple markup element that you can style however you like.

Our demo also uses jQuery. In this example, we'll add the script to the HTML file directly from the Cloudflare CDN so it's always up to date. Be careful to add the jQuery library before the custom script so it can use its functionality.

After creating the files just paste the following codes into your file. Make sure to save your HTML document with a .html extension, so that it can be properly viewed in a web browser.

This is the basic structure of our dark/light mode switch using HTML, and now we can move on to styling it using CSS.

Read AlsoResponsive Gym Website Design | Fitness Center Template

Step 2 (CSS Code):

Once the basic HTML structure of the website is in place, the next step is to add styling to the website using CSS. CSS allows us to control the visual appearance of the website, including things like layout, color, and typography.

Next, create the CSS for the light mode and dark mode. The CSS below uses a column-based flexbox layout that allows you to easily position elements on the page, especially the .switch class that handles the dark mode toggle wrapper and img elements. Create a CSS file with the name of styles.css and paste the given codes in your CSS file.

body { font-family: sans-serif; font-size: 1.125rem; display: flex; flex-direction: column; max-width: 50rem; margin: 0 auto; padding: 0 0.9375rem; }small { font-style: italic;}article { display: flex; flex-direction: column;}img { max-width: 100%; display: block; align-self: center;}.switch { align-self: flex-end; margin: 0.9375rem;}.inner-switch { display: inline-block; cursor: pointer; border: 1px solid #555; border-radius: 1.25rem; width: 3.125rem; text-align: center; font-size: 1rem; padding: 0.1875rem; margin-left: 0.3125rem;}.dark,.dark * { background-color: #222; color: #e6e6e6; border-color: #e6e6e6;} 

Step 3 (JavaScript Code):

Finally, the script.js file adds toggle functionality to the toggle button. So when the user clicks the toggle button, the dark mode will be applied and the label on the button will change to "ON". And, if the user clicks the toggle when the page is in dark mode, the light mode will be applied and the label will change to "OFF".

Create a JavaScript file with the name of script.js and paste the given codes into your JavaScript file and make sure it's linked properly to your HTML document, so that the scripts are executed on the page. Remember, you’ve to create a file with .js extension.

$( ".inner-switch" ).on("click", function() { if( $( "body" ).hasClass( "dark" )) { $( "body" ).removeClass( "dark" ); $( ".inner-switch" ).text( "OFF" ); } else { $( "body" ).addClass( "dark" ); $( ".inner-switch" ).text( "ON" ); }});

Final Output:

Create a Dark/Light Mode Switch with HTML, CSS and Jquery (4)

Read AlsoCreate a Responsive Bootstrap Registration Form (Source Code)

Conclusion:

Congratulations, you've now learned how to create a dark/light mode switch using HTML, CSS, and jQuery. Not only does this switch make your website look more modern and stylish, but it also provides a better user experience for visitors. By following the steps outlined in this tutorial, you can easily implement this switch on your own website and make it more user-friendly for everyone.

That’s a wrap!

I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

Tags: dark mode, light mode, switch, html, css, jquery, how to create a dark mode switch on website, implementing light mode switch using jquery, html and css for dark/light mode switch, creating a toggle button for dark and light mode, adding a switch button to your website

End of the article

Create a Dark/Light Mode Switch with HTML, CSS and Jquery (2024)

FAQs

How do I change dark and light mode in HTML CSS? ›

It uses 2 functions namely: darkMode() and lightMode() to switch between the two modes. Output: Example 2: In the above example, we have used two buttons to switch between Dark and light modes. Now we will use the toggle() method in JavaScript to toggle between dark and light modes.

How to make a dark mode in jQuery? ›

Steps to Create Dark/Light Mode
  1. Create an HTML Document.
  2. Create CSS for the document file as well as for dark mode.
  3. Add a switch/toggler to toggle between light and dark modes.
  4. Add functionality to the switch/toggler to toggle between light and dark mode using JavaScript or jQuery code.
May 14, 2024

How do you make a dark theme switch? ›

From the Home menu, head to 'System Settings' which can be found underneath the game tiles and resembles a cog wheel icon. Tap the A button, and scroll down the list of options on the left hand side of the screen. From there, click on 'Themes' and select 'Basic Black' by pressing the A button again.

How to make a theme changer in JavaScript? ›

"Change to light theme" : "Change to dark theme"; button. innerText = newCta; // use an aria-label if you are omitting text on the button // and using sun/moon icons, for example button. setAttribute("aria-label", newCta); // update theme attribute on HTML to switch theme in CSS document.

What is the color code for light dark in CSS? ›

The light-dark() function enables providing two color values where any <color> value is accepted. The light-dark() CSS color function returns the first value if the user's preference is set to light or if no preference is set and the second value if the user's preference is set to dark .

How to change from dark mode to light mode? ›

Select Start > Settings . Select Personalization > Colors. In the list for Choose your mode, select Custom. In the list for Choose your default Windows mode, select Light or Dark.

How to turn dark mode on switch? ›

From the HOME Menu, select System Settings. Scroll down the options on the left and select Theme. Select the desired background theme.

Is there light or dark mode on switch? ›

Mobile (Android):

From the app menu, tap Settings. Scroll down to the header which reads Display, and tap Display Theme. Pick from System Default, Light, or Dark.

How to change theme color using CSS? ›

To opt the entire page into the user's color scheme preferences, declare color-scheme on the :root element. To opt in specific elements to the user's color scheme preferences, declare color-scheme on those elements.

How to make a black background in HTML? ›

The first and simplest way to change the background color is by using inline CSS, which appears in the HTML code itself. To use inline CSS, find the opening tag of the element you want to target, then add the attribute style=“background-color: yourcolorhere;”.

How to change color in HTML using CSS? ›

Simply add the appropriate CSS selector and define the color property with the value you want. For example, say you want to change the color of all paragraphs on your site to navy. Then you'd add p {color: #000080; } to the head section of your HTML file.

How do I change the brightness in HTML CSS? ›

brightness() The brightness() CSS <filter-function> applies a linear multiplier value on an element or an input image, making the image appear brighter or darker.

How to change background color in HTML using internal CSS? ›

Setting the background color using inline CSS involves adding the style attribute to the HTML element and specifying the background-color property with a color value, directly within the element's opening tag.

How do I change CSS color dynamically? ›

There are several different ways that you can add and manipulate dynamic colors with CSS, in this article, we'll explore a few:
  1. Using transparency. Relative colors.
  2. Using the calc() function.
  3. Filter percentage value.
  4. Additional methods in SASS and JavaScript.
Nov 16, 2022

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5712

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.