Simple Day Night Toggle Button Using HTML and CSS

Day-Night Toggle Button is a common web element that is often found on websites. This Day Night Toggle Switch in the navigation bar helps to choose the dark and light theme of the website.

Currently, the Day And Night Toggle Switch can be seen on almost all major websites starting from Facebook and Instagram.

CSS Day Night Toggle Button




This article will help you to know step by step how to create Day Night Toggle Button using CSS. This type of CSS Toggle Button is used to create HTML checkboxes. Here's how to easily create a Toggle Switch using the checkboxes.

CSS Day Night Toggle Button

Although there are many such tutorials on the Internet. But here I will tell you how to make Day Night Toggle Button and how to implement it in your project.

A check box was first created using HTML input to create this button. Then that checkbox is shaped like a button by CSS.

To make CSS Day Night Toggle Switch you must have some idea about HTML and CSS. This is not the first time I have shared another design.

 

Step 1: 

HTML of Day Night Toggle Switch

I have added all the information on this Toggle Button using the following HTML. First I created a check box using input which was later turned into a button.


<label>
  <input type="checkbox" name="">
  <span class="check"></span>
</label>


Step 2: 

Design the checkbox

The webpage is designed with the following CSS.


body {
  margin: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}


Design the checkbox

Here I used display: none to hide the checkbox. Because under normal circumstances we don't want to see the checkbox. The checkbox will only be used to activate or select this button.


input[type="checkbox"] {
  -webkit-appearance: none;
  visibility: hidden;
  display: none;
}


Step 3: 

The basic structure of Day Night Toggle Button

Now it's time to create this Day Night Toggle button. The basic area has been created here first. Border-radius: 20px has been used to make the area width: 40px, height: 20px, slightly rounded.


.check {
  display: block;
  height: 20px;
  width: 40px;
  background: #0a0a0a;
  border-radius: 20px;
  cursor: pointer;
  transition: ease-in 0.3s;
  overflow: hidden;
  position: relative;
  transform: scale(5);
}


structure of Day Night Toggle Button

Here's what happens when you click on this Toggle Button. When you click on the button, the background of the button will change from black to white and the background of the webpage will be black. Box-shadow is used here to darken the background of the webpage.


input[type="checkbox"]:checked ~ .check {
  background: white;
  box-shadow: 0 0 0 12000px #0a0a0a;
}


when you click on this Toggle Button


Step 4: 

Create a round ball in Toggle Button

Now you need to create a small round ball in this day-night CSS toggle switch using CSS's ": before". This round button is not fixed. 

When you click on this button, this small circular button will move from left to right and from right to left. The height: 16px, width: 16px of this small round button have been used.


.check:before {
  content: "";
  display: block;
  position: absolute;
  height: 16px;
  width: 16px;
  background: #878686;
  border-radius: 50%;
  top: 2px;
  left: 2px;
  transition: 0.5s;
}


round ball in Toggle Button

This small round button will change its position when you click on the check box. Transform is used to change position. The background of the button will be blue.


input[type="checkbox"]:checked ~ .check:before {
  background: #01f4fd;
  position: absolute;
  transform: translateX(20px);
}


Simple Day Night Toggle Button

Hope you know how to make this Day Night Toggle Button. I have shared many more CSS toggle switch designs before.






Comments

Popular posts from this blog

Splash Image Mask Using HTML and CSS (Free Code)

Simple RGB Color Generator Using JavaScript & CSS