Creating a Day/Night Toggle Button Using HTML/CSS

picture[1]-Creating a Day/Night Toggle Button Using HTML/CSS-PANDA

In numerous applications and websites, the day/night mode switch has become a pivotal means of enhancing user experience. Through a day/night toggle button, users can effortlessly switch between daytime and nighttime modes according to their preferences and environment. Using HTML and CSS, we can implement a simple yet creative day/night toggle button, providing users with a convenient way to operate this feature.

Here is the specific code:

HTML

<button class="theme__icon">
  <span></span>
  <span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
  </span>
  <span></span>
</button>

CSS

/* <reset-style> ============================ */
button {
  border: none;
  background: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
  font-family: inherit;
}

/* <main-style> ============================ */
.theme__icon {
  width: 32px;
  height: 32px;
  padding: 4px;
  border-radius: 10px;
  background-color: #5856d6;
  position: relative;
  overflow: hidden;
}

.theme__icon > :nth-child(1) {
  width: 16px;
  height: 16px;
  border-radius: 24px;
  border: 3px solid #5856d6;
  background-color: rgb(28, 28, 30);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: width .4s, height .4s, border .4s, background-color .4s;
  z-index: 10;
}

.theme__icon:hover > :nth-child(1) {
  width: 20px;
  height: 20px;
  border: 1px solid rgb(245, 245, 247);
  background-color: rgb(245, 245, 247);
}

.theme__icon > :nth-child(3) {
  width: 4px;
  height: 4px;
  border-radius: 9px;
  background-color: #5856d6;
  position: absolute;
  top: 2px;
  right: 2px;
  transition: width .6s, height .6s;
  transition-delay: .08s;
  z-index: 10;
}

.theme__icon:hover > :nth-child(3) {
  width: 18px;
  height: 18px;
}

.theme__icon > :nth-child(2) {
  width: 24px;
  height: 24px;
  border-radius: 24px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: transform .4s;
}

.theme__icon:hover > :nth-child(2) {
  transform: translate(-50%, -50%) rotate(-45deg) scale(.8);
}

.theme__icon > :nth-child(2) span {
  display: block;
  width: 24px;
  height: 2px;
  border-radius: 2px;
  background-color: rgb(28, 28, 30);
  position: relative;
}

.theme__icon > :nth-child(2) > :nth-child(1) {
  top: calc(50% - 1px);
}

.theme__icon > :nth-child(2) > :nth-child(2) {
  top: calc(50% - 3px);
  transform: rotate(90deg);
}

.theme__icon > :nth-child(2) > :nth-child(3) {
  top: calc(50% - 5px);
  transform: rotate(45deg);
}

.theme__icon > :nth-child(2) > :nth-child(4) {
  top: calc(50% - 7px);
  transform: rotate(-45deg);
}

HTML & CSS

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    /* <reset-style> ============================ */
    button {
      border: none;
      background: none;
      padding: 0;
      margin: 0;
      cursor: pointer;
      font-family: inherit;
    }

    /* <main-style> ============================ */
    .theme__icon {
      width: 32px;
      height: 32px;
      padding: 4px;
      border-radius: 10px;
      background-color: #5856d6;
      position: relative;
      overflow: hidden;
    }

    .theme__icon> :nth-child(1) {
      width: 16px;
      height: 16px;
      border-radius: 24px;
      border: 3px solid #5856d6;
      background-color: rgb(28, 28, 30);
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      transition: width .4s, height .4s, border .4s, background-color .4s;
      z-index: 10;
    }

    .theme__icon:hover> :nth-child(1) {
      width: 20px;
      height: 20px;
      border: 1px solid rgb(245, 245, 247);
      background-color: rgb(245, 245, 247);
    }

    .theme__icon> :nth-child(3) {
      width: 4px;
      height: 4px;
      border-radius: 9px;
      background-color: #5856d6;
      position: absolute;
      top: 2px;
      right: 2px;
      transition: width .6s, height .6s;
      transition-delay: .08s;
      z-index: 10;
    }

    .theme__icon:hover> :nth-child(3) {
      width: 18px;
      height: 18px;
    }

    .theme__icon> :nth-child(2) {
      width: 24px;
      height: 24px;
      border-radius: 24px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      transition: transform .4s;
    }

    .theme__icon:hover> :nth-child(2) {
      transform: translate(-50%, -50%) rotate(-45deg) scale(.8);
    }

    .theme__icon> :nth-child(2) span {
      display: block;
      width: 24px;
      height: 2px;
      border-radius: 2px;
      background-color: rgb(28, 28, 30);
      position: relative;
    }

    .theme__icon> :nth-child(2)> :nth-child(1) {
      top: calc(50% - 1px);
    }

    .theme__icon> :nth-child(2)> :nth-child(2) {
      top: calc(50% - 3px);
      transform: rotate(90deg);
    }

    .theme__icon> :nth-child(2)> :nth-child(3) {
      top: calc(50% - 5px);
      transform: rotate(45deg);
    }

    .theme__icon> :nth-child(2)> :nth-child(4) {
      top: calc(50% - 7px);
      transform: rotate(-45deg);
    }
  </style>
</head>

<body>

  <button class="theme__icon">
    <span></span>
    <span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
    </span>
    <span></span>
  </button>

</body>

</html>
THE END
Like12 Share
comment
 avatar
Your valuable insights are welcome!
Submit
 avatar

Nickname

cancel
NicknameSmiliesCodeImage

    No comments