{"id":93492,"date":"2026-03-28T17:14:34","date_gmt":"2026-03-28T22:14:34","guid":{"rendered":"https:\/\/www.bricktowntom.com\/blog\/?p=93492"},"modified":"2026-03-28T17:14:34","modified_gmt":"2026-03-28T22:14:34","slug":"8-css-javascript-snippets-for-switching-between-dark-or-light-modes","status":"publish","type":"post","link":"https:\/\/www.bricktowntom.com\/blog\/03\/8-css-javascript-snippets-for-switching-between-dark-or-light-modes.html","title":{"rendered":"8 CSS &amp; JavaScript Snippets for Switching between Dark or Light Modes"},"content":{"rendered":"<p>Web designers are increasingly adding multiple contrast modes to their projects. This provides users with the ability to view a website in their preferred color scheme.<\/p>\n<p>In practice, this tends to result in the offering of both dark and light modes. But it&#8217;s not just for cosmetic purposes. Contrast plays a huge role in accessibility. For instance, some users with <a href=\"https:\/\/speckyboy.com\/website-accessible-visually-impaired\/\" target=\"_blank\" rel=\"noopener\">visual impairment<\/a> will find a dark color scheme easier to read.<\/p>\n<p>Designers are also taking a user&#8217;s system preferences into account. Some websites will now detect whether a user&#8217;s operating system is set to use a dark or light color scheme \u2013 then serve up the appropriate styles.<\/p>\n<p>Still, it&#8217;s never a good idea to <em>force<\/em> users into a particular contrast mode. That&#8217;s why it&#8217;s important to provide a way to toggle between them. And that functionality is our focus for today.<\/p>\n<p>Here are eight unique CSS and JavaScript code snippets for switching between dark and light modes.<\/p>\n<h2>Light,  Dark or Black Theme <small>by H\u00e5vard Brynjulfsen<\/small><\/h2>\n<p>This settings panel provides three distinct color modes and is beautifully designed. The transition between modes is smooth, allowing for less-jarring changes. The menu uses HTML radio buttons that have been styled into a <a href=\"https:\/\/speckyboy.com\/toggle-switch-css\/\" target=\"_blank\" rel=\"noopener\">toggle switch<\/a>. It&#8217;s simple, attractive, and functional.<\/p>\n<p class=\"codepen\" style=\"height:600px;align-items:center;justify-content:center;border:2px solid;margin:1em 0;padding:1em\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"dyOJyje\" data-preview=\"true\" data-user=\"havardob\">See the Pen <a href=\"https:\/\/codepen.io\/havardob\/pen\/dyOJyje\" target=\"_blank\" rel=\"noopener\">Light \/ Dark \/ Black Theme<\/a> by H\u00e5vard Brynjulfsen<\/p>\n<h2>Easy Dark Mode with SASS <small>by Kaio Almeida<\/small><\/h2>\n<p>A simple checkbox powers this contrast mode toggle. From there, JavaScript is used to add a <code>data-theme<\/code> attribute to the page&#8217;s HTML tag. SASS then looks for the attribute&#8217;s value and styles the content accordingly.<\/p>\n<p class=\"codepen\" style=\"height:600px;align-items:center;justify-content:center;border:2px solid;margin:1em 0;padding:1em\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"MdvWmg\" data-preview=\"true\" data-user=\"KaioRocha\">See the Pen <a href=\"https:\/\/codepen.io\/KaioRocha\/pen\/MdvWmg\" target=\"_blank\" rel=\"noopener\">Easy Dark Mode with SASS<\/a> by Kaio Almeida<\/p>\n<h2>Light or Dark Mode <small>by \u00c1lex<\/small><\/h2>\n<p>Why not have some fun with the concept of switching contrast modes? This snippet features SVG images and animation to create a unique day-to-night toggle. Note that while it visually conveys the message, it may benefit from some accessibility enhancements before being put into a production environment.<\/p>\n<p class=\"codepen\" style=\"height:600px;align-items:center;justify-content:center;border:2px solid;margin:1em 0;padding:1em\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"ZEbWBjp\" data-preview=\"true\" data-user=\"lerida\">See the Pen <a href=\"https:\/\/codepen.io\/lerida\/pen\/ZEbWBjp\" target=\"_blank\" rel=\"noopener\">Light \/ Dark Mode<\/a> by \u00c1lex<\/p>\n<h2>CSS Theme Switcher <small>by Michelle Barker<\/small><\/h2>\n<p>Several snippets in this collection use JavaScript \u2013 but CSS is capable of handling the task alone. This example not only looks good but also implements an accessible HTML form to power the mode switch. When you create functionality that everyone can use, it&#8217;s a win-win situation.<\/p>\n<p class=\"codepen\" style=\"height:600px;align-items:center;justify-content:center;border:2px solid;margin:1em 0;padding:1em\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"GzzrGJ\" data-preview=\"true\" data-user=\"michellebarker\">See the Pen <a href=\"https:\/\/codepen.io\/michellebarker\/pen\/GzzrGJ\" target=\"_blank\" rel=\"noopener\">CSS Theme Switcher<\/a> by Michelle Barker<\/p>\n<h2>GitHub Dark Mode Toggle <small>by Chintu Yadav Sara<\/small><\/h2>\n<p>While this oversized toggle maintains its white background throughout, it does change icons along with the page&#8217;s contrast mode. Also, note that the various background shapes turn into bright <a href=\"https:\/\/speckyboy.com\/neon-effects-web-design\/\" target=\"_blank\" rel=\"noopener\">neon colors<\/a> while in dark mode. This adds both fun and context to the presentation.<\/p>\n<p class=\"codepen\" style=\"height:600px;align-items:center;justify-content:center;border:2px solid;margin:1em 0;padding:1em\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"yLaaVYa\" data-preview=\"true\" data-user=\"chintuyadav\">See the Pen <a href=\"https:\/\/codepen.io\/chintuyadav\/pen\/yLaaVYa\" target=\"_blank\" rel=\"noopener\">GitHub Dark Mode Toggle<\/a> by Chintu Yadav Sara<\/p>\n<h2>Persistent Dark Mode <small>by Brian Haferkamp<\/small><\/h2>\n<p>If you&#8217;re looking for simplicity, this color mode button will do the job. No fancy icons (although you can certainly add them in) or wild animation. Just a single click and some smooth CSS transitions. In addition, there&#8217;s a handy bit of JavaScript that will save the user&#8217;s preference in local storage.<\/p>\n<p class=\"codepen\" style=\"height:600px;align-items:center;justify-content:center;border:2px solid;margin:1em 0;padding:1em\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"WNrJmZb\" data-preview=\"true\" data-user=\"brianhaferkamp\">See the Pen <a href=\"https:\/\/codepen.io\/brianhaferkamp\/pen\/WNrJmZb\" target=\"_blank\" rel=\"noopener\">Persistent Dark Mode<\/a> by Brian Haferkamp<\/p>\n<h2>Basic Vue Reactivity <small>by CodePen<\/small><\/h2>\n<p>As we&#8217;re seeing more JavaScript-driven UIs these days, it&#8217;s only fitting that we have such an example. Here, a Vue component lets users switch contrast modes via a simple checkbox. This one has plenty of potential for <a href=\"https:\/\/speckyboy.com\/css-snippets-html-checkboxes-radio-buttons\/\" target=\"_blank\" rel=\"noopener\">dressing up<\/a> via CSS.<\/p>\n<p class=\"codepen\" style=\"height:600px;align-items:center;justify-content:center;border:2px solid;margin:1em 0;padding:1em\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"MRoLdr\" data-preview=\"true\" data-user=\"codepen\">See the Pen <a href=\"https:\/\/codepen.io\/team\/codepen\/pen\/MRoLdr\" target=\"_blank\" rel=\"noopener\">Basic Vue Reactivity<\/a> by CodePen<\/p>\n<h2>Dark Mode <small>by Airen<\/small><\/h2>\n<p>For projects that could use a bit more creativity, this swinging-lightbulb toggle should provide plenty of inspiration. To keep things accessible, the bulb image is placed inside an HTML <code>button<\/code> element. Animation is provided through CSS. It&#8217;s a great way to spice up a portfolio or blog.<\/p>\n<p class=\"codepen\" style=\"height:600px;align-items:center;justify-content:center;border:2px solid;margin:1em 0;padding:1em\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"dyYNPWQ\" data-preview=\"true\" data-user=\"airen\">See the Pen <a href=\"https:\/\/codepen.io\/airen\/pen\/dyYNPWQ\" target=\"_blank\" rel=\"noopener\">Dark Mode<\/a> by Airen<\/p>\n<h2>Come to the Dark Mode (Or Light &#8211; It&#8217;s Your Choice)<\/h2>\n<p>Adding color contrast modes to your website makes sense in several scenarios. It allows designers to offer users their choice of aesthetic while also making content more accessible.<\/p>\n<p>As the snippets above demonstrate, there is no shortage of ways to implement this feature. CSS allows for near-limitless styling options, while JavaScript can provide key functionality where needed.<\/p>\n<p>We hope this roundup serves as an example of what is possible when it comes to color contrast modes. And if you&#8217;d like to see even more snippets, check out our <a href=\"https:\/\/codepen.io\/collection\/OLpwoW\" target=\"_blank\" rel=\"noopener\">CodePen collection<\/a>!<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/speckyboy.com\/css-javascript-snippets-dark-light-mode\/\">8 CSS &#038; JavaScript Snippets for Switching between Dark or Light Modes<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/speckyboy.com\">Speckyboy Design Magazine<\/a>.<\/p>\n<p>Source: Specky Boy<\/p>\n<p id=\"kc_opp\"><small>Republished by  <a href=\"http:\/\/www.blogtrafficexchange.com\/\">Blog Post Promoter<\/a><\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Web designers are increasingly adding multiple contrast modes to their projects. This provides users with the ability to view a website in their &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"false","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[128],"class_list":["post-93492","post","type-post","status-publish","format-standard","hentry","category-affiliate-marketing","tag-advantage"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3k0YU-ojW","jetpack-related-posts":[{"id":93609,"url":"https:\/\/www.bricktowntom.com\/blog\/03\/weekly-news-for-designers-%e2%84%96-658.html","url_meta":{"origin":93492,"position":0},"title":"Weekly News for Designers \u2116 658","author":"admin","date":"March 22, 2026","format":false,"excerpt":"The 10 Keyboard Shortcuts That Will Give You Figma Superpowers \u2013 Increase your efficiency with this collection of handy shortcuts. CSS Grid and Custom Shapes \u2013 Learn how to create a uniquely-shaped layout with this tutorial. Everything Developers Must Know About Figma \u2013 A look at how the design tool\u2026","rel":"","context":"In &quot;Affiliate Marketing&quot;","block_context":{"text":"Affiliate Marketing","link":"https:\/\/www.bricktowntom.com\/blog\/category\/affiliate-marketing"},"img":{"alt_text":"Envato Elements","src":"https:\/\/i0.wp.com\/speckyboy.com\/wp-content\/uploads\/2019\/08\/envato-elements-weekly-news.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":93220,"url":"https:\/\/www.bricktowntom.com\/blog\/03\/accessibility-tip-how-to-test-color-contrast-ratios.html","url_meta":{"origin":93492,"position":1},"title":"Accessibility Tip: How to Test Color Contrast Ratios","author":"admin","date":"March 15, 2026","format":false,"excerpt":"There are several key ingredients required to build an accessible website. Each has a role to play in ensuring that all users can navigate and consume a site\u2019s content. Color is perhaps the foundational element. Because, regardless of the other steps you take, an inaccessible color palette severely undermines usability.\u2026","rel":"","context":"In &quot;Affiliate Marketing&quot;","block_context":{"text":"Affiliate Marketing","link":"https:\/\/www.bricktowntom.com\/blog\/category\/affiliate-marketing"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.bricktowntom.com\/blog\/wp-content\/uploads\/2022\/06\/color-contrast-testing-04.jpg?fit=900%2C400&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.bricktowntom.com\/blog\/wp-content\/uploads\/2022\/06\/color-contrast-testing-04.jpg?fit=900%2C400&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.bricktowntom.com\/blog\/wp-content\/uploads\/2022\/06\/color-contrast-testing-04.jpg?fit=900%2C400&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.bricktowntom.com\/blog\/wp-content\/uploads\/2022\/06\/color-contrast-testing-04.jpg?fit=900%2C400&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":92740,"url":"https:\/\/www.bricktowntom.com\/blog\/03\/weekly-news-for-designers-%e2%84%96-640.html","url_meta":{"origin":93492,"position":2},"title":"Weekly News for Designers \u2116 640","author":"admin","date":"March 29, 2026","format":false,"excerpt":"Ideal SVG exports \u2013 Some thoughts regarding how design tools should export the vector format. On-Scroll Text Repetition Animation \u2013 This tutorial demonstrates an on-scroll animation that shows repeated fragments of big text. The Anatomy of a Design System \u2013 Defining a shared understanding of how systems connect core brand\u2026","rel":"","context":"In &quot;Affiliate Marketing&quot;","block_context":{"text":"Affiliate Marketing","link":"https:\/\/www.bricktowntom.com\/blog\/category\/affiliate-marketing"},"img":{"alt_text":"Envato Elements","src":"https:\/\/i0.wp.com\/speckyboy.com\/wp-content\/uploads\/2019\/08\/envato-elements-weekly-news.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":93384,"url":"https:\/\/www.bricktowntom.com\/blog\/03\/8-css-snippets-for-styling-html-checkboxes-radio-buttons.html","url_meta":{"origin":93492,"position":3},"title":"8 CSS Snippets for Styling HTML Checkboxes &amp; Radio Buttons","author":"admin","date":"March 22, 2026","format":false,"excerpt":"HTML form elements such as checkboxes and radio buttons are staples of the web. But for many years, web designers didn\u2019t do a whole lot to enhance them. That has changed quite a bit in recent times. Thanks to the power of CSS (and the occasional bit of JavaScript), these\u2026","rel":"","context":"In &quot;Affiliate Marketing&quot;","block_context":{"text":"Affiliate Marketing","link":"https:\/\/www.bricktowntom.com\/blog\/category\/affiliate-marketing"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":92247,"url":"https:\/\/www.bricktowntom.com\/blog\/03\/weekly-news-for-designers-%e2%84%96-632.html","url_meta":{"origin":93492,"position":4},"title":"Weekly News for Designers \u2116 632","author":"admin","date":"March 4, 2026","format":false,"excerpt":"20 Free High-Resolution Brush Packs for Adobe Illustrator \u2013 Add some unique elements to your Illustrator projects with these free brushes. Pagify \u2013 This online tool promises to turn your Google Doc into a mobile-friendly website. The Most Popular Front-end Frameworks in 2022 \u2013 A look at what\u2019s trending, along\u2026","rel":"","context":"In &quot;Affiliate Marketing&quot;","block_context":{"text":"Affiliate Marketing","link":"https:\/\/www.bricktowntom.com\/blog\/category\/affiliate-marketing"},"img":{"alt_text":"Envato Elements","src":"https:\/\/i0.wp.com\/speckyboy.com\/wp-content\/uploads\/2019\/08\/envato-elements-weekly-news.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":93265,"url":"https:\/\/www.bricktowntom.com\/blog\/03\/weekly-news-for-designers-%e2%84%96-650.html","url_meta":{"origin":93492,"position":5},"title":"Weekly News for Designers \u2116 650","author":"admin","date":"March 17, 2026","format":false,"excerpt":"Areas Where AI Can (and Already Is) Benefitting Web Designers \u2013 Artificial intelligence has the potential to make a web designer\u2019s job easier. Avatarg Figma Plugin \u2013 This Figma plugin lets you generate avatar images. Single Element Loaders: The Bars \u2013 Learn how to create CSS animated loading bars with\u2026","rel":"","context":"In &quot;Affiliate Marketing&quot;","block_context":{"text":"Affiliate Marketing","link":"https:\/\/www.bricktowntom.com\/blog\/category\/affiliate-marketing"},"img":{"alt_text":"Envato Elements","src":"https:\/\/i0.wp.com\/speckyboy.com\/wp-content\/uploads\/2019\/08\/envato-elements-weekly-news.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/posts\/93492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/comments?post=93492"}],"version-history":[{"count":1,"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/posts\/93492\/revisions"}],"predecessor-version":[{"id":93574,"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/posts\/93492\/revisions\/93574"}],"wp:attachment":[{"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/media?parent=93492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/categories?post=93492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bricktowntom.com\/blog\/wp-json\/wp\/v2\/tags?post=93492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}