Open In App

How To Create Toggle Switch by using HTML and CSS?

Last Updated : 19 Nov, 2024
Comments
Improve
Suggest changes
6 Likes
Like
Report

A toggle switch allows users to switch between two states, typically “on” and “off,” and is commonly used in forms and interactive elements. To create a toggle switch, we will use HTML and CSS. We can add different styles to the checkbox element to create a toggle switch.

1. Basic Circular Toggle Switch

The simplest way to create a toggle switch is to use an HTML checkbox input and style it with CSS to look like a switch.

HTML
<h2>Basic Toggle Switch</h2>

<label class="toggle-switch">
    <input type="checkbox" />
    <span class="slider"></span>
</label>
CSS

Output: Using transform and translateX, we can shift the circular slider when the checkbox is checked.

basic-toggle-switch

Create Toggle Switch by using HTML and CSS

2. Square Toggle Switch

For a modern look, let’s create a square toggle switch by adjusting the border-radius properties.

HTML
<h3>Square Toggle Switch</h3>

<label class="toggle-switch square">
    <input type="checkbox">
    <span class="slider"></span>
</label> 
CSS

Explanation

  • .square .slider: Removes full rounding from the switch track and gives a slight rounding of 4px for a softer square.
  • .square .slider:before: Makes the toggle button a square with border-radius: 0

Output

square-toggle-switch

Create Toggle Switch by using HTML and CSS

HTML and CSS both are foundation of webpages. HTML is used for webpage development by structuring websites, web apps and CSS used for styling websites and webapps. You can learn more about HTML and CSS from the links given below:



Next Article

Similar Reads