As someone who's recently added a light/dark mode toggle to my site, I'm struck with the issue plaguing many a site: dark mode flickering. But for whatever reason, only my index page flickers upon a reload.
See what (index)
I mean? (my sitemap. But any other page works for this example.)
To witness the bug in action, you need to click on the sun icon, change the colour to purple and then save beforehand. Otherwise it won't persist.
The worst thing is, I think I know what's causing it, too. I just don't know how to fix it.
In my css file, a generic :root is defined so the site displays its light mode colours by default. The flickering goes away if I remove it, but then it breaks my site until one of the radiobuttons is checked. And I can't just make one of the buttons checked by default, because then my save button breaks.
:root {
/* Background Colors: */
--background-color: #ff6a6a;
--content-background-color: #ffc0c0;
--sidebar-background-color: #1bb6a4;
/* Text Colors: */
--text-color: #000000;
--sidebar-text-color: #000000;
--link-color: #00565f;
--link-color-hover: #80bfb8;
--header-color: black;
--other-details-colour: black;
/* Text: */
--font: Open Sans, sans-serif;
--heading-font: Chelsea Market, sans-serif;
--font-size: 16px;
/* Other Settings: */
--margin: 10px;
--padding: 20px;
--border: 2px solid #083630;
--round-borders: 10px;
--sidebar-width: 300px;
}
/* DARK/LIGHT MODE STUFF */
:root:has(#light:checked) {
/* Background Colors: */
--background-color: #ff6a6a;
--content-background-color: #ffc0c0;
--sidebar-background-color: #1bb6a4;
/* Text Colors: */
--text-color: #000000;
--sidebar-text-color: #000000;
--link-color: #00565f;
--link-color-hover: #80bfb8;
--header-color: black;
--other-details-colour: black;
/* Text: */
--font: Open Sans, sans-serif;
--heading-font: Chelsea Market, sans-serif;
--font-size: 16px;
/* Other Settings: */
--margin: 10px;
--padding: 20px;
--border: 2px solid #083630;
--round-borders: 10px;
--sidebar-width: 300px;
.darkmode-only {
display: none;
}
.lightmode-only {
display: block;
}
}
:root:has(#dark:checked) {
/* Background Colors: */
--background-color: black;
--content-background-color: #2a145e;
--sidebar-background-color: #81458F;
/* Text Colors: */
--text-color: #D1D1D1;
--sidebar-text-color: #D1D1D1;
--link-color: #a708cc;
/*--link-color-hover: #1bb6a4;*/
--link-color-hover: #9F7EA8;
--header-color: white;
/* Text: */
--font: Open Sans, sans-serif;
--heading-font: Chelsea Market, sans-serif;
--font-size: 16px;
/* Other Settings: */
--margin: 10px;
--padding: 20px;
--border: 2px solid #743e80;
--round-borders: 10px;
--sidebar-width: 300px;
--other-details-colour: white;
.darkmode-only {
display: block;
}
.lightmode-only {
display: none;
}
}
.darkmode-only and .lightmode-only are for select elements like the surf club button on my sidebar, and thus aren't too relevant to this.
Not sure if you'd need to see my JavaScript as well, but I'll provide it if so.
var r = document.querySelector(':root');
if(document.getElementById('dark').checked) {
localStorage.setItem('theme', 'dark');}
if(document.getElementById('light').checked) {
localStorage.setItem('theme', 'light');}
if(localStorage.getItem('theme') == 'dark'){
document.getElementById('dark').checked = true;}
if(localStorage.getItem('theme') == 'light'){
document.getElementById('light').checked = true;}
function savetheme() {
if(document.getElementById('dark').checked) {
localStorage.setItem('theme', 'dark');
console.log("Saved Theme: Light");}
if(document.getElementById('light').checked) {
localStorage.setItem('theme', 'light');
console.log("Saved Theme: Dark");}
}
With that in mind...any tips?