{"id":1263,"date":"2026-07-08T06:15:16","date_gmt":"2026-07-07T23:15:16","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/08\/how-to-implement-dark-mode-with-css-a-simple-tutorial\/"},"modified":"2026-07-08T06:15:16","modified_gmt":"2026-07-07T23:15:16","slug":"how-to-implement-dark-mode-with-css-a-simple-tutorial","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/08\/how-to-implement-dark-mode-with-css-a-simple-tutorial\/","title":{"rendered":"How to Implement Dark Mode with CSS: A Simple Tutorial"},"content":{"rendered":"<h1>How to Implement Dark Mode with CSS: A Simple Tutorial<\/h1>\n<p>Dark mode reduces eye strain and saves battery on OLED screens. With modern CSS, you can easily add dark mode support using custom properties and the <code>prefers-color-scheme<\/code> media query. This tutorial walks you through the process step by step.<\/p>\n<p>The core idea is to define color variables in <code>:root<\/code> for a light theme, then automatically override them inside a media query that detects the user&#8217;s system preference for dark mode. This requires no JavaScript for the basic auto-switching behavior.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20implement%20dark%20mode%20with%20CSS\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>1. Define CSS Custom Properties for Colors<\/h2>\n<p>Start by declaring your theme colors as CSS custom properties in the <code>:root<\/code> pseudo-class. Use descriptive variable names like <code>--bg<\/code> and <code>--text<\/code> so you can reuse them throughout your stylesheet.<\/p>\n<ul>\n<li><code>:root { --bg: #ffffff; --text: #000000; }<\/code><\/li>\n<li>Add more variables as needed (e.g., <code>--accent<\/code>, <code>--border<\/code>).<\/li>\n<\/ul>\n<h2>2. Add the Dark Mode Media Query<\/h2>\n<p>Create a media query for <code>prefers-color-scheme: dark<\/code>. Inside it, reassign the same variables with darker colors. This overrides the variables when the user\u2019s OS is set to dark mode.<\/p>\n<ul>\n<li><code>@media (prefers-color-scheme: dark) { :root { --bg: #121212; --text: #e0e0e0; } }<\/code><\/li>\n<li>You can also add a <code>light<\/code> query for explicit control.<\/li>\n<\/ul>\n<h2>3. Apply the Variables to Your Elements<\/h2>\n<p>Use <code>var(--bg)<\/code> and <code>var(--text)<\/code> for background and text colors on your <code>body<\/code> and other elements. Your site will now automatically switch themes based on the user\u2019s system setting.<\/p>\n<ul>\n<li><code>body { background-color: var(--bg); color: var(--text); }<\/code><\/li>\n<li>Extend to other elements like cards, buttons, etc.<\/li>\n<\/ul>\n<p>That\u2019s it! With just a handful of CSS rules, you\u2019ve implemented a clean, accessible dark mode. For a manual toggle, add a small JavaScript snippet to toggle a class \u2013 but for most sites, the auto approach is the best starting point.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Implement Dark Mode with CSS: A Simple Tutorial Dark mode reduces eye strain and saves battery on OLED screens. With modern CSS, you can easily add dark mode support using custom properties and the prefers-color-scheme media query. This tutorial walks you through the process step by step. The core idea is to define &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-1263","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1263"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1263\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}