the-geeky-codes-high-resolution-logo-color-on-transparent-background geeky code red logo
  • Home
  • AI
    AIShow More
    generate vector icons
    Generate Vector Icons with ChatGPT DALLE 3: A Comprehensive Guide
    14 Min Read
    Dalle 3
    Dalle 3: A Step-by-Step Guide to Mastering AI Art Generation
    4 Min Read
    5 Best AI Tools to Convert Images to Video Animations
    5 Best AI Tools to Convert Images to Video Animations
    8 Min Read
    Exploring the Impressive Mistral 7B Model
    Exploring the Impressive Mistral 7B Model for Text Summarization and Coding
    6 Min Read
    The AI Revolution this week
    Must Read – The AI Revolution this week 30 Sep 2023: Integrating AI Tools into Everyday Life
    6 Min Read
  • Tutorial
    • React js
    • Python
    • Javascript
  • AI Tools
Reading: Implementing Dark Mode with JavaScript
Share
the geeky codesthe geeky codes
Aa
  • AI
  • AI Tools
  • Javascript
  • Python
  • React js
  • Advertise
Search
  • Categories
    • AI
    • AI Tools
    • Javascript
    • Python
    • React js
  • More
    • Advertise
Follow US
Copyright ©2023 The Geeky codes. All Rights Reserved.
the geeky codes > Blog > Tutorial > Javascript > Implementing Dark Mode with JavaScript
TutorialJavascript

Implementing Dark Mode with JavaScript

thegeekycodes By thegeekycodes 9 October 2023 3 Min Read
Implementing Dark Mode with JavaScript
SHARE

Introduction: Dark Mode with JavaScript

Dark mode has become a popular feature in many web applications and websites due to its eye-friendly nature and sleek appearance. It helps to reduce eye strain, especially under low light conditions, and can also save battery life on OLED screens. Implementing dark mode in your web application can significantly enhance the user experience. In this article, we will explore how to create a simple dark mode toggle using JavaScript.

Contents
Introduction: Dark Mode with JavaScriptSetting Up the HTML and CSSCreating the JavaScript FileExplanationAdding CSS:💁 Check out our other articles😃
Dark Mode with JavaScript

Setting Up the HTML and CSS

Firstly, let’s set up the HTML structure and some basic CSS for our web application:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Dark Mode Example</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <header>
            <h1>Embracing the Dark Side: Dark Mode with JavaScript</h1>
        </header>
        <button id="toggle-button">Toggle Dark Mode</button>
        <p>
            Dark mode has grown in popularity as it provides a visually soothing
            user experience, especially under low-light conditions. It also
            tends to save battery life on OLED and AMOLED screens, making it a
            desirable feature for modern web applications.
        </p>
        <script src="script.js"></script>
    </body>
</html>

Creating the JavaScript File

Now, create a new file named script.js and add the following code to toggle the dark mode on and off:

const toggleButton = document.getElementById('toggle-button');

toggleButton.addEventListener('click', () => {
  document.body.classList.toggle('dark-mode');
});

Explanation

In the above JavaScript code:

  1. We first get a reference to the toggle button using document.getElementById.
  2. We then set up an event listener on the button for the click event.
  3. Inside the event handler, we use document.body.classList.toggle to toggle the dark-mode class on the body element.

Adding CSS:

body {
    transition: background-color 0.5s ease, color 0.5s ease;
    color: #000000; /* Default text color is black */
}
.dark-mode {
    background-color: #121212;
    color: #ffffff; /* Text color in dark mode is white */
}
h1 {
    transition: color 0.5s ease;
}
.dark-mode h1 {
    color: #ff5722; /* Example color for heading in dark mode */
}
p {
    transition: color 0.5s ease;
}
.dark-mode p {
    color: #8bc34a; /* Example color for paragraph text in dark mode */
}

With these simple steps, we have created a basic dark mode toggle for our web application. Now, whenever the user clicks the “Toggle Dark Mode” button, the background color of the page will switch between the default white and dark gray, providing a basic dark mode functionality.

💁 Check out our other articles😃

 👉  Generate a free Developer Portfolio website with AI prompts

 👉  Build a Simple Calculator App with React, Tailwind CSS. Displays History

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Copy Link Print
Previous Article supademo Supademo: Revolutionize Your Customer Onboarding with Interactive Demos
Next Article timeos timeOS: Personalized AI for Efficient Meeting Notes & Scheduling
Leave a comment

Leave a Reply Cancel reply

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

Twitter Follow
Telegram Follow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!

Most Popular
Advanced Routing Techniques in Nextjs 15
Advanced Routing Techniques in Next js 15
20 November 2024
Attachment Details Image-to-Text-Converter-with-Claude-Nextjs-15
Building an AI-Powered Image-to-Text Converter with Claude, Next.js 15, and Vercel AI SDK
20 November 2024
Generate-Dynamic-OpenGraph-Images-in-Nextjs15
How to Generate Dynamic OpenGraph Images in Next.js App Router 15 with TypeScript
20 November 2024
Google Analytics 4 in Nextjs 14
How to Install Google Analytics 4 in Next.js 15 (App Router) with TypeScript [2024]
20 November 2024
docker compose
Getting Started with Docker Compose
20 November 2024

You Might Also Like

Advanced Routing Techniques in Nextjs 15
TutorialNextjs

Advanced Routing Techniques in Next js 15

7 Min Read
Attachment Details Image-to-Text-Converter-with-Claude-Nextjs-15
TutorialNextjs

Building an AI-Powered Image-to-Text Converter with Claude, Next.js 15, and Vercel AI SDK

4 Min Read
Generate-Dynamic-OpenGraph-Images-in-Nextjs15
TutorialNextjs

How to Generate Dynamic OpenGraph Images in Next.js App Router 15 with TypeScript

9 Min Read
Google Analytics 4 in Nextjs 14
TutorialNextjs

How to Install Google Analytics 4 in Next.js 15 (App Router) with TypeScript [2024]

6 Min Read

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!

the geeky codes geeky code red logo

Providing valuable resources for developers in the form of code snippets, software tutorials, and AI related content.

About

  • About Us
  • Contact
  • Terms and Conditions
  • Privacy Policy
  • Disclaimer
  • Affiliate Disclosure

Resource

  • The Art of AI Prompt Engineering: Crafting Effective Inputs for AI Models

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
© 2023 The Geeky Codes. All Rights Reserved
We are happy to see you join Us!

🔥📢Subscribe to our newsletter and never miss our latest code snippets, tutorials and AI updates

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?