creatingaswitch.md
HTML document, ASCII text, with very long lines (338)
title: Creating a Switch in HTML and CSS date: 2022 Feb 11 updated: 2023 Feb 1 --- A switch is something that is basically just a nicer checkbox. Here, I'll be showing you how to make one.
The first step is to create checkbox with any classname; I'll be using 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 abit less than the height of the main switch, a display of inline-block, a top and left of 0, a content of anything, color of transparent, 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: ""; color: transparent; width: 14px; height: 14px; } </style> <input type="checkbox" class="switch" />
Finally add a pseudo called :checked
which checks if a checkbox, or a radio, was checked; you'll want to change the before pseudo if the the checkbox is checked. You need to set the left to a bit less than the width of the checkbox; you can adjust it until it looks right to you.
<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: ""; color: transparent; width: 14px; height: 14px; } input[type=checkbox].switch:checked::before { left: unset !important; right: 1px; } </style> <input type="checkbox" class="switch" />
Now, let's look at what it looks like:
1--- 2title: Creating a Switch in HTML and CSS 3date: 2022 Feb 11 4updated: 2023 Feb 1 5--- 6A switch is something that is basically just a nicer checkbox. Here, I'll be showing you how to make one. 7 8The first step is to create checkbox with any classname; I'll be using `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```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 abit less than the height of the main switch, a display of inline-block, a top and left of 0, a content of anything, color of transparent, 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: ""; 50color: transparent; 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, or a radio, was checked; you'll want to change the before pseudo if the the checkbox is checked. You need to set the left to a bit less than the width of the checkbox; you can adjust it until it looks right to you. 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: ""; 78color: transparent; 79width: 14px; 80height: 14px; 81} 82input[type=checkbox].switch:checked::before { 83left: unset !important; 84right: 1px; 85} 86</style> 87<input type="checkbox" class="switch" /> 88``` 89 90Now, let's look at what it looks like: 91 92<iframe style="background:#fff;border:none" src="/blog-files/switch-final.html"></iframe> 93 94[See Code](/blog-files/switch-final.txt)