Hey Programmers! In this blog post, we will make an Awesome Night & Day Mode Toggle Switch With Pure HTML, and CSS without using javascript. This is the most included feature by the web developers on websites.
This project allows you to create a minimal Night & Day Mode Toggle Switch so that users can seamlessly switch between light & dark themes.
Also Read: Light Bulb Switch With HTML & CSS
I will explain important CSS & HTML code step by step with code boxes so that you can create such projects on your own. In the end, I will share the entire source code to run this awesome project on your local device.
The HTML sets up the structure for the toggle switch. The wrapper
the class centers the switch on the page. Inside the wrapper, there is a toggle
element that contains an input checkbox, a background div, and the switch elements.
<div class="wrapper">
<div class="toggle">
<input class="toggle-input" type="checkbox" />
<div class="toggle-bg"></div>
<div class="toggle-switch">
<div class="toggle-switch-figure"></div>
<div class="toggle-switch-figureAlt"></div>
</div>
</div>
</div>
<div class="wrapper">
: This div wraps the entire switch, positioning it on the page.<div class="toggle">
: The main container for the toggle switch.<input class="toggle-input" type="checkbox" />
: The actual checkbox input that will be styled and hidden.<div class="toggle-bg"></div>
: Acts as the background of the switch, changing color when toggled.<div class="toggle-switch">
: Contains the switch that moves left and right.
<div class="toggle-switch-figure"></div>
: A decorative figure, part of the switch design.<div class="toggle-switch-figureAlt"></div>
: Another decorative element that changes when the switch is toggled.Also Read: Simple Minimal Toggle Switch With HTML & CSS
The CSS is responsible for the switch’s visual appearance and animation effects. It uses modern CSS features like variables, pseudo-elements, and transitions.
These styles set up a neutral background and center the toggle on the page.
body {
background-color: #F3F3F3;
}
.wrapper {
padding-top: 40px;
text-align: center;
}
body
: A light gray background is set for the entire page.wrapper
: Adds padding to position the toggle vertically and centers it horizontally.The .toggle
class defines the basic dimensions and position of the switch. It includes padding and a border-radius to make the switch appear rounded.
.toggle {
position: relative;
display: inline-block;
width: 100px;
margin-left: 100px;
padding: 4px;
border-radius: 40px;
}
position: relative
: This allows for absolutely positioned child elements within the toggle.display: inline-block
: Makes the element fit to its content and allows for margin and padding to be applied.border-radius: 40px
: Rounds the corners, giving the switch a pill shape.The :before
and :after
pseudo-elements clear floats, ensuring proper layout of the inner elements.
.toggle:before,
.toggle:after {
content: "";
display: table;
}
.toggle:after {
clear: both;
}
content: ""
: Adds an empty content that helps in layout purposes.display: table
: Ensures the pseudo-elements participate in the layout as table elements.clear: both
: Ensures no floating elements affect the layout of the toggle switch.The background of the toggle is styled with a light blue color, rounded edges, and a border. The transition property makes it smoothly animate when the state changes.
.toggle-bg {
position: absolute;
top: -4px;
left: -4px;
width: 100%;
height: 100%;
background-color: #C0E6F6;
border-radius: 40px;
border: 4px solid #81C0D5;
transition: all 0.1s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
position: absolute
: Allows for precise positioning relative to the parent.background-color: #C0E6F6
: Light blue background for the toggle.border-radius: 40px
: Rounds the edges to match the container.transition
: Smoothly animates changes.The checkbox input is hidden from view but still accessible. It covers the entire toggle area and is positioned on top of other elements with a z-index
of 2.
.toggle-input {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2;
opacity: 0;
}
opacity: 0
: Makes the checkbox invisible while keeping it interactive.z-index: 2
: Places the checkbox on top of other elements.The switch that moves is styled as a yellow circle with a border. It also has transitions for smooth movement.
.toggle-switch {
position: relative;
width: 40px;
height: 40px;
margin-left: 50px;
background-color: #F5EB42;
border: 4px solid #E4C74D;
border-radius: 50%;
transition: all 0.1s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
border-radius: 50%
: Creates a circular shape.transition
: Smoothly animates changes in position and appearance.The .toggle-switch-figure
and .toggle-switch-figureAlt
elements are decorative, adding visual flair to the switch. They use pseudo-elements to create complex shapes and animations.
.toggle-switch-figure {
position: absolute;
bottom: -14px;
left: -50px;
width: 80px;
height: 30px;
border: 8px solid #D4D4D2;
border-radius: 20px;
background-color: #fff;
transform: scale(0.4);
transition: all 0.12s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.toggle-switch-figure:after,
.toggle-switch-figure:before {
content: "";
display: block;
position: absolute;
background-color: #fff;
border: 8px solid #D4D4D2;
border-radius: 100%;
background-color: #fff;
border-right-color: transparent;
border-bottom-color: transparent;
}
border-radius: 20px
: Gives a rounded rectangular shape.transform: scale(0.4)
: Scales the element down.border-right-color: transparent
: Makes part of the border transparent to create a cut-out effect..toggle-switch-figureAlt {
position: absolute;
top: 5px;
left: 2px;
width: 2px;
height: 2px;
background-color: #EFEEDA;
border-radius: 100%;
border: 4px solid #DEE1C5;
box-shadow: 42px -7px 0 -3px #FCFCFC, 75px -10px 0 -3px #FCFCFC, 54px 4px 0 -4px #FCFCFC, 83px 7px 0 -2px #FCFCFC, 63px 18px 0 -4px #FCFCFC, 44px 28px 0 -2px #FCFCFC, 78px 23px 0 -3px #FCFCFC;
transition: all 0.12s cubic-bezier(0.25, 0.46, 0.45, 0.94);
transform: scale(0);
}
box-shadow
: Creates multiple shadows for a complex visual effect.transform: scale(0)
: Initially hides the element.When the checkbox is checked, these styles apply, moving the switch and changing the background and border colors.
.toggle-input:checked ~ .toggle-switch {
margin-left: 0;
border-color: #DEE1C5;
background-color: #FFFDF2;
}
.toggle-input:checked ~ .toggle-bg {
background-color: #484848;
border-color: #202020;
}
.toggle-input:checked ~ .toggle-switch .toggle-switch-figure {
margin-left: 40px;
opacity: 0;
transform: scale(0.1);
}
.toggle-input:checked ~ .toggle-switch .toggle-switch-figureAlt {
transform: scale(1);
}
.toggle-input:checked ~ .toggle-switch
: Moves the switch to the left and changes its color..toggle-input:checked ~ .toggle-switch .toggle-switch-figure
: Hides the main figure..toggle-input:checked ~ .toggle-switch .toggle-switch-figureAlt
: Shows the alternative figure.HTML:
<div class="wrapper">
<div class="toggle">
<input class="toggle-input" type="checkbox" />
<div class="toggle-bg"></div>
<div class="toggle-switch">
<div class="toggle-switch-figure"></div>
<div class="toggle-switch-figureAlt"></div>
</div>
</div>
</div>
CSS:
<style>
body {
background-color: #F3F3F3;
}
.wrapper {
padding-top: 40px;
text-align: center;
}
.toggle {
position: relative;
display: inline-block;
width: 100px;
margin-left: 100px;
padding: 4px;
border-radius: 40px;
}
.toggle:before,
.toggle:after {
content: "";
display: table;
}
.toggle:after {
clear: both;
}
.toggle-bg {
position: absolute;
top: -4px;
left: -4px;
width: 100%;
height: 100%;
background-color: #C0E6F6;
border-radius: 40px;
border: 4px solid #81C0D5;
transition: all 0.1s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.toggle-input {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 1px solid red;
border-radius: 40px;
z-index: 2;
opacity: 0;
}
.toggle-switch {
position: relative;
width: 40px;
height: 40px;
margin-left: 50px;
background-color: #F5EB42;
border: 4px solid #E4C74D;
border-radius: 50%;
transition: all 0.1s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.toggle-switch-figure {
position: absolute;
bottom: -14px;
left: -50px;
display: block;
width: 80px;
height: 30px;
border: 8px solid #D4D4D2;
border-radius: 20px;
background-color: #fff;
transform: scale(0.4);
transition: all 0.12s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.toggle-switch-figure:after {
content: "";
display: block;
position: relative;
top: -65px;
right: -42px;
width: 15px;
height: 15px;
border: 8px solid #D4D4D2;
border-radius: 100%;
border-right-color: transparent;
border-bottom-color: transparent;
transform: rotateZ(70deg);
background-color: #fff;
}
.toggle-switch-figure:before {
content: "";
display: block;
position: relative;
top: -25px;
right: -10px;
width: 30px;
height: 30px;
border: 8px solid #D4D4D2;
border-radius: 100%;
border-right-color: transparent;
border-bottom-color: transparent;
transform: rotateZ(30deg);
background-color: #fff;
}
.toggle-switch-figureAlt {
content: "";
position: absolute;
top: 5px;
left: 2px;
width: 2px;
height: 2px;
background-color: #EFEEDA;
border-radius: 100%;
border: 4px solid #DEE1C5;
box-shadow: 42px -7px 0 -3px #FCFCFC, 75px -10px 0 -3px #FCFCFC, 54px 4px 0 -4px #FCFCFC, 83px 7px 0 -2px #FCFCFC, 63px 18px 0 -4px #FCFCFC, 44px 28px 0 -2px #FCFCFC, 78px 23px 0 -3px #FCFCFC;
transition: all 0.12s cubic-bezier(0.25, 0.46, 0.45, 0.94);
transform: scale(0);
}
.toggle-switch-figureAlt:before {
content: "";
position: absolute;
top: -6px;
left: 18px;
width: 7px;
height: 7px;
background-color: #EFEEDA;
border-radius: 100%;
border: 4px solid #DEE1C5;
}
.toggle-switch-figureAlt:after {
content: "";
position: absolute;
top: 19px;
left: 15px;
width: 2px;
height: 2px;
background-color: #EFEEDA;
border-radius: 100%;
border: 4px solid #DEE1C5;
}
.toggle-input:checked ~ .toggle-switch {
margin-left: 0;
border-color: #DEE1C5;
background-color: #FFFDF2;
}
.toggle-input:checked ~ .toggle-bg {
background-color: #484848;
border-color: #202020;
}
.toggle-input:checked ~ .toggle-switch .toggle-switch-figure {
margin-left: 40px;
opacity: 0;
transform: scale(0.1);
}
.toggle-input:checked ~ .toggle-switch .toggle-switch-figureAlt {
transform: scale(1);
}
</style>
Last Updated: June 18, 2024