Style for dark mode

home | blog | Style for dark mode

I’m currently working on the dark and light mode versions of this page. Here’s how I’ve styled it – using local storage so that it remembers the user decision.

<script>
    const toggleBtn = document.getElementById('toggle-btn');
    const body = document.body;
    const isDarkMode = JSON.parse(localStorage.getItem('isDarkMode'));

    if (isDarkMode) {
      body.classList.add('dark-mode');
    }

    toggleBtn.addEventListener('click', function () {
      body.classList.toggle('dark-mode');
      localStorage.setItem('isDarkMode', body.classList.contains('dark-mode'));
    });
  </script>

CSS:

  /* Light Mode CSS */
  body {
    background-color: var(--light-background);
    color: var(--light-primary);
  }

body a {
    color: var(--dark-link);
  }

  /* Dark Mode CSS */
  body.dark-mode {
    background-color: var(--dark-background);
    color: var(--dark-primary);
  }

  body.dark-mode a {
    color: var(--dark-link);
  }
By on Aug 24th, 2023 in Code

One response to “Style for dark mode”

  1. Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

  2. car_fcMa says:
    Your comment is awaiting moderation.

    Locate the closest car rental services, Save time and money on your rental today, Choose from a variety of rental car options, Book a rental car with just a few clicks.

    mitsubishi rvr rental https://www.best-carrental.com/our-cars/?view=cardetails&carid=13 .

Leave a Reply

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

Continue reading »