Customizable Rounded & Square Toggle Switch Web Component

Category: Form , Javascript | August 7, 2025
Authorlullaby6
Last UpdateAugust 7, 2025
LicenseMIT
Views552 views
Customizable Rounded & Square Toggle Switch Web Component

The Toggle Switch Web Component creates rounded iOS-style and square on/off switches with the <toggle-switch> custom element.

Each switch stores its state in a hidden input as true when on and false when off. The name attribute is copied to that hidden input, while custom attributes control dimensions, padding, corner shape, transition duration, and colors.

How to use it:

1. Load the toggle-switch.js file in your HTML document.

<script src="toggle-switch.js"></script>

2. Insert the <toggle-switch> custom element.

<toggle-switch></toggle-switch>

3. Set name when the hidden input needs a field name.

<toggle-switch
  id="example"
  class="my-switch"
  name="switch">
</toggle-switch>

Rounded and Square Toggle Switches:

Set rounded="true" for rounded corners or rounded="false" for a square switch.

<!-- Rounded switch -->
<toggle-switch rounded="true"></toggle-switch>
<!-- Square switch -->
<toggle-switch rounded="false"></toggle-switch>

Customization Attributes:

AttributeDescription
nameSets the name copied to the hidden input that stores the switch state.
sizeSets the size of the toggle switch.
heightSets the switch height.
widthSets the switch width.
paddingSets the internal padding.
roundedUses rounded corners when set to true and square corners when set to false.
transition-durationSets the toggle transition duration in milliseconds.
unchecked-colorSets the color for the unchecked state.
checked-colorSets the color for the checked state.
dot-colorSets the switch dot color.

Full Customization Example:

<toggle-switch
  size="30"
  height="60"
  width="30"
  padding="15"
  rounded="true"
  transition-duration="200"
  unchecked-color="#ccc"
  checked-color="#2196F3"
  dot-color="#fff"
></toggle-switch>

Alternatives & Related Resources:

  • slider.css – CSS toggle controls based on radio buttons, with two-state and three-state configurations.
  • Toggle Switchy – Pure CSS checkbox switches with rounded and square styles, sizes, colors, and labels.
  • simple-switch – JavaScript toggle switches built from regular checkbox inputs with iOS and Material Design styles.
  • 10 Best Toggle Switch JavaScript And CSS Libraries – A collection of JavaScript and CSS switch implementations.

Changelog:

08/07/2025

  • v1.0.0

You Might Be Interested In:


Leave a Reply