steve0greatness,
created on Saturday, 3 February 2024, 06:40:48 (1706942448),
received on Monday, 6 May 2024, 02:55:37 (1714964137)
Author identity: Steve0Greatness <steve0greatnessiscool@gmail.com>
f4625b430c79e143123827b0d4bd182cf1279c0f
blog-posts/creatingaswitch.md
@@ -1,11 +1,11 @@
--- title: Creating a Switch in HTML and CSS date: 2022 Feb 11 updated: 2023 Feb 1updated: 2023 Feb 2--- A switch is something that is basically just a nicer checkbox. Here, I'll be showing you how to make one.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 classname; I'll be using `switch`.The first step is to create checkbox with any class; I'll be using `switch`, because it's a switch.```html <style>
@@ -15,6 +15,7 @@ The first step is to create checkbox with any classname; I'll be using `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 ```html <style> input[type=checkbox].switch {
@@ -27,7 +28,7 @@ Next step is to add an appearance of none to the CSS--make sure to add `-moz-` a
<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.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.```html <style>
@@ -47,7 +48,6 @@ Next we want to create `::before` pseudo. In there we want to make it have a pos
display: block; background: red; content: ""; color: transparent;width: 14px; height: 14px; }
@@ -55,7 +55,7 @@ Next we want to create `::before` pseudo. In there we want to make it have a pos
<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.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.```html <style>
@@ -75,19 +75,53 @@ Finally add a pseudo called `:checked` which checks if a checkbox, or a radio, w
display: block; background: red; content: ""; color: transparent;width: 14px; height: 14px; } input[type=checkbox].switch:checked::before { left: unset !important;right: 1px;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): ```html <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" /> ``` Now, let's look at what it looks like:All of this results in a final product that looks like:<iframe style="background: #fff; border: none" src="/blog-files/switch-final.html"></iframe>
static/blog-files/switch-final.html
@@ -1,14 +1,18 @@
<!DOCTYPE html> <html> <body><head><style> :root { background: #000; color: #fff; }input[type=checkbox].switch { -webkit-appearance: none; -moz-appearance: none; appearance: none; position: relative; background: #000;background: #fff;width: 30px; height: 16px; }
@@ -17,17 +21,20 @@
left: 1px; position: absolute; display: block; background: red;background-color: #ff5c1c;content: ""; color: transparent;width: 14px; height: 14px; transition: background-color .5s, left .5s;} input[type=checkbox].switch:checked::before { left: unset !important;right: 1px;left: 15px; /* .switch width - .switch::before width - 1px */ background-color: #3f9b00;} </style> </head> <body><input type="checkbox" class="switch" /> </body>
static/blog-files/switch-final.txt
@@ -1,32 +1,40 @@
<!DOCTYPE html> <html> <body><head><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: relative;position: absolute;display: block; background: red;background-color: #ff5c1c;content: ""; color: transparent;width: 14px; height: 14px; transition: background-color .5s, left .5s;} input[type=checkbox].switch:checked::before { left: unset;right: 1px;left: 15px; /* .switch width - .switch::before width - 1px */ background-color: #3f9b00;} </style> </head> <body><input type="checkbox" class="switch" /> </body>