Hi. I have not gone through the problem you described yet.
You might want to check out the S2 Framework, which includes a powerful and easy-to-use Light / Dark Mode switching feature built on Webflow’s variable modes. It’s super simple to set up, and almost no additional configuration is needed after setup.
The mode (theme) switching UI: UI Elements | S2 Framework
In case you are not using S2, here are the steps to enjoy it:
Step 0
Set your dark mode colors as a new mode (but not variables under the base mode).
Step 1
Clone the S2 Framework, and copy the entire Light / Dark Mode Switching section to your site. You may keep it somewhere hidden.
Set up the u-light-mode
and u-dark-mode
utility classes so that the correct Color mode is selected for each of the classes.
Step 2
Put the below in your site’s head code (it can also be found in S2’s global head code):
<!-- Light / Dark Mode Switching by the S2 Framework -->
<!-- To Apply Light / Dark Mode (Theme) -->
<link rel="preload" href="https://cdn.jsdelivr.net/gh/SPACESODA/S2-Framework@2.3.8/webflow/js/mode-apply.min.js" as="script">
<script src="https://cdn.jsdelivr.net/gh/SPACESODA/S2-Framework@2.3.8/webflow/js/mode-apply.min.js" defer></script>
<!-- Mode Switching Function -->
<link rel="preload" href="https://cdn.jsdelivr.net/gh/SPACESODA/S2-Framework@2.3.8/webflow/js/mode-switching.min.js" as="script">
<script src="https://cdn.jsdelivr.net/gh/SPACESODA/S2-Framework@2.3.8/webflow/js/mode-switching.min.js" defer></script>
Step 3
Include the below custom CSS in a “Global code” component of your pages:
/* toggle ui */
.toggle:active .toggle-button { width: 1.85em; }
/* mode switching ui */
/* toggle style */
body.u-dark-mode .toggle { justify-content: flex-end; }
/* mode-switch style */
:root {
--light-mode-on: rgba(255, 255, 255, 0.55);
--dark-mode-on: rgba(0, 0, 0, 0.35);
}
body.u-system-mode .mode-switch [data-button]:not([data-button="color-mode-system"]) { background-color: transparent; }
body.u-system-mode.u-light-mode .mode-switch [data-button="color-mode-system"] { background-color: var(--light-mode-on); }
body.u-system-mode.u-dark-mode .mode-switch [data-button="color-mode-system"] { background-color: var(--dark-mode-on); }
body.u-light-mode .mode-switch [data-button="color-mode-light"] { background-color: var(--light-mode-on); }
body.u-dark-mode .mode-switch [data-button="color-mode-dark"] { background-color: var(--dark-mode-on); }
body.u-light-mode .mode-switch .mode-switch_button:hover {
background-color: rgba(170, 170, 170, 1); color: rgba(240, 240, 240, 1);
}
body.u-dark-mode .mode-switch .mode-switch_button:hover {
background-color: rgba(210, 210, 210, 1); color: rgba(130, 130, 130, 1);
}
Step 4
Add the class body
to Body of each page.
That’s it — done!
You can customize the toggle styles and buttons, or use your own buttons by applying the right data-button=""
attributes, to match your design.