TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

CSS Only dark mode without JS

33 pointsby dulvuialmost 2 years ago

13 comments

austin-cheneyalmost 2 years ago
I am not sure why this qualifies as a front page article. To achieve color schemes in CSS only follow these steps.<p>1) Remove ALL color information from your CSS files and relocate that into a new CSS file only for color information. That is your default color scheme.<p>2) In that color scheme file prefix each rule with the same class name, possibly <i>colorDefault</i>.<p>3) Create a second color only CSS file with dark colors. This is your dark color scheme. Change the <i>colorDefault</i> class name to something else, possibly: <i>colorDark</i>.<p>4) Include those color files in your site using either media queries from the primary CSS or link tags in the HTML.<p>5) Finally add class name <i>colorDefault</i> to your HTML body tag. Use JavaScript to change that one class name as the user selects a different color scheme on user interaction.<p>Here is an example:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;prettydiff&#x2F;share-file-systems&#x2F;tree&#x2F;master&#x2F;lib&#x2F;css">https:&#x2F;&#x2F;github.com&#x2F;prettydiff&#x2F;share-file-systems&#x2F;tree&#x2F;master...</a>
评论 #36457169 未加载
评论 #36456934 未加载
评论 #36456871 未加载
评论 #36457175 未加载
sigg3almost 2 years ago
&gt; It was pretty sure, that dark mode can only be implemented using JS<p>I used this in the 90&#x27;s:<p>`&lt;body bgcolor=&quot;#000000&quot;&gt;`<p>You needed it to really appreciate the sparks from animated fire gifs.
评论 #36456733 未加载
评论 #36456740 未加载
jpc0almost 2 years ago
Ain&#x27;t there yet but soon we should be able to do this without CSS<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTML&#x2F;Element&#x2F;meta&#x2F;name&#x2F;theme-color" rel="nofollow noreferrer">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTML&#x2F;Element&#x2F;me...</a>
评论 #36457218 未加载
Springtimealmost 2 years ago
CSS only theme toggles are possible by utilizing a checkbox and placing the body content below the checkbox in a container element. Then adding a transparent label element that controls the checkbox state somewhere else on the page and styling a pseudo-button however one likes below the label.<p>Then in CSS setting both the <i>prefers-color-scheme</i> media queries but also configuring how the colors should react when the checkbox state is changed. So if a user has <i>prefers-color-scheme:light</i> then when the theme switcher is clicked it styles the page the opposite (example selector: <i>.theme-switch:checked ~ .pagebody</i>). Can also add such styling as a default outside of media queries, too, for browsers that don&#x27;t recognize <i>prefers-color-scheme</i>.<p>Saving the state across pages however requires some JS but just wanted to comment that CSS-only toggles are possible.
lloydatkinsonalmost 2 years ago
It’s ironic it says that the user should set the theme themselves via their OS because the author doesn’t like to use buttons on sites to do it.<p>The vast majority of users don’t know how to do that. I’m sure the vast majority of that group couldn’t really explain themes as a concept.
评论 #36456870 未加载
评论 #36456769 未加载
评论 #36456771 未加载
评论 #36456810 未加载
评论 #36457147 未加载
butzalmost 2 years ago
I&#x27;m pretty sure with all new CSS features theme toggle could be implemented without JS, but in that case it probably wouldn&#x27;t reflect your current setting.
garritfraalmost 2 years ago
There&#x27;s also:<p>`color-scheme: light dark`<p>which automatically switches the color scheme of default html elements depending on the preferred color scheme. No theming required!<p><a href="https:&#x2F;&#x2F;garrit.xyz&#x2F;posts&#x2F;2023-04-12-instant-dark-theme" rel="nofollow noreferrer">https:&#x2F;&#x2F;garrit.xyz&#x2F;posts&#x2F;2023-04-12-instant-dark-theme</a>
somisherealmost 2 years ago
Very simple CSS only example, including JS-free toggling:<p><a href="https:&#x2F;&#x2F;codepen.io&#x2F;theprojectsomething&#x2F;pen&#x2F;oNQYqRw" rel="nofollow noreferrer">https:&#x2F;&#x2F;codepen.io&#x2F;theprojectsomething&#x2F;pen&#x2F;oNQYqRw</a><p>Note: Could be simplified with the :has selector but it isn&#x27;t currently supported in Firefox.
Gualdrapoalmost 2 years ago
I just used CSS variables at my portfolio [0]. I just don&#x27;t feel like adding a toggle in it to switch between light&#x2F;dark mode adds something of special value in this case.<p>[0] <a href="https:&#x2F;&#x2F;miler.codeberg.page&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;miler.codeberg.page&#x2F;</a>
nicboualmost 2 years ago
I implemented a dark mode on all of my websites. It has become so much easier with CSS variables. Just change the colour variables and the whole website goes dark.<p>The biggest challenge is images. You have to mind images with transparent backgrounds, as well as SVG icons with a fixed colour.
skilledalmost 2 years ago
Sadly this isn&#x27;t the best way to do it because you have no option to switch back to light scheme unless you manually do it in your System settings.
maxiweralmost 2 years ago
Thank you for sharing. But the problem could be, when some users don&#x27;t want to the app to automatically switch to dark mode.
jdthedisciplealmost 2 years ago
Uses SCSS which seems outdated.<p>Why not simply use native CSS variables? Worth pointing out since this is a post about doing something in a simple way.