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

              
                <h2>Weekly Coding Challenge #12: Toggle</h2>
<div class="text-container">
	<h3>Dark / Light theme Toggle</h3>
	<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Sit illum velit temporibus doloribus explicabo, eius vero laboriosam asperiores accusantium hic unde voluptatem, veritatis rerum atque debitis voluptatibus aspernatur mollitia natus!</p>
	<p>
		<strong>
			<i class="fas fa-hand-point-down"></i> Toggle me <i class="fas fa-hand-point-down"></i>
		</strong>
	</p>
</div>

<div class="toggle">
	<input type="checkbox" id="toggle" />
	<label for="toggle"></label>
</div>

<footer>
	<p>
		Created with <i class="fa fa-heart"></i> by
		<a target="_blank" href="https://florin-pop.com">Florin Pop</a>
		- Read how I created this and how you can join the challenge
		<a target="_blank" href="https://www.florin-pop.com/blog/2019/05/dark-light-theme-toggle/">here</a>
	</p>
</footer>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Lato');

* {
	box-sizing: border-box;
}

body {
	background-color: #f5f7fa;
	
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	
	transition: all ease-in-out 0.3s;

	min-height: 100vh;
	font-family: 'Lato', sans-serif;
	margin: 0;
}

body.dark-theme {
	background-color: #333;
}

.text-container {
	max-width: 600px;
	text-align: center;
}

h2 {
	margin-bottom: 0;
}

h3 {
	color: #0054B0;
}

p {
	letter-spacing: 1px;
}

body.dark-theme h2, body.dark-theme p {
	color: #ddd;
}

.toggle input[type=checkbox] {
	display: none;
}

.toggle label {
	background-color: #777;
	border: 2px solid #555;
	border-radius: 50px;
	cursor: pointer;
	display: inline-block;
	position: relative;
	transition: all ease-in-out 0.3s;
	width: 100px;
	height: 50px;
}

.toggle label::after {
	background-color: #555;
	border-radius: 50%;
	content: ' ';
	cursor: pointer;
	display: inline-block;
	position: absolute;
	left: 2px;
	top: 2px;
	transition: all ease-in-out 0.3s;
	width: 42px;
	height: 42px;
}

.toggle input[type=checkbox]:checked ~ label {
	background-color: #00a0fc;
	border-color: #006dc9;
}

.toggle input[type=checkbox]:checked ~ label::after {
	background-color: #0054B0;
	transform: translateX(50px);
}

footer {
    background-color: #222;
    color: #fff;
    font-size: 14px;
    bottom: 0;
    position: fixed;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 999;
}

footer p {
    margin: 10px 0;
}

footer i {
    color: red;
}

footer a {
    color: #3c97bf;
    text-decoration: none;
}
              
            
!

JS

              
                const toggle = document.getElementById('toggle');
const body = document.body;

toggle.addEventListener('input', (e) => {
	const isChecked = e.target.checked;
	
	if(isChecked) {
		body.classList.add('dark-theme');
	} else {
		body.classList.remove('dark-theme');
	}
});
              
            
!
999px

Console