Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
Autumn, 27 Nov 2026 - @578.16
Forum Activity: Three Stars Random | Recent Posts | Guild Recents
News: Love is not possession  :4u: Guild Events: 22: fate
Super News: ++ MelonJam 2026 ~ 1st Dec to 1st Jan! ++
+  MelonLand Forum
|-+  Da Web Revival
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  Dark mode flicker...but only on index?


« previous next »
Pages: [1] Print Embed
Author Topic: Dark mode flicker...but only on index?  (Read 79 times)
DrakoTheDragon
Newbie ⚓︎
*
View Profile WWWArt


Running around aimlessly, what about you?
⛺︎ My Room
iMood: DrakoTheDragon
OS: Windows

Guild Memberships:
Artifacts:
Triple HornsDevil Cursor TailJeansKitty T-ShirtStripey Scarfstarry yappin kittay
« on: Today at @147 » Toggle Font Embed

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.

Code
: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.
Code
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?

Logged

"You don't have to be ready. You just have to do it."

https://64.media.tumblr.com/778f45cbfe9eb3cbad98b689ed000130/9eb06495f8f9f32f-4a/s250x400/ea294f8fe0cf59e5c75a2e81b3ab1f3c661c4e5a.gifv
https://64.media.tumblr.com/2bfb535f7d789033ae3df0112a6ed2fa/81a7afd7695dcc71-b7/s100x200/b277b2408c22f9e1963a9d96aa3fa8624a656592.gifvhttps://64.media.tumblr.com/f40ce174e969e0b423240343c0695df8/e1dd46eba178dc71-51/s100x200/f2ce4c50549d6377d5d2acfd84b9a90d13467271.pnjhttps://64.media.tumblr.com/a1dbb1f8f4b954bd3848593bc2a9ad80/e9a831999458d974-9a/s250x400/6e2a09e794fb5335067f513b07ed80efdff9eeff.gifv

Artifact Swap: oh heck it has a bombStupid wall, MOVE!sunsetChaos rune500 coin
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
The Worm is creeping around Melonland profiles!Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #1 on: Today at @357 » Toggle Font Embed

I'll provide a full explanation of why you're seeing what you are, how I diagnosed it, and what you can do about it in a moment. But to keep things moving, here's the solution:

Move this block of code, which currently appears at the top of the <body>:


Code
  <script is:inline>
    // The configured mode is stored in local storage
    const isDarkMode = localStorage.getItem("dark");

    // Set theme to 'dark' if darkMode = 'true'
    const theme = isDarkMode === "true" ? "dark" : "";

    // Put dark class on html tag to enable dark mode
    document.querySelector(":root").className = theme;
  </script>

Instead, put it in your <head> section, right before this line of code:

Code
<link href="/css/style.css" rel="stylesheet" />

That should fix it. I'll explain more after I've taken the kids to school!

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
The Worm is creeping around Melonland profiles!Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #2 on: Today at @366 » Toggle Font Embed

Here's why you're seeing what you're seeing. A web browser is allowed to start rendering a page as soon as it's gotten to the end of the </head> section. This means that it can e.g. load the stylesheet and start rendering it. Even though there isn't much other content by this point, you can get at the very least the page background and maybe a few other things. That's the flash you're seeing.

You can work out exactly what you're seeing by using the Performance tab in your browser's debugger. Press the Record button, reload the page, and then press the Stop button. You can now browse through screenshots of every DOM/loading state that your page went though.



JavaScript only blocks rendering that happens after it (and even that can be overridden). If you want some CSS to be blocked by some JavaScript, you have to run the JavaScript first.

(Normally we want our JavaScripts to be as load-loaded as possible, which is why they're often placed in the bottom of the </body>. This ensures that the page can render as much as possible before it has to start processing the relatively-slow JS code. But in the specific example that your stylesheet is largely affected by your JS, you need the JS to be earlier in the page.)

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Guide | Rules | Whos a Mod? | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
@000 Melon
Land
Forum Art Hub Chat Webring Explore Random Recent Tenement Arcade Want to Login or Join ? Web Craft Guide Graphic Catalogue Wiki Help I'm Lost! Link Land Newsletters Image Stream Minecraft: Online
Zap!
Passport Sites tamaNOTchi Asatte Add your site?
Aero Archive Onio Cafe 32 Bit Cafe Status Cafe