Live data from Hacker News

Dark mode in a website with CSS

tombrow.com

11–20 of 191 posts

Re: Dark mode in a website with CSS

#11
post #4

I did this recently for my new personal website! It's quite easy and really satisfying. I love doing as much as possible in stateless CSS instead of JavaScript. http://www.brandonsmith.ninja/

I've been using it for a few months on my personal site as well. It defaults to the light version and then switches to the dark them on request. This is so people who don't care or aren't good with computers get the light version and those that can set the system theme have the option of getting the dark version. https://thomasryan.xyz/

FWIW, your Posts/Demo/Resume links are laid out a bit oddly on my iPad.

Re: Dark mode in a website with CSS

#12
post #4

I did this recently for my new personal website! It's quite easy and really satisfying. I love doing as much as possible in stateless CSS instead of JavaScript. http://www.brandonsmith.ninja/

I also did this on my personal website. I was surprised how easy it was, and happy that it was built into CSS, requiring no JS (as you said).

Re: Dark mode in a website with CSS

#14
post #4

I did this recently for my new personal website! It's quite easy and really satisfying. I love doing as much as possible in stateless CSS instead of JavaScript. http://www.brandonsmith.ninja/

I've been using it for a few months on my personal site as well. It defaults to the light version and then switches to the dark them on request. This is so people who don't care or aren't good with computers get the light version and those that can set the system theme have the option of getting the dark version. https://thomasryan.xyz/

> people who don't care or aren't good with computers get the light version

Or more importantly, people whose browsers/OSes don't set that CSS flag

Re: Dark mode in a website with CSS

#15
post #4

I did this recently for my new personal website! It's quite easy and really satisfying. I love doing as much as possible in stateless CSS instead of JavaScript. http://www.brandonsmith.ninja/

Mine too! Finally put up a personal site last month after not touching any web projects for a long while. I've checked out of a lot of social media and wanted my own place to put stuff and to poke around with javascript and web graphics.

One of the upcoming posts is going to cover handling dark mode for content. So far I've just used transparent backgrounds with no black or white drawing so that it's visible either way.

https://will.institute/

Not much on it yet, just been slowly working through the setup for doing a canvas with a responsive site.

Neat tidbit - the logo image is a single SVG in shades of dark gray, which doesn't work well on a dark background. You can apply image transformations in CSS under the dark mode media query to adapt things like this:

    img#wi-logo {
        filter: invert(100%) brightness(110%);
    }
This is the same tool that the linked post uses for desaturating images, but adding that invert is a useful tool for dark mode. Personally I wouldn't recommend general image desaturation, photos taking more focus against a dark background is a good look if you ask me. At least consider making it an opt-in setting instead of the default.

Re: Dark mode in a website with CSS

#17
post #4

I did this recently for my new personal website! It's quite easy and really satisfying. I love doing as much as possible in stateless CSS instead of JavaScript. http://www.brandonsmith.ninja/

Same here: https://phili.pe/

I've used CSS variables though, which make it even simpler. E.g. I have `--page-background-color` and `--text-color-primary` that are set to a certain default value. In dark mode, these variables are set to other values, removing the need to redefine the actual selectors.

Re: Dark mode in a website with CSS

#18
Shameless plug: I did the same thing to my personal website and wrote about it with light/dark screenshots: https://www.reillywood.com/blog/dark-mode/

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.

Re: Dark mode in a website with CSS

#19
I recently added dark-mode support to a few sites. It took way more time then I thought it would. At first I thought "oh just add a few lines off CSS" but then ...

* Some diagrams had white backgrounds

Solution: remove the backgrounds so they are transparent

* Most diagrams had black lines/arrows which didn't look good on dark gray backgrounds

Solution: use CSS filter:invert for images

* Some images are photos not diagrams so inverting their colors doesn't work

Solution: Use a selector for svg only img[src$=.svg] and a class when manually needed

* Some svg diagrams have their colors referred to like "see the red object" in that text.

Solution: filter: invert(1) hue-rotate(180deg);

That makes the blacks become white but the colors kind of stay the same hues at least

* The are many interactive digrams on the sites.

Solution: All of those had to be refactored to not use hard coded colors but do something like

    const darkMatcher = window.matchMedia("(prefers-color-scheme: dark)");
    const isDarkMode = darkMatcher.matches;
    const colors = isDarkMode ? darkColors : lightColors;
And draw everything with the appropriate colors. Its up to you if you want to trigger re-drawing if the user changes their preference

* Some text was color coded for to match the diagrams as in

    x-axis"
But now those colors looked bad on a dark background.

Even 5 months later I still find places I missed.

Re: Dark mode in a website with CSS

#20

Shameless plug: I did the same thing to my personal website and wrote about it with light/dark screenshots: https://www.reillywood.com/blog/dark-mode/ 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.

I only use Solarized for code snippets on my website, but the benefit of using it as a color scheme is that switching between the two is a one line addition: only the background needs to respond to prefers-color-scheme.
Post reply on HN