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