Implementing Dark Mode With SASS
My site officially has dark mode and it automatically responds to visitors’ system preferences! I held off on implementing it for a while because I figured it would be a lot of work. Turns out it only took 30 minutes and 5 lines of code.
Here’s how I did it.
Using a SASS Mixin
Ok so what’s hidden in that dark-mode mixin?
The truth is there are about 50 lines of code hidden in the mixin. It’s a bunch of style overrides setting the dark theme. Here’s an excerpt:
The cool part is that these are 50 existing lines of code. My original site design included some light and some dark pages, depending on the type of content being served. Previously, pages with a lot of text (blog posts) were white and pages with a lot of media (case studies) were dark.
I was using a body class to apply these style overrides to pages selectively:
I was delighted to find that including this existing mixin inside a media query is all it took to enable dark mode:
The Power of CSS
CSS gets a lot of hate, but this is one of those situations where the cascade proved incredibly helpful and efficient. For a small, mostly-editorial site like mine, adding some simple overrides works great.
This approach also plays nice with external libraries.
I was able to add a dark theme for syntax highlighting by including a new Pygments theme in the media query. Easy.
You probably you don’t have a convenient file of dark mode overrides sitting around for your site. Still, keep in mind that it only took 50 lines of color customization to make this happen. If your site has a lot of text, it’s worth adding dark mode to improve your nighttime UX.