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.

Dark mode in a website with CSS

491 pointsby teaspoonover 5 years ago

36 comments

greggman2over 5 years ago
I recently added dark-mode support to a few sites. It took way more time then I thought it would. At first I thought &quot;oh just add a few lines off CSS&quot; but then ...<p>* Some diagrams had white backgrounds<p>Solution: remove the backgrounds so they are transparent<p>* Most diagrams had black lines&#x2F;arrows which didn&#x27;t look good on dark gray backgrounds<p>Solution: use CSS filter:invert for images<p>* Some images are photos not diagrams so inverting their colors doesn&#x27;t work<p>Solution: Use a selector for svg only img[src$=.svg] and a class when manually needed<p>* Some svg diagrams have their colors referred to like &quot;see the red object&quot; in that text.<p>Solution: filter: invert(1) hue-rotate(180deg);<p>That makes the blacks become white but the colors kind of stay the same hues at least<p>* The are many interactive digrams on the sites.<p>Solution: All of those had to be refactored to not use hard coded colors but do something like<p><pre><code> const darkMatcher = window.matchMedia(&quot;(prefers-color-scheme: dark)&quot;); const isDarkMode = darkMatcher.matches; const colors = isDarkMode ? darkColors : lightColors; </code></pre> And draw everything with the appropriate colors. Its up to you if you want to trigger re-drawing if the user changes their preference<p>* Some text was color coded for to match the diagrams as in<p><pre><code> &lt;span style=&quot;color: red;&quot;&gt;x-axis&quot;&lt;&#x2F;span&gt; </code></pre> But now those colors looked bad on a dark background.<p>Even 5 months later I still find places I missed.
评论 #21197109 未加载
评论 #21197967 未加载
评论 #21199039 未加载
评论 #21196805 未加载
评论 #21200064 未加载
评论 #21196953 未加载
crazygringoover 5 years ago
I love dark mode as a user, but as a developer it terrifies me how much complexity is involved in programming in 2019. At a stroke, it <i>doubles</i> the number of possible combinations of:<p>- 2 device sizes: desktop vs mobile (e.g. responsive)<p>- 3 platforms: web vs iOS vs Android, or web vs Windows vs macOS vs Linux (not even counting cross-browser or OS version quirks), or god forbid all of the above<p>- 2 input methods: hover&#x2F;click&#x2F;drag vs tap&#x2F;hold&#x2F;swipe<p>- 2 modes for accessibility: full visual+audio vs screenreader&#x2F;captioned<p>- And now 2 visual modes: light vs dark<p>That&#x27;s <i>48</i> combinations to design and test for. I&#x27;m probably forgetting some too. But as a developer it makes just the <i>baseline cost</i> of any basic app <i>so damned high</i>.<p>Again, as a user I love it. But the entrance cost for entrepreneurs, or even hobbyists? Man oh man.
评论 #21197309 未加载
评论 #21197993 未加载
评论 #21200733 未加载
评论 #21197294 未加载
评论 #21199730 未加载
评论 #21199323 未加载
评论 #21200058 未加载
评论 #21200550 未加载
评论 #21198101 未加载
评论 #21211145 未加载
JDiculousover 5 years ago
The problem is that this doesn&#x27;t let the user toggle dark mode on&#x2F;off.<p>If you want the user to be able to toggle dark mode on your site without changing their Operating System preferences, then you&#x27;ll need to implement your dark theme as a class (eg. body.theme-dark) since there&#x27;s no way to dynamically set the media query.<p><pre><code> const darkMode = window.matchMedia(&#x27;(prefers-color-scheme: dark)&#x27;) if (darkMode) { document.body.classList.add(&#x27;theme-dark&#x27;) } </code></pre> If you have to write your dark theme CSS as a separate class, then there&#x27;s no sense in duplicating that logic inside the media query and having to override it when the user toggles it. So I ended up using that Javascript instead of putting the styles in the media query.<p>It&#x27;s a shame, I would&#x27;ve preferred a pure HTML&#x2F;CSS solution.
评论 #21199103 未加载
评论 #21198772 未加载
评论 #21203404 未加载
评论 #21199252 未加载
评论 #21201533 未加载
saagarjhaover 5 years ago
&gt; Photographs also looked harsh. It turns out that many people prefer images, too, to be desaturated in dark mode<p>This is an interesting choice. While it might be easier on the eyes, personally it feels kind of icky to change colors on images…
评论 #21198519 未加载
评论 #21197090 未加载
评论 #21200899 未加载
Y-barover 5 years ago
I’m probably not representative of the general dark mode user, I think, but I enable dark mode because I want the UI chrome out of the way to concentrate on the content. I don’t want dark content. Is there a way to disable this media query?
评论 #21196903 未加载
评论 #21197341 未加载
评论 #21196927 未加载
brundolfover 5 years ago
I did this recently for my new personal website! It&#x27;s quite easy and really satisfying. I love doing as much as possible in stateless CSS instead of JavaScript.<p><a href="http:&#x2F;&#x2F;www.brandonsmith.ninja&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.brandonsmith.ninja&#x2F;</a>
评论 #21197293 未加载
评论 #21196553 未加载
评论 #21196319 未加载
评论 #21196600 未加载
评论 #21199642 未加载
评论 #21196450 未加载
cbsksover 5 years ago
Now if only Hacker News would implement it!
评论 #21197020 未加载
评论 #21196760 未加载
评论 #21197352 未加载
评论 #21196773 未加载
评论 #21201360 未加载
评论 #21196847 未加载
kristofferRover 5 years ago
The two best Dark Mode extensions just implemented automatic Dark Mode support (so the web goes dark automatically when Dark Mode is enabled) :D<p><a href="https:&#x2F;&#x2F;github.com&#x2F;darkreader&#x2F;darkreader" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;darkreader&#x2F;darkreader</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;openstyles&#x2F;stylus" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;openstyles&#x2F;stylus</a> (not built into release yet)
评论 #21199662 未加载
ognarbover 5 years ago
I did this for the KDE wikis {userbase,techbase,community}.kde.org, this was a lot harder than I first thought, because of the amount of user contributed content with white background, hardcoded color in the text.<p>I wrote about it here: <a href="https:&#x2F;&#x2F;carlschwan.eu&#x2F;2019&#x2F;07&#x2F;12&#x2F;new-userbase.html" rel="nofollow">https:&#x2F;&#x2F;carlschwan.eu&#x2F;2019&#x2F;07&#x2F;12&#x2F;new-userbase.html</a>
louismerlinover 5 years ago
I baked dark mode into my css microframework [0], and it&#x27;s always so satisfying to see people using it !<p>I didn&#x27;t do the &quot;dimmer images&quot; thing, but I&#x27;ll try it sometimes.<p>[0] <a href="https:&#x2F;&#x2F;concrete.style" rel="nofollow">https:&#x2F;&#x2F;concrete.style</a>
评论 #21196981 未加载
jfengelover 5 years ago
I apologize for my ignorance, but could somebody explain the need for dark mode? I know that it&#x27;s bad to have a phone glow in your face when your eyes are adjusted to the dark, but don&#x27;t most phones automatically lower the brightness? That seems to work well enough for me. Perhaps a specific dark mode could do it better, if well designed, but most sites have a hard enough time doing one good color scheme.<p>I know some people would prefer a light-on-dark color scheme in general, even during the day. Is this entirely about what you see in the dark, or is there a component of wanting more &quot;skins&quot; in general?
评论 #21203274 未加载
评论 #21206144 未加载
ualloover 5 years ago
Here is another article that goes much deeper into how to actually use CSS to implement the dark mode. While it does not rely on the media query (the article is almost 17 months old after all), it shows many other CSS features you could use.<p><a href="https:&#x2F;&#x2F;medium.com&#x2F;@mwichary&#x2F;dark-theme-in-a-day-3518dde2955a" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@mwichary&#x2F;dark-theme-in-a-day-3518dde2955...</a>
hosejaover 5 years ago
If you want to make sure your website is pleasant for dark-browsers, check if it doesn&#x27;t break with extensions such as <a href="https:&#x2F;&#x2F;addons.mozilla.org&#x2F;en-US&#x2F;firefox&#x2F;addon&#x2F;dark-background-light-text&#x2F;" rel="nofollow">https:&#x2F;&#x2F;addons.mozilla.org&#x2F;en-US&#x2F;firefox&#x2F;addon&#x2F;dark-backgrou...</a>
评论 #21200681 未加载
flukusover 5 years ago
The problem with all these dark modes is that I end up with 50 shades of grey and things are harder to distinguish because there&#x27;s no consistency on where to apply the various shades.<p>It&#x27;s a testament to how primitive the web is as a platform that it can&#x27;t do something like apply a universal theme like Windows 3 did.
defanorover 5 years ago
Sounds like good news, and similar to how Emacs handles it; do newer versions of web browsers also change the base&#x2F;default colours when dark mode is used, by the way?<p>Still not a complete replacement of global colour overriding (white flashes, or simply switching between dark and light themes often, can be quite unpleasant in a dark environment, and of course all the websites won&#x27;t ever employ it, and some will use it wrong), but at least from the webmaster side it should simplify the colour theme decisions quite a bit (which were&#x2F;are hard if you care about that sort of thing: I hoped that major web browsers will do something like that with alternate stylesheets someday, but as a media feature it would work too).
评论 #21197601 未加载
jordanmiguelover 5 years ago
Try accomdating for Dark Mode as an email developer...
评论 #21206199 未加载
评论 #21201051 未加载
chrismorganover 5 years ago
I recently added dark mode support with a new website for myself, using (prefers-color-scheme: dark) by default, but also allowing the user to switch between light and dark mode if they have JavaScript enabled. It also supports switching image sources in dark mode, so that I can use a dark mode screenshot in dark mode, and light in light. (I haven&#x27;t yet published the draft article I have using this feature.)<p>I wrote about it at <a href="https:&#x2F;&#x2F;chrismorgan.info&#x2F;blog&#x2F;dark-theme-implementation&#x2F;" rel="nofollow">https:&#x2F;&#x2F;chrismorgan.info&#x2F;blog&#x2F;dark-theme-implementation&#x2F;</a>.
评论 #21197724 未加载
pedrocrover 5 years ago
So that&#x27;s why sites have been annoyingly turning dark lately. In Firefox apparently this can be disabled by creating a new integer preference in about:config called &quot;ui.systemUsesDarkTheme&quot; and setting it to 0.
romdevover 5 years ago
I have chrome updates restricted by my organization and can&#x27;t use this feature. Instead I&#x27;ll occasionally use this bookmarklet to invert colors on very bright pages:<p>javascript:(function() { var tag = document.createElement(&#x27;style&#x27;); tag.type = &#x27;text&#x2F;css&#x27;; document.getElementsByTagName(&#x27;head&#x27;)[0].appendChild(tag); tag[ (typeof document.body.style.WebkitAppearance==&#x27;string&#x27;) ? &#x27;innerText&#x27; : &#x27;innerHTML&#x27;] = &#x27;* { background-color: white !important; color: #444 !important; } html { filter: invert(100%) grayscale(100%); }&#x27;; })();
tripzilchover 5 years ago
&gt; It&#x27;s recommended to avoid pure white for text, and I chose likewise to avoid pure black for the background.<p>This is such nonsense. Pure black is never pure black on a monitor and neither is white. And the link he provides just restates the assertion with no additional reasoning.<p>See, I get it. If you create work as an artistic statement, you deliberately pick not-quite-blacks&#x2F;whites because the defaults are not your artistic choices if you just default to them.<p>However, once you take this as a given rule and you pick some not-quite-white because &quot;it is recommended&quot;, that defeats the entire purpose.
lostmsuover 5 years ago
Does not seem to work in Firefox for Android.
评论 #21198299 未加载
评论 #21197425 未加载
评论 #21196862 未加载
skykoolerover 5 years ago
How does one enable this setting in Firefox?
评论 #21196201 未加载
评论 #21196220 未加载
评论 #21197179 未加载
评论 #21196281 未加载
评论 #21196329 未加载
arjunkomathover 5 years ago
It&#x27;s not widely available yet, <a href="https:&#x2F;&#x2F;caniuse.com&#x2F;#search=prefers-color-scheme" rel="nofollow">https:&#x2F;&#x2F;caniuse.com&#x2F;#search=prefers-color-scheme</a>
评论 #21198070 未加载
eyegorover 5 years ago
Slightly off topic,<p>I find it kind of ironic that there&#x27;s so much debate surrounding the intricacies of dark mode support on a forum with no dark mode support (even though it avoids 90% of the gotchas).
ripley12over 5 years ago
Shameless plug: I did the same thing to my personal website and wrote about it with light&#x2F;dark screenshots: <a href="https:&#x2F;&#x2F;www.reillywood.com&#x2F;blog&#x2F;dark-mode&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reillywood.com&#x2F;blog&#x2F;dark-mode&#x2F;</a><p>I like working in Solarized Dark, so I wanted my website to serve up Solarized Dark. I dim images in dark mode too, but I turn them up to full brightness on hover.
评论 #21196675 未加载
m-p-3over 5 years ago
One of my (maybe unwarranted) concern with automatic dark mode, is the potential ability of the browser and apps to determine my (very) coarse location based on when the dark mode is enabled or not, as it because another potentially identifying info among others.<p>Does that kind of CSS allows a script to determine if the browser is displaying dark mode or not, or send back that information to ads servers?
评论 #21206172 未加载
qubyteover 5 years ago
Naïvely I tried to do this by overriding a few custom properties and using filters. My site has a position: sticky nav, and filters introduce a stacking context, so things which filters apply to naturally go on top of the nav when before they went behind. A minor annoyance, but it demonstrates how introducing a dark mode isn’t as straight forward as I thought it would be.
xfalcoxover 5 years ago
I wish more stuff was presented also as a header, so you could serve a specific CSS file for people who prefer dark-mode.
barbsover 5 years ago
Where did this dark mode craze come from?<p>Don&#x27;t get me wrong - I quite enjoy dark mode most of the time, but why now and not earlier?
评论 #21201078 未加载
评论 #21202632 未加载
nodesocketover 5 years ago
Doesn’t work in Chrome on iOS 13.1.2 for me. Works in Safari though.
alibover 5 years ago
Not working on Firefox for iOS. Can anyone else confirm this to be the case? Looks as though Firefox for iOS does support ‘prefers-color-scheme’ media query.
评论 #21197323 未加载
评论 #21198266 未加载
michalf6over 5 years ago
I get that you want to avoid pure white, but why avoid pure black though? It looks gorgeous on an OLED device, blending with the bezels.
andy_pppover 5 years ago
I would really love a dark mode for hacker news!
ocelikerover 5 years ago
Can HN implement this one day? A dark mode for this website would be great.
mister_hnover 5 years ago
Please add IE6 support
floatrockover 5 years ago
Wikipedia needs this.<p>yes yes there&#x27;s browser plugins, but I&#x27;d rather not have some rando plugin potentially reading every site I visit to load a dark mode plugin on the ones it recognizes.
评论 #21199600 未加载