Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en" >
<head>
<meta charset="UTF-8">
<title>Dark/Light Mode Pattern</title>
<link rel="stylesheet" href="style.css">
<link id="favicon" rel="icon" href="favicon-dark.svg" color="#DDD" />
<meta name="viewport" content="width=device-width, maximum-scale=1.0, minimum-scale=1.0, initial-scale=1.0, user-scalable=no" />
</head>
<body class="dark">

<div class="load-flash"></div>

<div class="mode-toggle noselect">
	<h6 class="label-dark">Dark</h6>
	<div class="toggle-switch"></div>
	<h6 class="label-light">Light</h6>
</div>

<script src="jquery.js"></script>
<script src="script.js"></script>

</body>
</html>
              
            
!

CSS

              
                :root {
	--gray-5:#010101; /*dark*/
	--gray-10:#040404;
	--gray-15:#070808;
	--gray-20:#0D0D0E;
	--gray-25:#141516;
	--gray-30:#1E2022;
	--gray-35:#2D2F31;
	--gray-40:#404346;
	--gray-45:#5B5E62;
	--gray-50:#7C8084;
	--gray-55:#9DA1A5;
	--gray-60:#B9BDC1;
	--gray-65:#CDD1D5;
	--gray-70:#DCE0E3;
	--gray-75:#E7EAED;
	--gray-80:#EFF2F5;
	--gray-85:#F4F7FA;
	--gray-90:#F7FAFD;
	--gray-95:#FCFEFE; /*light*/
	--animate:200ms;
}
body {
	margin:0;
	padding:40px;
	color:var(--gray-40);
	font-family:"Arial";
	background:var(--gray-70);
	-webkit-font-smoothing:antialiased;
	transition:color var(--animate) ease-in-out, background var(--animate) ease-in-out;
}
body.dark {
	color:var(--gray-80);
	background:var(--gray-35);
}
h6 {
	color:var(--gray-40);
	transition:color var(--animate) ease-in-out;
}
body.dark h6 {
	color:var(--gray-80);
}
.load-flash {
	z-index:1000;
	top:0;
	left:0;
	right:0;
	bottom:0;
	position:absolute;
	background:var(--gray-35);
}
.mode-toggle {
	display:inline-flex;
	flex-direction:row;
	align-items:center;
}
.label-dark {
	letter-spacing:-0.15px;
}
.label-light {
	letter-spacing:-0.031px;
}
.mode-toggle h6,
body.dark .mode-toggle h6 {
	text-transform:uppercase;
}
.label-dark,
.label-light {
	padding:8px 12px;
	min-width:32px;
	text-align:center;
	color:var(--gray-45);
}
body.dark .label-dark,
body.dark .label-light {
	color:var(--gray-60);
}
.label-dark:hover {
	color:var(--gray-40);
}
.label-dark:active {
	color:var(--gray-5);
}
body.dark .label-dark {
	pointer-events:none;
}
body.dark .label-light:hover {
	color:var(--gray-95);
}
body.dark .label-light:active {
	color:var(--gray-65);
}
.toggle-switch {
	margin:0;
	padding:0;
	width:48px;
	height:28px;
	border:none;
	outline:none;
	overflow:hidden;
	position:relative;
	border-radius:14px;
	display:inline-block;
	background:var(--gray-40);
}
body.dark .toggle-switch {
	width:48px;
}
.toggle-switch,
.toggle-switch:before,
.toggle-switch:after {
	transition:all var(--animate) ease-in-out;
}
body.dark .toggle-switch {
	background:var(--gray-70);
}
.toggle-switch:before {
	top:4px;
	left:24px;
	content:"";
	width:20px;
	height:20px;
	display:block;
	position:absolute;
	border-radius:12px;
	background:var(--gray-70);
}
body.dark .toggle-switch:before {
	left:4px;
	background:var(--gray-35);
}
.toggle-switch:after {
	content:"";
	top:14px;
	right:2px;
	width:1px;
	height:1px;
	display:block;
	position:absolute;
	border-radius:0.5px;
	background:var(--gray-40);
}
body.dark .toggle-switch:after {
	top:-2px;
	right:2px;
	width:32px;
	height:32px;
	border-radius:16px;
	background:var(--gray-70);
}
.toggle-switch:hover,
.toggle-switch:hover:after {
	background:var(--gray-35);
}
.toggle-switch:focus,
.toggle-switch:active,
.toggle-switch:active:after {
	background:var(--gray-25);
}
body.dark .toggle-switch:hover,
body.dark .toggle-switch:hover:after {
	background:var(--gray-95);
}
body.dark .toggle-switch:focus,
body.dark .toggle-switch:active,
body.dark .toggle-switch:active:after {
	background:var(--gray-65);
}
.noselect {
	user-select:none;
	-ms-user-select:none;
	-moz-user-select:none;
	-webkit-user-select:none;
}
.disableEasingTemporarily,
.disableEasingTemporarily:after,
.disableEasingTemporarily:before {
  -o-transition-property:none!important;
  -moz-transition-property:none!important;
  -ms-transition-property:none!important;
  -webkit-transition-property:none!important;
  transition-property:none!important;
  -o-transform:none!important;
  -moz-transform:none!important;
  -ms-transform:none!important;
  -webkit-transform:none!important;
  transform:none!important;
  -webkit-animation:none!important;
  -moz-animation:none!important;
  -o-animation:none!important;
  -ms-animation:none!important;
  animation:none!important;
}
              
            
!

JS

              
                // Full demo here: https://timsilva.com/dm/

var darkmodeactive = localStorage.getItem("darkmode");
console.log("Dark mode is: " + darkmodeactive);
function labelDark() {
  $(".toggle-switch").attr("alt", "Go light");
  $(".toggle-switch").attr("title", "Go light");
}
function goDark() {
  console.log("Dark mode is active");
  labelDark();
  $("body").addClass("dark");
  refreshFavicon();
}
function stayDark() {
  goDark();
  localStorage.setItem("darkmode", true);
  darkmodeactive = localStorage.getItem("darkmode");
  console.log("Dark mode is: " + darkmodeactive + " and it will stay dark");
}
function labelLight() {
  $(".toggle-switch").attr("alt", "Go dark");
  $(".toggle-switch").attr("title", "Go dark");
}
function goLight() {
  console.log("Light mode is active");
  labelLight();
  $("body").removeClass("dark");
  refreshFavicon();
}
function stayLight() {
  goLight();
  localStorage.setItem("darkmode", false);
  darkmodeactive = localStorage.getItem("darkmode");
  console.log("Dark mode is: " + darkmodeactive + " and it will stay light");
}
window.matchMedia("(prefers-color-scheme: dark)").addListener(e => e.matches && stayDark());
window.matchMedia("(prefers-color-scheme: light)").addListener(e => e.matches && stayLight());
$(".toggle-switch").click(function() {
  if ($("body").hasClass("dark")) {
    stayLight();
  } else {
    stayDark();
  }
});
$(".label-light").click(function() {
  if ($("body").hasClass("dark")) {
    stayLight();
  }
});
$(".label-dark").click(function() {
  if (!$("body").hasClass("dark")) {
    stayDark();
  }
});
window.onload=function() {
  if (localStorage.darkmode=="true") {
    console.log("User manually selected dark mode from a past session");
    goDark();
  } else if (localStorage.darkmode=="false") {
    console.log("User manually selected light mode from a past session");
    goLight();
  } else {
    console.log("User hasn't selected dark or light mode from a past session, dark mode has been served by default and OS-level changes will automatically reflect");
    if ($("body").hasClass("dark")) {
      labelDark();
    } else {
      labelLight();
    }
  }
};
function tempDisableAnim() {
  $("*").addClass("disableEasingTemporarily");
  setTimeout(function() {
    $("*").removeClass("disableEasingTemporarily");
  }, 20);
}
setTimeout(function() {
  $(".load-flash").css("display","none");
  $(".load-flash").css("visibility","hidden");
  tempDisableAnim();
}, 20);
$(window).resize(function() {
  tempDisableAnim();
  setTimeout(function() {
    tempDisableAnim();
  }, 0);
});
function refreshFavicon() {
  if (matchMedia('(prefers-color-scheme: dark)').matches) {
    var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'icon';
    link.href = 'favicon-dark.svg';
    document.getElementsByTagName('head')[0].appendChild(link);
  } else {
    var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'icon';
    link.href = 'favicon.svg';
    document.getElementsByTagName('head')[0].appendChild(link);
  }
}
              
            
!
999px

Console