{"id":592,"date":"2023-08-19T02:21:53","date_gmt":"2023-08-19T02:21:53","guid":{"rendered":"https:\/\/www.cssportal.com\/blog\/?p=592"},"modified":"2024-02-28T23:25:19","modified_gmt":"2024-02-28T23:25:19","slug":"css-dark-mode-guide","status":"publish","type":"post","link":"https:\/\/www.cssportal.com\/blog\/css-dark-mode-guide\/","title":{"rendered":"CSS Dark Mode Guide"},"content":{"rendered":"<p>Dark mode has become a popular feature in web design, providing users with a more comfortable browsing experience in low-light environments and reducing eye strain. In this tutorial, we will walk you through the process of implementing a dark mode using CSS.<\/p>\n<h3>Prerequisites<\/h3>\n<p>Basic understanding of HTML and CSS is required for this tutorial.<\/p>\n<h3>Step 1: Set Up Your HTML Structure<\/h3>\n<p>Create an HTML file (e.g., <code>index.html<\/code>) and set up the basic structure:<\/p>\n<div class=\"examples\">\n<div class=\"topic\">HTML<\/div>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=&quot;en&quot;&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\r\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot;&gt;\r\n    &lt;title&gt;Dark Mode Tutorial&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;header&gt;\r\n        &lt;h1&gt;Dark Mode Tutorial&lt;\/h1&gt;\r\n        &lt;button id=&quot;dark-mode-toggle&quot;&gt;Toggle Dark Mode&lt;\/button&gt;\r\n    &lt;\/header&gt;\r\n    &lt;main&gt;\r\n        &lt;!-- Your content goes here --&gt;\r\n    &lt;\/main&gt;\r\n    &lt;footer&gt;\r\n        &lt;!-- Footer content goes here --&gt;\r\n    &lt;\/footer&gt;\r\n    &lt;script src=&quot;script.js&quot;&gt;&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<\/div>\n<h3>Step 2: Create Your CSS Styles<\/h3>\n<p>Create a CSS file (e.g., <code>styles.css<\/code>) and define your default styles, including the styles for the light mode:<\/p>\n<div class=\"examples\">\n<div class=\"topic\">CSS<\/div>\n<pre><code class=\"language-css\">body {\r\n    font-family: Arial, sans-serif;\r\n    background-color: #ffffff;\r\n    color: #333333;\r\n    transition: background-color 0.3s, color 0.3s;\r\n}\r\n\r\nheader {\r\n    background-color: #f2f2f2;\r\n    padding: 1rem;\r\n    display: flex;\r\n    justify-content: space-between;\r\n    align-items: center;\r\n}\r\n\r\nbutton {\r\n    background-color: #007bff;\r\n    color: #ffffff;\r\n    border: none;\r\n    padding: 0.5rem 1rem;\r\n    border-radius: 4px;\r\n    cursor: pointer;\r\n}\r\n<\/code><\/pre>\n<\/div>\n<h3>Step 3: Implement Dark Mode CSS<\/h3>\n<p>Add the dark mode styles to your <code>styles.css<\/code> file. You can use a media query to apply these styles when the user has enabled dark mode:<\/p>\n<div class=\"examples\">\n<div class=\"topic\">CSS<\/div>\n<pre><code class=\"language-css\">\/* Dark mode styles *\/\r\nbody.dark-mode {\r\n    background-color: #333333;\r\n    color: #ffffff;\r\n}\r\n\r\nbody.dark-mode header {\r\n    background-color: #1a1a1a;\r\n}\r\n\r\nbody.dark-mode button {\r\n    background-color: #17a2b8;\r\n}\r\n<\/code><\/pre>\n<\/div>\n<h3>Step 4: Add JavaScript for Dark Mode Toggle<\/h3>\n<p>Create a JavaScript file (e.g., <code>script.js<\/code>) to handle the dark mode toggle functionality:<\/p>\n<div class=\"examples\">\n<div class=\"topic\">JavaScript<\/div>\n<pre><code class=\"language-javascript\">const darkModeToggle = document.getElementById(&#39;dark-mode-toggle&#39;);\r\nconst body = document.body;\r\n\r\ndarkModeToggle.addEventListener(&#39;click&#39;, () =&gt; {\r\n    body.classList.toggle(&#39;dark-mode&#39;);\r\n});\r\n<\/code><\/pre>\n<\/div>\n<h3>Step 5: Link JavaScript in HTML<\/h3>\n<p>Finally, link your JavaScript file at the bottom of your HTML body:<\/p>\n<div class=\"examples\">\n<div class=\"topic\">HTML<\/div>\n<pre><code class=\"language-html\">&lt;script src=&quot;script.js&quot;&gt;&lt;\/script&gt;\r\n<\/code><\/pre>\n<\/div>\n<h3>Conclusion<\/h3>\n<p>Congratulations! You&#39;ve successfully implemented dark mode using CSS in your web project. Users can now toggle between light and dark modes, providing a more versatile and user-friendly browsing experience.<\/p>\n<p>Feel free to further customize your dark mode styles and expand this tutorial to include more elements and features in your web application.<\/p>\n<p><a class=\"btn btn-primary\" href=\"https:\/\/www.cssportal.com\/blog-examples\/dark-mode.php\" target=\"_blank\" rel=\"noopener noreferrer\">View in Action<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dark mode has become a popular feature in web design, providing users with a more comfortable browsing experience in low-light environments and reducing eye strain. In this tutorial, we will walk you through the process of implementing a dark mode using CSS. Prerequisites Basic understanding of HTML and CSS is required for this tutorial. Step [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":645,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-592","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","wpautop"],"_links":{"self":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/comments?post=592"}],"version-history":[{"count":1,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":593,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/592\/revisions\/593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/media\/645"}],"wp:attachment":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/media?parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/categories?post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/tags?post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}