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.
37 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 !
Did a bit of playing around and have a slightly new take on the clock component:
Keeps state internal and all you need to do is
const clock = newClock("element-selector-here");
then clock.start()
. Easy peasy!
This version cuts down on the maths and only creates a new date object once per loop, and compares the seconds component to see if it needs to update the display or not.you forgot to cancel the raf
in the
stop
method
if you dont cancel it, it will run one last time, which can have negative side effectsOh, right, thanks!
you're welcome
And now, with even more options:
you have a tiny bug: if a dummy stops it twice, you are passing
null
to the function to cancel the raf.
which in turn it means you also are adding multiple rafs if some dummy starts the clock multiple times
also, querySelector throws an exception if it detects syntax errors - which is used by jquery and sizzle to detect and parse pseudo-selectorsBetter?
-# Hope it's better, I have to go to work now ^_^;;
it is a lot better
it's near perfection
personally, i would check against null, but that's nitpicky and personal preference
Where am I not checking against null?
you are checking against null
i meant to say "wouldn't"
Ah, gotcha
still on monday brian, sorry
So for
start()
you’d just check if (!timer) { }
?i would do
if(timer) return;
then continue the code normally
and for the stop
, i would do if(timer) cancel...(timer);
but with proper curlies and indentation, obviouslyYeah, obvi :p
gotta have some clean code!
Hello everyone I completed my html, css and JavaScript I making a project after see and I can also understanding code but when I starting from scratch I can't able to create project I am frustrating anyone can suggest me that what I do for created self project without seeing any code
Don't hijack other posts, please make your own in #help