Certain hues remain unaffected when changing themes, exclusive to Chrome

I have been working on a project that includes a toggle feature to switch between different themes. However, I am encountering an issue where the theme does not update properly when changing from a dark theme to a light theme or vice versa. The colors remain unchanged until I hover over the elements.

This problem seems to be specific to Chrome, as it does not occur in Chrome's incognito mode or in Firefox!

To see the Live Preview, visit:

You can also access the GitHub Repository here: https://github.com/SadeghRastgoo/Axies-NFT-Market-Place

_colors.scss:

:root {
  --primary-text-color: #fdfdfd;
  --bg-color: #14141f;
  ...

script.js:

document.querySelector(".theme-light").addEventListener("click", function () {
  document.querySelector("body").classList.toggle("light-colors");
});
document.querySelector(".theme-dark").addEventListener("click", function () {
  document.querySelector("body").classList.toggle("light-colors");
});

Answer №1

I implemented a similar functionality on my own website but with a different approach, here is how I approached it, hoping that it might be helpful for you as well

// The default theme is the one stored in local storage or dark theme if nothing is found
changeTheme(localStorage.theme || "dark");

let themes = {
    light: {
      "main-bg-color": "#F5F5F5",
      "card-bg-color": "#08D9D6",
      "font": "#252A34",
    },
    dark: {
      "main-bg-color": "#23252e",
      "card-bg-color": "#1c1e25",
      "font": "#fff",
    },
};

// Function to toggle between themes
function changeTheme(theme) {
    localStorage.setItem("theme", theme);
    switchCheckbox.checked = (theme === "light");

    for (let prop in themes[theme]) {
        // Setting custom properties for root element
        document.documentElement.style.setProperty("--" + prop, themes[theme][prop]);
    }
}

// Toggle switch functionality
switchCheckbox.addEventListener("change", () => {
    changeTheme(switchCheckbox.checked ? "light" : "dark");
});

Answer №2

The issue seems to be limited to Chrome and is not present in Chrome's incognito mode or Firefox!

It is possible that this problem is related to Google's cache. To troubleshoot, try turning off your browser's cache and then reloading the page. You can disable it by pressing ctrl+shift+j on your keyboard, accessing the Network section, and selecting Disable Cache. https://i.sstatic.net/o6BfY.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to track dynamic changes in window dimensions within Vue?

Working on my Vue mobile web app, I encountered an issue with hiding the footer when the soft keyboard appears. I've created a function to determine the window height-to-width ratio... showFooter(){ return h / w > 1.2 || h > 560; } ...and ...

Customize the AngularJS ng-grid filter to format the filter text

Currently, I am working with ng-grid in AngularJS (v2.0.8) and I am interested in exploring the syntax for the filterText field within the API. Specifically, I am looking to understand how to filter data based on certain columns and how to filter multiple ...

Encountered an issue locating the stylesheet file 'css/style.css' that is supposed to be linked from the template. This error occurred when trying to integrate Bootstrap with Angular

<link rel="stylesheet" href="plugins/bootstrap/bootstrap.min.css"> <link rel="stylesheet" href="plugins/owl-carousel/owl.carousel.css"> <link rel="stylesheet" href="plugins/magnific-pop ...

"Every time an Ajax call is successful, the 'else' clause in

When it comes to using Ajax for user login in the system, I encountered an issue where the Ajax success function would always run the else statement even if the server returned a true Boolean value. This meant that even when the login credentials were vali ...

How can I update the style of my array-bars using componentDidMount()?

I created a visualization tool for sorting algorithms that displays vertical bars with varying heights and sorts them. The "Generate new Array" button triggers a function to generate a new array each time it's clicked, which is also used in the compon ...

Separate each item in the list and display them across multiple pages within a table

I'm looking to display a list of 37 items across four separate pages within a table. Can anyone suggest a way to split these items and showcase them in four different pages using either javascript or vue.js? ...

Select the small image to reveal the larger overlay image

I have a grid of images and I want each image to expand into fullscreen when clicked. However, the JavaScript I am using is causing only the last image in the grid to overlay. How can I resolve this issue? Here is the code I am currently using: http://js ...

Animating a Bootstrap 4 card to the center of the screen

I am attempting to achieve the following effect: Display a grid of bootstrap 4 cards Upon clicking a button within a card, animate it by rotating 180 degrees, adjusting its height/width from 400px - 350px to the entire screen, and positioning it at the c ...

Delightful Bootstrap Tabs with Dynamic Content via Ajax

My website has a lot of tabs designed with Bootstrap. I wanted to make them responsive, so I tried using a plugin called Bootstrap Tabcollapse from https://github.com/flatlogic/bootstrap-tabcollapse (you can see a demo here: http://tabcollapse.okendoken.co ...

ngx-bootstrap encountered an issue: TypeError - _co.openModal is unavailable as a function

Just starting out with ngx-bootstrap, I was attempting to create a modal using the instructions from this site : However, I encountered the following error: ERROR TypeError: _co.openModal is not a function Here are some code snippets: //app.component ...

JavaScript scripts are unable to function within an Angular directive's template due to compatibility issues

I'm facing an issue with a directive (function (angular) { 'use strict'; function digest(factory) { var directive = { restrict: 'E', templateUrl: '/js/app/digest/templates/digest.tem ...

Navigating through a text field using the arrow keys

The arrow keys (left and right) are unable to navigate within a text field. Additionally, placing the cursor within a text field for editing is not possible as it jumps to the end every time a character is added. This issue is present in Chrome and IE, but ...

How can I align rectangles with different heights to display side by side using Javascript?

Currently, I am designing a press page for a website where the headlines/articles are displayed in rectangles. To achieve this layout, I am using the following CSS: .press-blocks{ column-count: 4; column-gap: 2em; padding-left: 10%; padding ...

The innerHTML of null cannot be set in Vue JS

I am attempting to showcase my array and its modifications after applying various methods <template> <div> <div id="example"></div> <div id="example1"></div> </div> </template> <s ...

Strikethrough text with a distinct color similar to that seen in email messages

In my endeavor to customize the line-through text decoration in red and change the text color to black or any other color for an email, I have experimented with various methods. These include wrapping the text in a span and setting its color to red, using ...

Tips for concealing the "maxlength" attribute in HTML

Here is the code snippet I currently use on a signup and login form to restrict users from entering more than a specified number of characters: $( "#limit1" ).attr('maxlength','11').on('input', function() { if ($(this).val(). ...

What methods can I use to display or conceal certain content based on the user's location?

I'm looking to display specific content exclusively to local users. While there are APIs available for this purpose, I'm not sure how to implement them. I'm interested in creating a feature similar to Google Ads, where ads are tailored base ...

What is the best way to display a list of lists in a React application?

I have a collection of lists containing data that I need to display on the view. The data: { "blocks_content": [ [ " Bakery", " Company GbR", ], [ ...

Using nodeJS's util module to format and pass an array

I've been using util.format to format strings like this: util.format('My name is %s %s', ['John', 'Smith']); However, the second parameter being an array ['John', 'Smith'] is causing issues because m ...

Grails: Exploring ways to make a database request for checking updates

Is there a way for me to query my MySQL database and determine if any changes have been made, so that I can automatically refresh a specific div element if there are updates? -UPDATE- Here is the current code I am using. It simply refreshes the load func ...