A mirror of my website's source code.

By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 creatingaswitch.md

View raw Download
text/html • 3.74 kiB
HTML document, ASCII text, with very long lines (313)

title: Creating a Switch in HTML and CSS date: 2022 Feb 11 updated: 2023 Feb 2

A switch is something that is basically just a nicer looking checkbox. Here, I'll be showing you how to make one.

The first step is to create checkbox with any class; I'll be using switch, because it's a switch.

<style>
   input[type=checkbox].switch {}
</style>
<input type="checkbox" class="switch" />

Next step is to add an appearance of none to the CSS--make sure to add -moz- and -webkit-. After that, you're going to want to set it so that the checkbox a rectangle--make sure it's in px. We also want to set the position to relative

<style>
   input[type=checkbox].switch {
       -webkit-appearance: none;
       -moz-appearance: none;
       appearance: none;
       position: relative;
   }
</style>
<input type="checkbox" class="switch" />

Next we want to create ::before pseudo. In there we want to make it have a position of relative, a width and height that are -1px less than the height of the main switch, a display of block, top and left set to 1px, a content of "", and a background color that's different from the one in the main switch.

<style>
   input[type=checkbox].switch {
       -webkit-appearance: none;
       -moz-appearance: none;
       appearance: none;
       position: relative;
       background: #000;
       width: 30px;
       height: 16px;
   }
   input[type=checkbox].switch::before {
       top: 1px;
       left: 1px;
       position: absolute;
       display: block;
       background: red;
       content: "";
       width: 14px;
       height: 14px;
   }
</style>
<input type="checkbox" class="switch" />

Finally add a pseudo called :checked which checks if a checkbox was checked, you'll want to change the before pseudo if the the checkbox is checked. You'll need to put it on the opposite side of where it started, for me, that's on the right-side.

<style>
   input[type=checkbox].switch {
       -webkit-appearance: none;
       -moz-appearance: none;
       appearance: none;
       position: relative;
       background: #000;
       width: 30px;
       height: 16px;
   }
   input[type=checkbox].switch::before {
       top: 1px;
       left: 1px;
       position: absolute;
       display: block;
       background: red;
       content: "";
       width: 14px;
       height: 14px;
   }
   input[type=checkbox].switch:checked::before {
       left: 15px; /* .switch width - .switch::before width - 1px */
   }
</style>
<input type="checkbox" class="switch" />

I'm using the left property instead of the right property as it makes it easier to transition between the 2 positions. Here is the CSS with the transition added in, plus a background change(also dark mode):

<style>
   :root {
       background: #000;
       color: #fff;
   }
   input[type=checkbox].switch {
       -webkit-appearance: none;
       -moz-appearance: none;
       appearance: none;
       position: relative;
       background: #fff;
       width: 30px;
       height: 16px;
   }
   input[type=checkbox].switch::before {
       top: 1px;
       left: 1px;
       position: absolute;
       display: block;
       background-color: #ff5c1c;
       content: "";
       width: 14px;
       height: 14px;
       transition: background-color .5s, left .5s;
   }
   input[type=checkbox].switch:checked::before {
       left: 15px; /* .switch width - .switch::before width - 1px */
       background-color: #3f9b00;
   }
</style>
<input type="checkbox" class="switch" />

All of this results in a final product that looks like:

#fff; border: none" src="/blog-files/switch-final.html"

See Code

                
                    
1
title: Creating a Switch in HTML and CSS
2
date: 2022 Feb 11
3
updated: 2023 Feb 2
4
5
A switch is something that is basically just a nicer looking checkbox. Here, I'll be showing you how to make one.
6
7
The first step is to create checkbox with any class; I'll be using `switch`, because it's a switch.
8
9
```html
10
<style>
11
input[type=checkbox].switch {}
12
</style>
13
<input type="checkbox" class="switch" />
14
```
15
16
Next step is to add an appearance of none to the CSS--make sure to add `-moz-` and `-webkit-`. After that, you're going to want to set it so that the checkbox a rectangle--make sure it's in `px`. We also want to set the position to relative
17
18
```html
19
<style>
20
input[type=checkbox].switch {
21
-webkit-appearance: none;
22
-moz-appearance: none;
23
appearance: none;
24
position: relative;
25
}
26
</style>
27
<input type="checkbox" class="switch" />
28
```
29
30
Next we want to create `::before` pseudo. In there we want to make it have a position of relative, a width and height that are `-1px` less than the height of the main switch, a display of block, top and left set to `1px`, a content of `""`, and a background color that's different from the one in the main switch.
31
32
```html
33
<style>
34
input[type=checkbox].switch {
35
-webkit-appearance: none;
36
-moz-appearance: none;
37
appearance: none;
38
position: relative;
39
background: #000;
40
width: 30px;
41
height: 16px;
42
}
43
input[type=checkbox].switch::before {
44
top: 1px;
45
left: 1px;
46
position: absolute;
47
display: block;
48
background: red;
49
content: "";
50
width: 14px;
51
height: 14px;
52
}
53
</style>
54
<input type="checkbox" class="switch" />
55
```
56
57
Finally add a pseudo called `:checked` which checks if a checkbox was checked, you'll want to change the before pseudo if the the checkbox is checked. You'll need to put it on the opposite side of where it started, for me, that's on the right-side.
58
59
```html
60
<style>
61
input[type=checkbox].switch {
62
-webkit-appearance: none;
63
-moz-appearance: none;
64
appearance: none;
65
position: relative;
66
background: #000;
67
width: 30px;
68
height: 16px;
69
}
70
input[type=checkbox].switch::before {
71
top: 1px;
72
left: 1px;
73
position: absolute;
74
display: block;
75
background: red;
76
content: "";
77
width: 14px;
78
height: 14px;
79
}
80
input[type=checkbox].switch:checked::before {
81
left: 15px; /* .switch width - .switch::before width - 1px */
82
}
83
</style>
84
<input type="checkbox" class="switch" />
85
```
86
87
I'm using the `left` property instead of the `right` property as it makes it easier to transition between the 2 positions. Here is the CSS with the transition added in, plus a background change(also dark mode):
88
89
```html
90
<style>
91
:root {
92
background: #000;
93
color: #fff;
94
}
95
input[type=checkbox].switch {
96
-webkit-appearance: none;
97
-moz-appearance: none;
98
appearance: none;
99
position: relative;
100
background: #fff;
101
width: 30px;
102
height: 16px;
103
}
104
input[type=checkbox].switch::before {
105
top: 1px;
106
left: 1px;
107
position: absolute;
108
display: block;
109
background-color: #ff5c1c;
110
content: "";
111
width: 14px;
112
height: 14px;
113
transition: background-color .5s, left .5s;
114
}
115
input[type=checkbox].switch:checked::before {
116
left: 15px; /* .switch width - .switch::before width - 1px */
117
background-color: #3f9b00;
118
}
119
</style>
120
<input type="checkbox" class="switch" />
121
```
122
123
All of this results in a final product that looks like:
124
125
<iframe style="background: #fff; border: none" src="/blog-files/switch-final.html"></iframe>
126
127
[See Code](/blog-files/switch-final.txt)
128
129