creatingaswitch.md
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:
1title: Creating a Switch in HTML and CSS 2date: 2022 Feb 11 3updated: 2023 Feb 2 4 5A switch is something that is basically just a nicer looking checkbox. Here, I'll be showing you how to make one. 6 7The first step is to create checkbox with any class; I'll be using `switch`, because it's a switch. 8 9```html 10<style> 11input[type=checkbox].switch {} 12</style> 13<input type="checkbox" class="switch" /> 14``` 15 16Next 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> 20input[type=checkbox].switch { 21-webkit-appearance: none; 22-moz-appearance: none; 23appearance: none; 24position: relative; 25} 26</style> 27<input type="checkbox" class="switch" /> 28``` 29 30Next 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> 34input[type=checkbox].switch { 35-webkit-appearance: none; 36-moz-appearance: none; 37appearance: none; 38position: relative; 39background: #000; 40width: 30px; 41height: 16px; 42} 43input[type=checkbox].switch::before { 44top: 1px; 45left: 1px; 46position: absolute; 47display: block; 48background: red; 49content: ""; 50width: 14px; 51height: 14px; 52} 53</style> 54<input type="checkbox" class="switch" /> 55``` 56 57Finally 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> 61input[type=checkbox].switch { 62-webkit-appearance: none; 63-moz-appearance: none; 64appearance: none; 65position: relative; 66background: #000; 67width: 30px; 68height: 16px; 69} 70input[type=checkbox].switch::before { 71top: 1px; 72left: 1px; 73position: absolute; 74display: block; 75background: red; 76content: ""; 77width: 14px; 78height: 14px; 79} 80input[type=checkbox].switch:checked::before { 81left: 15px; /* .switch width - .switch::before width - 1px */ 82} 83</style> 84<input type="checkbox" class="switch" /> 85``` 86 87I'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 { 92background: #000; 93color: #fff; 94} 95input[type=checkbox].switch { 96-webkit-appearance: none; 97-moz-appearance: none; 98appearance: none; 99position: relative; 100background: #fff; 101width: 30px; 102height: 16px; 103} 104input[type=checkbox].switch::before { 105top: 1px; 106left: 1px; 107position: absolute; 108display: block; 109background-color: #ff5c1c; 110content: ""; 111width: 14px; 112height: 14px; 113transition: background-color .5s, left .5s; 114} 115input[type=checkbox].switch:checked::before { 116left: 15px; /* .switch width - .switch::before width - 1px */ 117background-color: #3f9b00; 118} 119</style> 120<input type="checkbox" class="switch" /> 121``` 122 123All 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