<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ dark mode - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ dark mode - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 29 May 2026 20:58:20 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/dark-mode/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Code Dark Mode for Google Sheets with Apps Script and JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Google Sheets is a great tool for working and collaborating on spreadsheets, but it doesn't have native support for dark mode.  In this article, we'll create our own dark mode. One way to do that would be by selecting all the cells and manually chang... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-code-dark-mode-for-google-sheets/</link>
                <guid isPermaLink="false">66b8de03753e1e313e83948f</guid>
                
                    <category>
                        <![CDATA[ dark mode ]]>
                    </category>
                
                    <category>
                        <![CDATA[ google apps script ]]>
                    </category>
                
                    <category>
                        <![CDATA[ google sheets ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Eamonn Cottrell ]]>
                </dc:creator>
                <pubDate>Wed, 20 Sep 2023 22:23:15 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/09/code-your-own-dark-mode.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Google Sheets is a great tool for working and collaborating on spreadsheets, but it doesn't have native support for dark mode. </p>
<p>In this article, we'll create our own dark mode. One way to do that would be by selecting all the cells and manually changing their background color and font color. This will get the job done, but we can automate the process and add more style options to choose from.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/automate-2.gif" alt="Image" width="600" height="400" loading="lazy">
<em>gif of cartoon character grabbing a computer</em></p>
<p>Here’s what we’ll build: a style selector that has four different styles triggered by either a new dropdown menu or by clicking a custom button icon.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/1-12.png" alt="Image" width="600" height="400" loading="lazy">
<em>screenshot of Google Sheets with different background modes</em></p>
<p>And here's the video walkthrough if you'd like to follow along with that instead: </p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/TxSGuXPav70" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-how-to-create-dark-mode-in-apps-script">How to Create Dark Mode in Apps Script</h2>
<p>We’ll create four functions, one for each style mode. Each of our functions will do the following:</p>
<ol>
<li>Set background color</li>
<li>Set font color</li>
<li>Set font family</li>
<li>Set border color and style</li>
</ol>
<p>Let’s walk through how to create a <code>darkMode</code> function in <a target="_blank" href="https://script.google.com/home/start">Apps Script</a>. We'll define each function with the <code>function</code> keyword, followed by whatever we’ll name it.</p>
<p>Because the functions take no arguments (they just run without needing more info from us). That is, we have open and closed parentheses with nothing inside them followed by an open curly brace.</p>
<p>All of our code for the function goes between the <code>darkMode</code> function's curly braces:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">darkMode</span>(<span class="hljs-params"></span>) </span>{
  SpreadsheetApp.getActive().getRange(<span class="hljs-string">'A1:Z'</span>)
    .setBackground(<span class="hljs-string">"#333333"</span>)
    .setFontColor(<span class="hljs-string">"white"</span>)
    .setFontFamily(<span class="hljs-string">"Google Sans"</span>)
    .setBorder(<span class="hljs-literal">false</span>,<span class="hljs-literal">false</span>,<span class="hljs-literal">false</span>,<span class="hljs-literal">false</span>,<span class="hljs-literal">true</span>,
     <span class="hljs-literal">true</span>,<span class="hljs-string">"#444444"</span>,SpreadsheetApp.BorderStyle.SOLID)
}
</code></pre>
<p>To select all the cells, we used the built-in methods from the <code>SpreadsheetApp</code> class: <code>getActive()</code> and <code>getRange()</code>. These select the active sheet as well as a given range.</p>
<p>In our case, we’ll plug in <code>A1:Z</code> as the range, but you can extend this further if you’d like. For instance,  <code>A1:AZ</code>, would add columns <code>AA:AZ</code> and then apply our styling to them.</p>
<p>The four lines that follow are simply dot notation extensions telling what styles to apply. You can write this on one line if you’d like, but it’s good practice to have line breaks to make the code easy to read.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/clever.gif" alt="Image" width="600" height="400" loading="lazy">
<em>gif of man saying, that’s clever</em></p>
<h2 id="heading-how-to-set-colors-and-fonts">How to Set Colors and Fonts</h2>
<p>You'd notice that we used both <code>setBackground(#333333)</code> and <code>setFontColor("white")</code> in the code. This is because we can use CSS notation colors in either hex format or by using the color’s name.</p>
<p>Using <code>setFontFamily("Google Sans")</code>, we gave it the font family name within quotations. Being a Google product, you can use any of the <a target="_blank" href="https://fonts.google.com/">Google Fonts</a> as well as Google’s own Google Sans font as I found out making this project.</p>
<h2 id="heading-how-to-set-the-border">How to set the Border</h2>
<p>The <code>setBorder(false,false,false,false,true, true,"#444444",SpreadsheetApp.BorderStyle.SOLID)</code> function let’s you enter <code>true</code> or <code>false</code> values for the top, left, bottom, right, vertical, or horizontal borders, in that order, followed by the color and style.</p>
<p>To set the style, we had to invoke a built in <a target="_blank" href="https://developers.google.com/apps-script/reference/document/attribute">Enum Attribute</a> — <code>BorderStyle</code> — to change the style of the border.</p>
<h2 id="heading-how-to-create-the-style-menu">How to Create the Style Menu</h2>
<p>To be able to select any of the styles we’re making from the actual spreadsheet, we need a menu.</p>
<p>To add the menu, we'll create a new function called  <code>onOpen()</code> that runs as soon as the spreadsheet opens and then the built-in methods from the <code>getUi()</code> to build our custom menu.</p>
<p>We can create the menu with <code>.createMenu()</code> and then add each of our functions to the menu with the <code>addItem()</code> function.</p>
<p>Here's the code:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">onOpen</span>(<span class="hljs-params"></span>)</span>{
  SpreadsheetApp.getUi()
    .createMenu(<span class="hljs-string">'Style'</span>)
    .addItem(<span class="hljs-string">"Dark"</span>,<span class="hljs-string">"darkMode"</span>)
    .addItem(<span class="hljs-string">"Papyrus"</span>,<span class="hljs-string">"papyrusMode"</span>)
    .addItem(<span class="hljs-string">"Light"</span>,<span class="hljs-string">"lightMode"</span>)
    .addItem(<span class="hljs-string">"Synth"</span>,<span class="hljs-string">"synthMode"</span>)
    .addToUi();
}
</code></pre>
<p>Google Apps Script automatically integrates with Google Workspace apps (like Google Sheets) so the functions we've added in the code will make the functionalities accessible in your Google Sheets.</p>
<h2 id="heading-how-to-add-icon-buttons">How to Add Icon Buttons</h2>
<p>As a bonus, I added four icon images to the sheet, and by selecting <code>Assign script</code> we can have these icons act as buttons to trigger any of the four styles we’ve built:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/2.png" alt="Image" width="600" height="400" loading="lazy">
<em>screenshot of icons in spreadsheet</em></p>
<p>When you type the function name into the Assign script dialog box, make sure you do not to use parentheses. You should only enter the name of the function itself:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/3.png" alt="Image" width="600" height="400" loading="lazy">
<em>screenshot of assigning script to image</em></p>
<p>And bingo! We’ve got ourselves a style picker. You can add or edit these to create any number of combinations that you can easily toggle on and off in your own Google Sheet!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/09/4.png" alt="Image" width="600" height="400" loading="lazy">
<em>screenshot of Synth Mode Style</em></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This article shows how you can create can code dark, and other background modes for your Google Sheets using Apps Script and JavaScript.</p>
<p>I hope this has been useful for you!</p>
<h2 id="heading-more-resources">More Resources😄</h2>
<p>▶ Find all my video tutorials and walkthroughs on YouTube: <a target="_blank" href="https://www.youtube.com/@eamonncottrell?sub_confirmation=1">https://www.youtube.com/@eamonncottrell?sub_confirmation=1</a></p>
<p>▶ Connect with me on LinkedIn where I share daily tips: <a target="_blank" href="https://www.linkedin.com/in/eamonncottrell/">https://www.linkedin.com/in/eamonncottrell/</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Turn On Dark Mode on Google – Chrome Black Theme Tutorial ]]>
                </title>
                <description>
                    <![CDATA[ These days, lots of developers like to use dark mode. And it's no surprise, as activating dark mode reduces eye strain and leads to better eye health. It also helps both machines and humans perform better overall. In this article, I will show you how... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-turn-on-dark-mode-on-google-chrome-black-theme-tutorial/</link>
                <guid isPermaLink="false">66adf149febac312b73075b6</guid>
                
                    <category>
                        <![CDATA[ Android ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Google Chrome ]]>
                    </category>
                
                    <category>
                        <![CDATA[ dark mode ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Windows ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Fri, 08 Oct 2021 19:10:46 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/10/dark.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>These days, lots of developers like to use dark mode. And it's no surprise, as activating dark mode reduces eye strain and leads to better eye health. It also helps both machines and humans perform better overall.</p>
<p>In this article, I will show you how to turn on the dark theme on your Google Chrome app, on both Windows machines and Android phones. We'll also learn how to turn on dark theme for Google with some Chrome extensions. </p>
<p>It doesn’t end there – you will also be able to run your Windows apps in dark mode after reading this article.</p>
<h2 id="heading-how-to-turn-on-dark-mode-for-google-on-windows-10">How to Turn on Dark Mode for Google on Windows 10</h2>
<p><strong>Step 1</strong>: To turn on dark theme for Google on your Windows 10 PC, click on Start, or press the <code>WIN</code> (Windows) key.</p>
<p><strong>Step 2</strong>: Click on Settings
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-1.jpg" alt="ss-1" width="600" height="400" loading="lazy"></p>
<p><strong>Step 3</strong>: Click on Personalization
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-2.jpg" alt="ss-2" width="600" height="400" loading="lazy"></p>
<p><strong>Step 4</strong>: Select Colors from the menu tab
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-3.jpg" alt="ss-3" width="600" height="400" loading="lazy"></p>
<p><strong>Step 5</strong>: And finally, under “Choose your default app mode”, choose dark.
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-4.jpg" alt="ss-4" width="600" height="400" loading="lazy"></p>
<p>The settings app itself will change to dark mode, meaning all the apps on your machine are now running on dark mode.
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-5-1.png" alt="ss-5-1" width="600" height="400" loading="lazy"></p>
<p>To confirm that Google is now running on dark mode, open up the Chrome app and search anything on Google:
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-6.png" alt="ss-6" width="600" height="400" loading="lazy"></p>
<p>Other parts of Chrome will run on dark theme, too:
<img src="https://www.freecodecamp.org/news/content/images/2021/10/other-parts-of-chrome.png" alt="other-parts-of-chrome" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-turn-on-dark-mode-for-google-with-a-chrome-extension">How to Turn on Dark Mode for Google With a Chrome Extension</h2>
<p>To turn on dark theme for Google with a Chrome extension, download and activate the Just Black chrome extension from the Chrome Web Store. </p>
<p>The extension was built by the Chrome team, so it's safe to install and use.
<img src="https://www.freecodecamp.org/news/content/images/2021/10/just-black.png" alt="just-black" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-turn-on-dark-mode-for-google-on-android-phones">How to Turn on Dark Mode for Google on Android Phones</h2>
<p><strong>Step 1</strong>: To turn on dark mode for Google on an Android phone, open your chrome app and click on the three dots in the top right corner.
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-a1.jpg" alt="ss-a1" width="600" height="400" loading="lazy"></p>
<p><strong>Step 2</strong>: Tap settings
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-a2.jpg" alt="ss-a2" width="600" height="400" loading="lazy"></p>
<p><strong>Step 3</strong>: Select theme
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-a3.jpg" alt="ss-a3" width="600" height="400" loading="lazy"></p>
<p><strong>Step 4</strong>: Finally, choose dark
<img src="https://www.freecodecamp.org/news/content/images/2021/10/ss-a4.jpg" alt="ss-a4" width="600" height="400" loading="lazy"></p>
<p>Your Chrome mobile app should be in dark mode, including the Google search page:
<img src="https://www.freecodecamp.org/news/content/images/2021/10/Screenshot_20211008-163435.png" alt="Screenshot_20211008-163435" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned how to turn on dark mode for Google on Windows machines and Android phones. </p>
<p>You also learned how to do the same with a chrome extension. </p>
<p>There are several other extensions that give you dark mode on Chrome, so feel free to check them out on the Chrome Web Store.</p>
<p>Thank you for reading, and have a nice time.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Gmail Dark Mode – How to Change the Gmail Background Theme on Desktop, iOS, and Android ]]>
                </title>
                <description>
                    <![CDATA[ If you're like me, you spend several hours a week in Gmail. Staring at Gmail's bright white design can strain your eyes – especially at night. Fortunately, Gmail has a built-in dark mode theme. This will make it much easier to read at night. In addit... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/gmail-dark-mode/</link>
                <guid isPermaLink="false">66ac87f54465950a6e26c92f</guid>
                
                    <category>
                        <![CDATA[ dark mode ]]>
                    </category>
                
                    <category>
                        <![CDATA[ gmail ]]>
                    </category>
                
                    <category>
                        <![CDATA[ how-to ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kristofer Koishigawa ]]>
                </dc:creator>
                <pubDate>Thu, 08 Apr 2021 06:07:32 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/602b5ce90a2838549dcc632e.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're like me, you spend several hours a week in Gmail. Staring at Gmail's bright white design can strain your eyes – especially at night.</p>
<p>Fortunately, Gmail has a built-in dark mode theme. This will make it much easier to read at night.</p>
<p>In addition to reducing eye strain, dark mode can even reduce the battery consumption of your device, allowing you to use it for longer without charging it. (It's a small improvement, but it helps.)</p>
<p>So how do you turn on dark mode in Gmail? I'll show you how to switch to Gmail's dark theme in just a few steps.</p>
<h2 id="heading-gmail-dark-mode-on-desktop">Gmail Dark Mode on Desktop</h2>
<p>First, open your browser and go to <a target="_blank" href="https://mail.google.com/">Gmail</a>. Sign in if necessary.</p>
<p>Click on the gear icon in the top right and next to "Theme" click "View all":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-browser-1-1.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In the "Pick your theme" menu, scroll down until you see the different color options.</p>
<p>Select "Dark" (this text will be visible when you hover over the black thumbnail), and click "Save":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-browser-2.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>And when you go back to your inbox, it'll be in dark mode:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-browser-final.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-gmail-dark-mode-on-android">Gmail Dark Mode on Android</h2>
<p><strong>Note:</strong> This method requires Android 10 and up.</p>
<p>First, open the <a target="_blank" href="https://play.google.com/store/apps/details?id=com.google.android.gm">Gmail</a> app. Download the app and / or sign in if necessary.</p>
<p>In Gmail, open the side menu by tapping the three vertical bars on the top left of the screen, or by swiping from the left:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-android-1-5.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In the side menu, scroll down to the bottom and tap "Settings":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-android-2.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Then in the Settings menu, tap "General settings":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-android-3.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In the General settings menu, tap "Theme":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-android-4.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Finally in the Theme pop up menu, select "Dark":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-android-5.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>And Gmail will switch to dark mode:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-android-final-1.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-gmail-dark-mode-on-ios">Gmail Dark Mode on iOS</h2>
<p><strong>Note:</strong> This method requires iOS 13 and up.</p>
<p>If you have an iPhone and want your Gmail to be in dark mode, it's super simple to make that happen.</p>
<p>There isn't a setting in the Gmail app itself – you simply set your phone's settings to dark mode (and Gmail will follow suit). Here's how to do that.</p>
<p>First, go to your phone's "Settings" and scroll until you see "Display &amp; Brightness" partway down:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/iphone-settings.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Click on Display and Brightness, and you'll be taken to this screen:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-vs-light-mode.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>As you can see, my phone is currently set to Light Mode (it's selected). If I want to switch to dark mode,  just toggle that option on, like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/change-to-dark-mode.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now my phone is set to dark mode (all the time, rather than just auto-adjusting when it gets dark outside).</p>
<p>So my Gmail app will now look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-gmail.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>There you have it! If you only want your Gmail app to use dark mode at night, you can update your settings accordingly. </p>
<p>Just make sure your phone is on Light mode and the "Automatic" option is toggled on. Then just select the "Sunset to Sunrise" option under "Options" and your phone will automatically adjust your apps into dark mode when the sun goes down!</p>
<p>It'll look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/dark-mode-vs-light-mode-1.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-thanks-for-reading"><strong>Thanks for Reading</strong></h2>
<p>If you found this helpful, please share it with your friends so more people can enable Gmail's dark mode on desktop, Android, and iOS devices.</p>
<p>Also, if you liked this article, let me know over on <a target="_blank" href="https://twitter.com/kriskoishigawa">Twitter</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Enable Dark Mode in HTML Email – Everything You Need to Know ]]>
                </title>
                <description>
                    <![CDATA[ By Patrik Krupař With the new iOS 13 update, Apple Mail is getting a dark theme. That means it's the first major email client that supports the prefers-color-scheme CSS media query. So you can now design emails specifically for both dark and light th... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/dark-mode-in-html-email-everything-you-need-to-know/</link>
                <guid isPermaLink="false">66d4608ca326133d12440a53</guid>
                
                    <category>
                        <![CDATA[ dark mode ]]>
                    </category>
                
                    <category>
                        <![CDATA[ email ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 16 Dec 2019 12:20:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/12/iPhone-X-XS---7@2x.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Patrik Krupař</p>
<p>With the new iOS 13 update, Apple Mail is getting a dark theme. That means it's the first major email client that supports the <code>prefers-color-scheme</code> CSS media query. So you can now design emails specifically for both dark and light themes.</p>
<p>I’m a massive dark mode fan, and blindingly-bright email is my nemesis. So when I learned about dark mode in iOS 13, I did the only obvious thing and ordered a brand new iPhone to test things out.</p>
<p>While I was at it, I also tested how dark mode works in almost all email clients, including the troublemaker Outlook. Here’s what I found.</p>
<p><strong>But first, w</strong>hat is prefers-color-scheme?<em>**</em><br>The <code>prefers-color-scheme</code> CSS media query is used to detect whether the user prefers a light or dark theme, making it possible to design email specifically for both. </p>
<p>With the iOS 13 update, <strong>the support in most popular email clients jumped from 2.3% to 38.4%</strong>! A huge step thanks to Apple Mail's popularity. Surprisingly, Outlook was the only email client that supported this before Apple Mail.</p>
<h2 id="heading-how-dark-mode-works-in-popular-email-clients">How dark mode works in popular email clients</h2>
<p>To render the email message dark itself, email clients invert the email’s colors automatically behind the scenes. For regular user-to-user emails, this works well and consistently in all email clients.</p>
<p>However, it’s not that simple for custom HTML emails — those that fill most of our inboxes. I’m talking transactional and promotional.</p>
<p>Here’re the differences I found in how email clients handle dark mode email rendering:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Email client</td><td>Popularity</td><td>Dark UI</td><td>Auto-invert email colors</td><td>Supports @media (prefers-color-scheme)</td><td></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Apple Mail</strong> iPhone + iPad</td><td>36.1%</td><td>✔&nbsp;Yes</td><td>✔&nbsp;Yes</td><td>✔&nbsp;Yes</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/applemail-ios.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Gmail</strong> Android 10</td><td>27.8% *</td><td>✔ Yes</td><td>✔ Yes</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/gmail-android-1.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Gmail</strong> iOS 13</td><td>27.8% *</td><td>✖ No</td><td>✖ No</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/gmail-ios.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Gmail</strong> webmail</td><td>27.8% *</td><td>✔ Yes</td><td>✖ No</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/gmail-webmail.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Outlook</strong> iOS 13</td><td>9.1% *</td><td>✔ Yes</td><td>✔ Yes</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/outlook-ios.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Outlook</strong> Android 10</td><td>9.1% *</td><td>✔ Yes</td><td>✔ Yes</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/outlook-android-1.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Outlook</strong> Windows 10</td><td>9.1% *</td><td>✔ Yes</td><td>✔ Yes</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/outlook-windows-10.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Outlook</strong> macOS</td><td>9.1% *</td><td>✔ Yes</td><td>✔ Yes</td><td>✔ Yes</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/outlook-macos.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Apple Mail</strong> macOS</td><td>7.5%</td><td>✔ Yes</td><td>✔ Yes</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/applemail-macos.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Yahoo!</strong> webmail</td><td>6.3% *</td><td>✔ Yes</td><td>✖ No</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/yahoo-webmail.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>AOL</strong> webmail</td><td>6.3% *</td><td>✖ No</td><td>✖ No</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/aol-webmail.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Outlook.com</strong> webmail</td><td>2.3%</td><td>✔ Yes</td><td>✔ Yes</td><td>✔ Yes</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/outlook-webmail.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Windows 10 Mail</strong> Windows 10</td><td>0.5%</td><td>✔ Yes</td><td>✔ Yes</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/windows-10-mail.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Zoho Mail</strong> webmail</td><td>less than 0.5%</td><td>✔ Yes</td><td>✔ Yes</td><td>✖ No</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/zoho-webmail.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Mozilla Thunderbird</strong> Windows 10</td><td>less than 0.5%</td><td>✔ Yes</td><td>✖ No</td><td>✔ Yes</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/thunderbird-windows-10.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Spark</strong> macOS</td><td>less than 0.5%</td><td>✔ Yes</td><td>✔ Yes</td><td>✔ Yes</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/spark-macos-dark-mode.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Spark</strong> iOS 13</td><td>less than 0.5%</td><td>✔ Yes</td><td>✔ Yes</td><td>✔ Yes</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/spark-ios-dark-mode.png">(Show screenshot)</a></td></tr>
<tr>
<td><strong>Spark</strong> Android 9</td><td>less than 0.5%</td><td>✔ Yes</td><td>✔ Yes</td><td>✔ Yes</td><td><a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/spark-android-dark-mode.png">(Show screenshot)</a></td></tr>
</tbody>
</table>
</div><p><em>* Popularity is shared across all platforms for the same email client because it cannot be reliably distinguished. Popularity source:</em> <a target="_blank" href="https://litmus.com/blog/infographic-the-2019-email-client-market-share"><em>Litmus, the 2019 email client market share</em></a><em>.</em></p>
<p>(<a target="_blank" href="https://sidemail.io/articles/dark-mode-in-html-email/">Visit the original post</a> to view my notes from the testing, and to see the latest tests as I gradually test more email clients and update the article there first.)</p>
<h2 id="heading-how-to-make-html-emails-dark-mode-friendly">How to make HTML emails dark mode friendly</h2>
<p>I already put the data to use, and a few Outlook related challenges later, I made our emails dark mode friendly. <strong>Here’s how you can do the same:</strong></p>
<blockquote>
<p><strong>What the data say:</strong><br>More than 55% of emails might be opened with dark mode enabled. Once Gmail joins the dark side, emails that might be opened with dark mode enabled will skyrocket to 83%!</p>
</blockquote>
<h3 id="heading-1-adjusting-colors">1) Adjusting colors</h3>
<p>Look out for Apple Mail, as it inverts colors only if the background color is transparent or unspecified — <strong>white background won’t do</strong>. The easiest way to make sure your emails won’t blind anybody is to check whether a background color is specified. For more control over the design, this is where <code>prefers-color-scheme</code> comes in handy.</p>
<p><strong>Syntax (@media prefers-color-scheme):</strong></p>
<pre><code class="lang-css">&lt;<span class="hljs-selector-tag">style</span>&gt;
    <span class="hljs-comment">/* Your light mode (default) styles: */</span>
    <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">background</span>: white;
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#393939</span>;
    }

    <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
        <span class="hljs-comment">/* Your dark mode styles: */</span>

        <span class="hljs-selector-tag">body</span> {
            <span class="hljs-attribute">background</span>: black;
            <span class="hljs-attribute">color</span>: <span class="hljs-number">#ccc</span>;
        }
    }
&lt;/<span class="hljs-selector-tag">style</span>&gt;
</code></pre>
<p><strong>A design tip:</strong> Avoid pure white <code>#fff</code> as the text color. I found that contrast ratio around 11.5 for the main text is a nice compromise between not too bright and not too dim. Check the contrast ratio here: <a target="_blank" href="https://contrast-ratio.com/">https://contrast-ratio.com</a> or use Chrome dev tools.</p>
<p><img src="https://sidemail.io/static/switching-logo-ccb909c2e5c3de55aeb36e5e69ca4d8b.svg" alt="Switching between light and dark logo version in HTML email with prefers-color-scheme media query" width="600" height="400" loading="lazy"></p>
<h3 id="heading-2-switching-between-light-and-dark-logo">2) Switching between light and dark logo</h3>
<p>A dark text on a dark background is pretty much invisible, and that’s precisely what happens to a logo if viewed in an email client with enabled dark mode.</p>
<p>Nowadays, a typical logo usually has a transparent background, colorful icon, and dark copy. See the problem? Because email clients don’t invert image colors, you need to handle it yourself.</p>
<p>To tackle this, you can either:</p>
<ol>
<li>save the logo with a white background instead of a transparent background (the easiest way to fix this). But I wouldn’t recommend this approach — dark mode users won’t be happy.</li>
<li>put a light logo on a dark background, and keep the rest of the email on a white background (<a target="_blank" href="https://sidemail.io/assets/dark-mode-in-html-email/litmus-light-logo-on-dark-background.png">see how Litmus does it</a>).</li>
<li>make dark mode email your default. A good candidate for this would be Spotify as they only offer a dark theme in their apps.</li>
<li>include both light and dark versions of your logo and switch between with <code>prefers-color-scheme</code> media query</li>
</ol>
<p>My favorite is the last approach, so here’s how you do it:</p>
<p>A simple <code>"display: none"</code> on the dark logo works just fine in all modern email clients. But to everyone's surprise, it doesn't work in Outlook and Windows 10 Mail.</p>
<p>In CSS styles:</p>
<pre><code class="lang-css">&lt;<span class="hljs-selector-tag">style</span>&gt;
    <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
        <span class="hljs-selector-class">.darkLogo</span> {
            <span class="hljs-attribute">display</span>: none <span class="hljs-meta">!important</span>;
        }

        <span class="hljs-selector-class">.lightLogoWrapper</span>,
        <span class="hljs-selector-class">.lightLogo</span> {
            <span class="hljs-attribute">display</span>: block <span class="hljs-meta">!important</span>;
        }
    }
&lt;/<span class="hljs-selector-tag">style</span>&gt;
</code></pre>
<p>…and the HTML structure:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">image</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"dark-logo.png"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"darkLogo"</span> /&gt;</span>

<span class="hljs-comment">&lt;!--
    To hide the light logo perfectly in Outlook and Windows 10 Mail, 
    you need to wrap the light logo image tag with a div.
--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"lightLogoWrapper"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"mso-hide: all; display: none"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">image</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"light-logo.png"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"lightLogo"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"display: none"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>This approach works pretty well, but it still won’t work correctly across the board. The dark text on dark background issue will happen with email clients that do support dark mode but don't support <code>prefers-color-scheme</code>. That is Outlook, Windows 10 Mail, Zoho, and potentially Gmail.</p>
<p><img src="https://sidemail.io/static/switching-logo-bulletproof-00ecb22b52186ddbf8bf6deec5c9b9f5.svg" alt="Bulletproof method: switching between light and dark logo version in HTML email with prefers-color-scheme media query" width="600" height="400" loading="lazy"></p>
<p>So, to make the logo in email fully bulletproof, I’ll combine methods 1 and 4 from above. Method 1 will cover all email clients that support dark mode, but not the <code>prefers-color-scheme</code>. And method 4 will cover Apple Mail, Outlook on MacOS, and Outlook.com, which does support both.</p>
<p>Also, instead of saving the logo on a white background, I’ll add a 3-pixel wide background-matching border and save it on a transparent background as usual.</p>
<p>It’s starting look pretty complex (just for a logo), so let’s see the HTML markup first:</p>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- Default logo with 3-pixel wide background-matching border --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">image</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"dark-logo-with-background.png"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"darkLogoDefault"</span> /&gt;</span>

<span class="hljs-comment">&lt;!-- Light theme (so dark logo): 
This is for Apple Mail, Outlook on macOS, Outlook.com --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"darkLogoWrapper"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"mso-hide: all; display: none"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">image</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"dark-logo.png"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"darkLogo"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"display: none"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-comment">&lt;!-- Dark theme (so light logo): 
This is for Apple Mail, Outlook on macOS, Outlook.com --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"lightLogoWrapper"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"mso-hide: all; display: none"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">image</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"light-logo.png"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"lightLogo"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"display: none"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>…and CSS styles:</p>
<pre><code class="lang-css">&lt;<span class="hljs-selector-tag">style</span>&gt;
    <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> light) {
        <span class="hljs-selector-class">.darkLogoDefault</span>,
        <span class="hljs-selector-class">.lightLogo</span> {
            <span class="hljs-attribute">display</span>: none <span class="hljs-meta">!important</span>;
        }

        <span class="hljs-selector-class">.darkLogoWrapper</span>,
        <span class="hljs-selector-class">.darkLogo</span> {
            <span class="hljs-attribute">display</span>: block <span class="hljs-meta">!important</span>;
        }
    }

    <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
        <span class="hljs-selector-class">.darkLogoDefault</span>,
        <span class="hljs-selector-class">.darkLogo</span> {
            <span class="hljs-attribute">display</span>: none <span class="hljs-meta">!important</span>;
        }

        <span class="hljs-selector-class">.lightLogoWrapper</span>,
        <span class="hljs-selector-class">.lightLogo</span> {
            <span class="hljs-attribute">display</span>: block <span class="hljs-meta">!important</span>;
        }
    }
&lt;/<span class="hljs-selector-tag">style</span>&gt;
</code></pre>
<h2 id="heading-dark-mode-in-gmail-is-coming">Dark mode in Gmail is coming</h2>
<p>Dark mode is coming to Android in the new Android 10, and Gmail should go completely dark too, finally. All you need is Android 10 and the newest Gmail (at least version 2019.09.01.268168002). However, Google tends to enable new features (a dark theme in this case) for users gradually with a server-side push, and I haven’t had luck with it for now.</p>
<p>I’m curious to see if support for <code>@media prefers-color-scheme</code> is coming to Gmail. From what I read, it doesn’t seem promising. I guess we have to wait to find out. I’ll update the article once I get the dark theme in Gmail enabled.</p>
<h2 id="heading-wrapping-up">Wrapping up</h2>
<p>Dark mode is coming to HTML email, and I love it! But, it’s another thing to worry about – like using HTML tables for layout wasn’t enough.</p>
<p><a target="_blank" href="https://hosted.sidemail.io/5d919d2fcc34a000fc97cfed">Stay up-to-date about the dark mode in email by joining our mailing list</a>. We also share there insights and challenges we face while building and growing our SaaS product — <a target="_blank" href="https://sidemail.io/">Sidemail</a>.</p>
<p>Thanks for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How I added Dark Mode to my website ]]>
                </title>
                <description>
                    <![CDATA[ I recently redesigned my website. Here are 2 pictures of how it looked, for reference: I designed this website almost 1 year ago, and did many changes along the way, just as we do with any website. Eventually I grew tired of the design: title is to... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-i-added-dark-mode-to-my-website-33611d246425/</link>
                <guid isPermaLink="false">66bb5a6e24dd38751ddeb5cf</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ dark mode ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ UX ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Flavio Copes ]]>
                </dc:creator>
                <pubDate>Mon, 04 Feb 2019 15:42:31 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*izsJBvK2z3sNZvkh.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>I recently redesigned <a target="_blank" href="https://flaviocopes.com">my website</a>. Here are 2 pictures of how it <em>looked</em>, for reference:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/omFzYPaJhb-CmAV7a0zLCSz00Ac31sfbUeV4" alt="Image" width="800" height="880" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/roWIFwsXoad3bhcoXrEUXVLarbvX9UB2vbgQ" alt="Image" width="800" height="880" loading="lazy"></p>
<p>I designed this website almost 1 year ago, and did many changes along the way, just as we do with any website.</p>
<p>Eventually I grew tired of the design: title is too big, too much space lost instead of showing the content right away, and so on.</p>
<p>I sat down yesterday evening and started re-imagining the website, and I finished the redesign this morning:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/WJZ6y3YnXqUNhJ4Ceav4LAAd5jFDc63TmQNw" alt="Image" width="800" height="880" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/AwAm1Tjr6abfHw5kPGo-9QGoIweVIOJr4oI5" alt="Image" width="800" height="880" loading="lazy"></p>
<p>Much better! Content, the most important thing, is more prominent.</p>
<p>I used a monospaced font (Inconsolata) because as a programming blog it’s a nice one, despite the reduced readability and increased page size due to the font use, because I <em>want</em> that font on my site. I like it better, and since my site is a big part of my day to day activity, I wanted it to be what I want.</p>
<p>I just missed one thing: <strong>dark mode</strong>. As I was in the process of redesigning, I had the dark mode option in mind.</p>
<p>How did I do it? First, I added the Moon Emoji ? in the sidebar, as a way to let people change the mode from light to dark.</p>
<p>Then, I added a JavaScript snippet that runs when it’s clicked. I just added it to the <code>onclick</code> event handler inline in the HTML, without going fancier:</p>
<pre><code>&lt;p&gt;  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"localStorage.setItem('mode', (localStorage.getItem('mode') || 'dark') === 'dark' ? 'light' : 'dark'); localStorage.getItem('mode') === 'dark' ? document.querySelector('body').classList.add('dark') : document.querySelector('body').classList.remove('dark')"</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Dark/light&lt;/p&gt;</span></span></span>
</code></pre><p>This is the JavaScript that runs in the onclick:</p>
<pre><code><span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">'mode'</span>, (<span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">'mode'</span>) || <span class="hljs-string">'dark'</span>) === <span class="hljs-string">'dark'</span> ? <span class="hljs-string">'light'</span> : <span class="hljs-string">'dark'</span>); <span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">'mode'</span>) === <span class="hljs-string">'dark'</span> ? <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'body'</span>).classList.add(<span class="hljs-string">'dark'</span>) : <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'body'</span>).classList.remove(<span class="hljs-string">'dark'</span>)
</code></pre><p>It’s a bit convoluted, but basically I check if the <code>mode</code> property in the <a target="_blank" href="https://flaviocopes.com/web-storage-api/">local storage</a> is ‘dark’ (and defaults to dark if it’s not set yet, using the <code>||</code> operator), and I set the opposite of that in the local storage.</p>
<p>Then I assign the <code>dark</code> class to the <code>body</code> HTML element, so we can use CSS to style the page in dark mode.</p>
<p>Another script runs as soon as the DOM loads, and checks if the mode is dark. If so, it adds the <code>dark</code> class to the <code>body</code> HTML element:</p>
<pre><code><span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">'DOMContentLoaded'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {  ((<span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">'mode'</span>) || <span class="hljs-string">'dark'</span>) === <span class="hljs-string">'dark'</span>) ? <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'body'</span>).classList.add(<span class="hljs-string">'dark'</span>) : <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'body'</span>).classList.remove(<span class="hljs-string">'dark'</span>)})
</code></pre><p>Now if people change modes, their choice will be remembered next time they load the page.</p>
<p>Then I added a lot of CSS instructions to the CSS, all prefixed with <code>body.dark</code>. Like these:</p>
<pre><code>body.dark {  background-color: rgb(<span class="hljs-number">30</span>,<span class="hljs-number">34</span>,<span class="hljs-number">39</span>);  color: #fff;}body.dark code[<span class="hljs-class"><span class="hljs-keyword">class</span>*</span>=language-],body.dark table tbody&gt;tr:nth-child(odd)&gt;td,body.dark table tbody&gt;tr:nth-child(odd)&gt;th {  <span class="hljs-attr">background</span>: #<span class="hljs-number">282</span>c34}
</code></pre><p>Now things should already be working! Here is my site in dark mode:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/qLGFEyXtuhIuhkoxWZUHeMxsO979cAzAyZwG" alt="Image" width="800" height="880" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/fopjlWMiRnntpt8x-k6DlWrzZKHagsSftcRT" alt="Image" width="800" height="880" loading="lazy"></p>
<p>I added the <code>dark</code> class to the <code>body</code> element by default, to make dark mode the default:</p>
<pre><code>&lt;body <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"dark"</span>&gt; ... &amp;lt;/body&gt;
</code></pre><p>Why? First, I liked it better. Then, I made a poll on Twitter and people liked it better.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/gl9vci-v5yBlwHtnVsqkgn8EhphopCHUeTY3" alt="Image" width="626" height="191" loading="lazy"></p>
<p>But also for a technical reason, a very simple one actually. I don’t store the user choice server-side, so I have no way to know the mode until the local storage is available.</p>
<p>I could do that if the site was generated server-side, but it’s a static site, so I always serve the same page to everyone that requests it. Even if I got a cookie, I have no place to process it (on the flip side this means my pages load faster).</p>
<p>So when someone navigates to another page on my site, or loads the page for the first time on a second visit, I don’t want to show a bright page while I determine the mode. Maybe the visitor is coding in the middle of the night in a dark room.</p>
<p>I’d rather do that in light mode: show a dark page for a couple milliseconds and then turn it white again.</p>
<p>The <em>Media Queries Level 5</em> specification, still in work in progress, contains a new <code>[prefers-color-scheme](https://drafts.csswg.org/mediaqueries-5/#prefers-color-scheme)</code> media feature. <a target="_blank" href="https://developer.apple.com/safari/technology-preview/">Safari Technology Preview</a> on macOS already supports it and we can use it to tell if the user is browsing the page in dark or light mode:</p>
<pre><code>@media (prefers-color-scheme: dark) {  body {    background-color: black;    color: white;  }}@media (prefers-color-scheme: light) {  body {    background-color: white;    color: black;  }}
</code></pre><p>Hopefully this is going to be stable in Safari soon, and in the future Chrome and Firefox will support it too.</p>
<p><em>Originally published at <a target="_blank" href="https://flaviocopes.com/dark-mode/">flaviocopes.com</a>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
