Back

Building a Stylish Toggle Switch with Smooth Transitions

A detailed tutorial on crafting an elegant toggle switch complete with fluid animations and customizable options.

HTML

To get started, we need a simple HTML structure comprising a checkbox and a label. The input element will use a hidden checkbox, styled to appear as a toggle switch when clicked.
    <div class="toggle-container">
      <input type="checkbox" id="toggle" class="toggle">
      <label for="toggle" class="toggle-label">
        <span class="toggle-button"></span>
      </label>
    </div>
  

CSS

Our next step is to style the toggle switch using CSS to achieve a slick, modern look. The .toggle class is set to display: none to hide the original checkbox. We use a label to mimic the switch's appearance, setting a light border-radius and transition effect to make the toggle button move smoothly.
    .toggle-container {
      display: flex;
      align-items: center;
      padding: 10px;
    }
    .toggle {
      display: none;
    }
    .toggle-label {
      position: relative;
      width: 60px;
      height: 30px;
      background-color: #aaa;
      border-radius: 50px;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }
    .toggle-button {
      position: absolute;
      top: 2px;
      left: 2px;
      width: 26px;
      height: 26px;
      background-color: #fff;
      border-radius: 50%;
      transition: left 0.3s ease;
    }
    .toggle:checked + .toggle-label .toggle-button {
      left: 32px;
    }
  

JavaScript

Although the CSS handles most of the animations, we'll employ a bit of JavaScript to change the toggle switch's background color dynamically. This allows for more customization and visual feedback when the state changes.
    document.getElementById('toggle').addEventListener('change', function(event) {
      const toggle = event.target;
      const toggleLabel = document.querySelector('.toggle-label');
      if (toggle.checked) {
        toggleLabel.style.backgroundColor = '#32ffcc';
      } else {
        toggleLabel.style.backgroundColor = '#aaa';
      }
    });
  

Advanced Customizations

To enhance the toggle switch's appeal, consider adding more diverse animations or even integrating it with other UI frameworks. This can greatly enhance accessibility by adding ARIA roles and states to reflect its functionality better for screen readers. For theming, modify CSS variables to allow easy color scheme changes. Additionally, ensuring the toggle is mobile-responsive would make it adaptable to different screen sizes, enriching user experience across devices.

Whole code

<div class="toggle-container">
  <input type="checkbox" id="toggle" class="toggle">
  <label for="toggle" class="toggle-label">
    <span class="toggle-button"></span>
  </label>
</div>


<script>
document.getElementById('toggle').addEventListener('change', function(event) {
  const toggle = event.target;
  const toggleLabel = document.querySelector('.toggle-label');
  if (toggle.checked) {
    toggleLabel.style.backgroundColor = '#32ffcc';
  } else {
    toggleLabel.style.backgroundColor = '#aaa';
  }
});
</script>

<style>
.toggle-container {
  display: flex;
  align-items: center;
  padding: 10px;
}
.toggle {
  display: none;
}
.toggle-label {
  position: relative;
  width: 60px;
  height: 30px;
  background-color: #aaa;
  border-radius: 50px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}
.toggle-button {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 26px;
  height: 26px;
  background-color: #fff;
  border-radius: 50%;
  transition: left 0.3s ease;
}
.toggle:checked + .toggle-label .toggle-button {
  left: 32px;
}
</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More inputs

Similar

See also