Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Darkmode On

gebi65

New Coder
Hello everyone,
I've been doing web design for a while.
My skills are still very limited.

But I thank you in advance that I have the opportunity to ask for help here and, if possible, to offer help.

I would like to add a "dark mode" to my website. Unfortunately, I can only find themes, templates, for Wordpress, etc. on Google. However, I can't find how I can do this myself.
I want the site to recognize when the OS is in dark mode, activate the dark mode of the website, or if the user activates it, the site remembers that. How do you usually approach this topic?

My way, which doesn't work as intended:
(Time delay until the javascript sets the right class)
(detection prefer-color-scheme dark is not implemented yet)

CSS:

CSS:
:root {
  --bg-color-body: rgb(255, 255, 255);
  --color-font: #333333;
  --color-bg-header: white;
   
.dark_mode {
  --bg-color-body: rgb(68, 68, 68);
  --color-font: rgb(202, 202, 202);
  --color-bg-header: grey;

HTML:

HTML:
<body class="dark_mode">
   
</body>


Javascript:

JavaScript:
window.onload = function ismatch() {
    let cookie_string = document.cookie;
   // alert(cookie_string);
    let cookie_test = cookie_string.search("light");
    if (cookie_test >= 0) {
        document.getElementById('nachtmodus').checked = true;
        document.body.classList.toggle("dark_mode");
    }
    else {
        document.getElementById('nachtmodus').checked = false;
    }
}

function darkmode() {
    a = document.getElementById('nachtmodus');
    if(a.checked) {
    document.getElementById('daynight').style.display ="none"; } else {
        document.getElementById('daynight').style.display ="inline";
    }
    a = document.getElementById('nachtmodus');
    if (a.checked) {
        document.body.classList.toggle("dark_mode");
        document.cookie = "style=light; path=/";
    }

      if (!a.checked) {
        document.body.classList.toggle("light");
        document.cookie = "style=dark; path=/";
    }

}

Thx!
Gebi65

And sry for my very bad english ;)
 
Last edited:
I only program color modes once a long time ago. The user had many choices of themes to use and I didn't have a timing problem.
I used cookies then. Now you also have a choice of internal memory. And if you're still having a slow timing problem think about PHP to render the page.
First CSS:
Code:
<style>
body {
  background-color: white
  color: black;
}

.dark-mode {
  background-color: black;
  color: white;
}
</style>
Just use <body> No class assigned.

After you retrieve the mode do something like this:
Code:
if(mode == 'dark')  document.body.classList.toggle("dark-mode")
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom