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
13eck
13eck6d ago
Please provide a minimal working codepen so we can fork and make changes
Myra
MyraOP6d ago
13eck
13eck6d ago
pro tip, don't do this:
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
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
Myra
MyraOP6d ago
Got it! Thanks for the tip
13eck
13eck6d ago
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.
Myra
MyraOP6d ago
Okay
13eck
13eck6d ago
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 block
Myra
MyraOP6d ago
Thanks for the feedback! @13eck.c I really appreciate the improvements you made
13eck
13eck6d ago
👍
Rowe2ry
Rowe2ry6d ago
Just a heads up, for managing light and dark colors, nested CSS is great and has decent support right now.
*, *::before, *::after {
box-sizing: border-box;
}


body {
height: 100svh;
background: #ffffff;

&.dark-theme {
background-color: #000000;
color: white;
}
}

.clock_container {
display: flex;
align-items: center;
justify-content: center;
background: rgba(255, 255, 255, 0.8);
backdrop-filter: blur(15px);
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.1);

.dark-theme & {
background: rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.3);
color: white;
}
}

.time-para {
font-size: 4rem;
font-weight: 700;
color: #030303;

.dark-theme & {
color: #fff;
}
}

.parent_container {
gap: 1rem;
margin-inline: auto;
width: 55ch;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

.container_a {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}

.container_a h1 {
color: #000000;

.dark-theme & {
color: #ffffff;
}
}

button {
padding: 1em 2em;
border-radius: 6px;
border: none;
background: #000000;
color: #ffffff;
align-items: center;

.dark-theme & {
background: #ffffff;
color: #000000;
}
}
*, *::before, *::after {
box-sizing: border-box;
}


body {
height: 100svh;
background: #ffffff;

&.dark-theme {
background-color: #000000;
color: white;
}
}

.clock_container {
display: flex;
align-items: center;
justify-content: center;
background: rgba(255, 255, 255, 0.8);
backdrop-filter: blur(15px);
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.1);

.dark-theme & {
background: rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.3);
color: white;
}
}

.time-para {
font-size: 4rem;
font-weight: 700;
color: #030303;

.dark-theme & {
color: #fff;
}
}

.parent_container {
gap: 1rem;
margin-inline: auto;
width: 55ch;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

.container_a {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}

.container_a h1 {
color: #000000;

.dark-theme & {
color: #ffffff;
}
}

button {
padding: 1em 2em;
border-radius: 6px;
border: none;
background: #000000;
color: #ffffff;
align-items: center;

.dark-theme & {
background: #ffffff;
color: #000000;
}
}
No description
13eck
13eck6d ago
That's true! Nesting has been widely supported for over a year now. Some browsers have supported it for over 2 years
Myra
MyraOP6d ago
Thanks for the explanation!The nesting approach makes the code cleaner and easier to manage, especially for theme switching
13eck
13eck6d ago
Holy crap! I've apparently been sleeping on nesting CSS! I didn't know you could prepend the &!
clevermissfox
clevermissfox5d ago
Light-dark function is gaining support which makes this ; prefers-color-scheme media queries can be helpful to toggle custom properties Eg
:root {
clr-primary-bg: whitesmoke;
}

@media (prefers-color-scheme : dark) {
clr-primary-bg: black;
}

Body {
background-color : var(—clr-primary-bg);
}
:root {
clr-primary-bg: whitesmoke;
}

@media (prefers-color-scheme : dark) {
clr-primary-bg: black;
}

Body {
background-color : var(—clr-primary-bg);
}
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 ?
13eck
13eck5d ago
Yeah, I’d use something like
.flex-container > * {
margin: 2em;
padding: 1em 3em;
}
.flex-container > * {
margin: 2em;
padding: 1em 3em;
}
Rowe2ry
Rowe2ry3d ago
Its a glorious time to be doing front end rn. At least for another year or two before AI is good at css too.
13eck
13eck2d ago
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.
Rowe2ry
Rowe2ry2d ago
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.
clevermissfox
clevermissfox23h ago
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 you find this page helpful?