Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025
  • GROUPING SETS, CUBE, and ROLLUP May 7, 2025

Creating a dark mode toggle with jQuery

Posted on March 27, 2025March 27, 2025 by Zubair Shaik

Loading

Creating a Dark Mode Toggle with jQuery: A Comprehensive Guide

Introduction

Dark mode is a popular feature in modern web design that enhances user experience by reducing eye strain and saving battery life on OLED and AMOLED screens. Implementing a dark mode toggle with jQuery allows users to switch between light and dark themes dynamically, making a website more accessible and user-friendly. In this guide, we will explore step-by-step instructions on how to implement a dark mode toggle using jQuery.


Table of Contents

  1. Understanding Dark Mode
  2. Benefits of Implementing Dark Mode
  3. Prerequisites
  4. Setting Up the HTML Structure
  5. Styling Light and Dark Modes with CSS
  6. Implementing the Dark Mode Toggle with jQuery
  7. Storing User Preferences with Local Storage
  8. Adding Animations for a Smooth Transition
  9. Enhancing Accessibility Features
  10. Testing and Debugging
  11. Optimizing Performance
  12. Best Practices for Dark Mode Implementation
  13. Conclusion

1. Understanding Dark Mode

Dark mode refers to a UI design style that uses a dark color palette for the background while keeping the text and UI elements in lighter colors. Many operating systems, applications, and websites support dark mode because of its usability benefits.

How Dark Mode Works

  • The website switches between two themes: Light Mode (default) and Dark Mode.
  • The user selects their preferred mode using a toggle button.
  • The selected theme is stored using Local Storage so that it persists even after the page is refreshed.
  • CSS is used to define styles for both modes, and jQuery dynamically switches between them.

2. Benefits of Implementing Dark Mode

User Experience

  • Reduces eye strain, especially in low-light conditions.
  • Enhances readability with higher contrast.

Battery Efficiency

  • OLED and AMOLED screens consume less power in dark mode.

Aesthetic Appeal

  • Gives the website a modern look and feel.
  • Users can choose the theme that suits them best.

3. Prerequisites

Before we begin, ensure you have the following:

  • Basic knowledge of HTML, CSS, and jQuery.
  • A text editor (e.g., VS Code, Sublime Text).
  • A web browser to test your code.

Including jQuery in Your Project

To use jQuery, you need to include it in your HTML file. You can add it via a CDN:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

4. Setting Up the HTML Structure

Create a simple HTML structure with a toggle button to switch between light and dark modes.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dark Mode Toggle with jQuery</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>

    <div class="container">
        <h1>Dark Mode Toggle Example</h1>
        <button id="dark-mode-toggle">Toggle Dark Mode</button>
        <p>This is an example of implementing dark mode using jQuery.</p>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="script.js"></script>

</body>
</html>

5. Styling Light and Dark Modes with CSS

We define the styles for both light and dark modes.

/* Default Light Mode */
body {
    background-color: #ffffff;
    color: #000000;
    font-family: Arial, sans-serif;
    text-align: center;
    transition: background-color 0.5s, color 0.5s;
}

.container {
    margin-top: 50px;
}

button {
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
}

/* Dark Mode */
.dark-mode {
    background-color: #121212;
    color: #ffffff;
}

6. Implementing the Dark Mode Toggle with jQuery

Now, let’s write the jQuery script to toggle dark mode.

$(document).ready(function() {
    // Check local storage for saved dark mode preference
    if (localStorage.getItem("darkMode") === "enabled") {
        $("body").addClass("dark-mode");
    }

    // Click event for the toggle button
    $("#dark-mode-toggle").click(function() {
        $("body").toggleClass("dark-mode");

        // Store the user's preference in local storage
        if ($("body").hasClass("dark-mode")) {
            localStorage.setItem("darkMode", "enabled");
        } else {
            localStorage.setItem("darkMode", "disabled");
        }
    });
});

7. Storing User Preferences with Local Storage

The above script saves the user’s theme preference using the localStorage API. This ensures that when the page reloads, the preferred mode is retained.

How Local Storage Works

  • localStorage.setItem("key", "value") stores data.
  • localStorage.getItem("key") retrieves data.
  • localStorage.removeItem("key") deletes stored data.

8. Adding Animations for a Smooth Transition

We can enhance the transition effect for better UX.

Modify the CSS:

body {
    transition: background-color 0.5s ease-in-out, color 0.5s ease-in-out;
}

9. Enhancing Accessibility Features

For accessibility improvements:

  • Ensure the toggle button is accessible via keyboard navigation.
  • Use ARIA attributes for screen readers.

Modify the button:

<button id="dark-mode-toggle" aria-label="Toggle Dark Mode">Toggle Dark Mode</button>

10. Testing and Debugging

After implementing the dark mode toggle:

  1. Test in different browsers (Chrome, Firefox, Edge).
  2. Check Local Storage using Developer Tools (F12 > Application > Local Storage).
  3. Ensure button functionality by clicking it multiple times.

11. Optimizing Performance

  • Use CSS classes instead of modifying individual properties.
  • Keep jQuery operations minimal to avoid performance bottlenecks.
  • Use event delegation if applying to multiple elements dynamically.

12. Best Practices for Dark Mode Implementation

  • Provide manual and automatic toggling options.
  • Allow users to follow system preferences using prefers-color-scheme.
  • Ensure high contrast for readability.

Implementing a dark mode toggle with jQuery is simple yet powerful. By using jQuery, CSS, and Local Storage, we can create an efficient dark mode switch that enhances UX and accessibility. This guide covered:

  • HTML structure
  • Styling light and dark modes
  • Writing the jQuery toggle script
  • Storing user preferences
  • Adding animations and accessibility features

Try extending this project by:

  • Using icons instead of text for toggling.
  • Allowing dark mode activation based on system settings.
  • Applying dark mode to specific sections instead of the whole page.

Dark mode is a feature that users appreciate, and adding it improves the overall experience of a website.


Complete Source 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 Toggle</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>

    <div class="container">
        <h1>Dark Mode Toggle Example</h1>
        <button id="dark-mode-toggle">Toggle Dark Mode</button>
        <p>Click the button to switch between light and dark mode.</p>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="script.js"></script>

</body>
</html>

This guide provides everything you need to create a fully functional dark mode toggle with jQuery. Happy coding!

Posted Under jQuery`UX/UI design` accessibility in dark mode cross-browser compatibility CSS transitions Dark Mode dark mode best practices dark mode CSS dark mode for mobile dark mode for websites dark mode implementation dark mode persistence dark mode tutorial dark mode user experience dark mode with jQuery dynamic UI with jQuery enable dark mode frontend development frontend performance optimization improve user experience interactive web design JavaScript local storage JavaScript theme switcher jQuery Animation jQuery button click jQuery dark mode script jQuery dark mode toggle jQuery dark theme jQuery effects jQuery event handling jQuery theme switcher jQuery UI design jQuery UI enhancements light mode vs dark mode local storage theme modern UI trends modern web design night mode with jQuery responsive dark mode save user settings smooth dark mode transition toggle dark mode button user preference theme user-friendly UI Web Accessibility Web Development Website Customization website dark mode

Post navigation

Inserting new rows into a table
Creating nested lists dynamically

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST
  • Dynamic SQL Execution with sp_executesql

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions