Toggle switch not working
Hello, I'm trying to create a clock with light and dark mode but there is an issue in it light mode is working correctly but when toggle to dark mode the theme is not changing correctly. The body bg color is not changing fully and the clock container also not changing according to theme
Here is the code link
https://github.com/Myragull/Digital-Clock
And preview
https://myragull.github.io/Digital-Clock/
GitHub
GitHub - Myragull/Digital-Clock
Contribute to Myragull/Digital-Clock development by creating an account on GitHub.
19 Replies
Please provide a minimal working codepen so we can fork and make changes
@13eck here is the link
https://codepen.io/Myra-Gull/pen/QwWVJQa
pro tip, don't do this:
You should never nuke padding and margin on your site. Those are important. Nuke 'em like that and you have to add it back to literally every element. Paragraph space? Gone. Indenting for lists? Gone. Any space anywhere? You guessed it: gone
Got it! Thanks for the tip
Best piece of advice I can give for CSS is that you should always add things, not take away. Need more space? Add more margin. Need a grid? Add
display: grid;
. Once you start removing things (because of the cascade) you might just be removing something important that you don't realize until you need it—and it's not there.Okay
Made a few changes to the CSS to remove all
px
lengths and started with my prior JS for the clock. Added a shorter event listener for you
https://codepen.io/13eck/pen/jEOvQxz
Used the classList.toggle()
function to toggle dark-theme
. If the class exists, then it's removed. If it's not there, it's added. Then used a ternary operator to basically do the toggling so you don't need an if/else blockThanks for the feedback! @13eck.c I really appreciate the improvements you made
👍
Just a heads up, for managing light and dark colors, nested CSS is great and has decent support right now.

That's true! Nesting has been widely supported for over a year now. Some browsers have supported it for over 2 years
Thanks for the explanation!The nesting approach makes the code cleaner and easier to manage, especially for theme switching
Holy crap! I've apparently been sleeping on nesting CSS! I didn't know you could prepend the
&
!Light-dark function is gaining support which makes this ; prefers-color-scheme media queries can be helpful to toggle custom properties
Eg
For flex and grid containers , since there are no collapsing margins , what’s your approach ? Do you then select everything inside anytime you use a flex/grid ?
Yeah, I’d use something like
Its a glorious time to be doing front end rn. At least for another year or two before AI is good at css too.
Yeah, no, that's not gonna happen with the current LLM tech. LLMs learn from what's on the internet. There's more shit code out there then good code, so LLMs will almost always give you bad code. Garbage in, garbage out. Until Kevin or Rachel Andrews teaches the LLM specifically (or someone else who's equally knowledgeable about good CSS) LLMs will never be better than someone who puts a moderate amout of effort to make it good.
This is an even better way to do it if your UI/UX person does a direct mapping of their light mode colors to dark mode colors. Unfortunately, in the workflow at our office, the UI/UX person has different patterns in the 2 color themes, such as an element with a heading and sub heading might have the same color for both text fields in light mode, but the subheading in dark mode is a different neutral shade than the one used for the heading etc.
I think it'd be loads of fun to make a smaller project with a handful of premade UI themes besides just light and dark and then used a body class to change the customer property values and have the whole page update.
Totally there are definitely those cases where the colours don’t translate one-to-one like your subheading. Theme toggles are so fun , I’m working on one that toggles the visual “oS” iPhone or Android . I’ve seen some really creative themes that aren’t just light or dark , and with the :has() selector it makes it so much easier to toggle a data attribute (or a class!) on the body or html and your styles are completely different !
A handful of pre-made ui themes sounds like a fun project ! Would love to see it if you end up deciding to create it !