creatingaswitch.html
HTML document, ASCII text, with very long lines (2828)
1
<!DOCTYPE html>
2
<html lang="en-us" prefix="og: https://ogp.me/ns#">
3
4
<head>
5
6
7
<meta charset="UTF-8" />
8
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
9
<title>Creating a Switch in HTML and CSS - S0G</title>
10
<link rel="stylesheet" href="/src/global.css" />
11
<meta property="og:locale" content="en_US"/>
12
<meta property="og:site_name" content="Steve0Greatness' Site"/>
13
14
<link rel="stylesheet" href="/src/pygments-friendly.css" />
15
<link rel="stylesheet" href="/src/blog.css" />
16
<link rel="alternate" href="/blog/creatingaswitch.txt" type="text/plain" title="Post source" />
17
<meta property="og:title" content="Creating a Switch in HTML and CSS" />
18
<meta property="og:url" content="https://steve0greatness.github.io/blog/creatingaswitch.html" />
19
20
</head>
21
22
<body>
23
<header>
24
<h2><a href="/">Steve0Greatness</a></h2>
25
<nav>
26
<a href="/blog">Blog</a>
27
<a href="/link-tree.html">Link Tree</a>
28
</nav>
29
</header>
30
31
<ol role="navigation" class="breadcrumbs" aria-roledescription="Site breadcrumb">
32
<li>
33
<a href="/">Index</a>
34
</li>
35
36
<li >
37
<a href="/blog">
38
Blog Index
39
</a>
40
</li>
41
42
<li aria-current="location">
43
44
Creating a Switch in HTML and CSS
45
46
</li>
47
48
</ol>
49
<main>
50
<article>
51
<header>
52
<h2 id="blog-post-titled">Creating a Switch in HTML and CSS</h2>
53
<div role="toolbar" class="toolbar">
54
<a href="https://toot.kytta.dev/?text=Take a look at this article by @S0G@mastodon.social: https://steve0greatness.github.io/blog/creatingaswitch.html" title="Share to Mastodon">
55
<img src="/toot-kytta-dev-icon.svg" width="16" height="16" aria-hidden="true" title="Share to Mastodon" />
56
</a>
57
<a href="/blog/creatingaswitch.html" title="Direct link">
58
<span aria-hidden="true">
59
🔗
60
</span>
61
</a>
62
<a href="/blog/creatingaswitch.txt" title="Markdown source">
63
<img src="/md-src.svg" width="16" height="16" aria-hidden="true" />
64
</a>
65
</div>
66
<time>2022 Feb 11</time>
67
</header>
68
<p><p>A switch is something that is basically just a nicer checkbox. Here, I'll be showing you how to make one.</p>The first step is to create checkbox with any classname; I'll be using <span class="code">switch</span>.<div class="code"><style><div style="margin-left:1em;">input[type=checkbox].switch {}</div></style><br><input type="checkbox" class="switch"></div><p>Next step is to add an appearance of none to the CSS--make sure to add <span class="code">-moz-</span> and <span class="code">-webkit-</span>. After that, you're going to want to set it so that the checkbox a rectangle--make sure it's in <span class="code">px</span>. We also want to set the position to relative</p><div class="code"><style><div style="margin-left:1em;">input[type=checkbox].switch {<div style="margin-left:1em;">-webkit-appearance: none;<br>-moz-appearance: none;<br>appearance: none;<br>position: relative;<br>width: 30px;<br>height: 16px;</div>}</div></style><br><input type="checkbox" class="switch"></div><p>Next we want to create a <span class="code">::before</span> 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.</p><div class="code"><style><div style="margin-left:1em;">input[type=checkbox].switch {<div style="margin-left:1em;">-webkit-appearance: none;<br>-moz-appearance: none;<br>appearance: none;<br>position: relative;<br>width: 30px;<br>height: 16px;</div>}<br>input[type=checkbox].switch::before {<div style="margin-left:1em;">top: 0;<br>left: 0;<br>position: relative;<br>background: red;<br>content: ".";<br>color: transparent;<br>width: 14px;<br>height: 14px;</div>}</div></style><br><input type="checkbox" class="switch"></div><p>Finally add a pseudo called <span class="code">:checked</span> 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.</p>Now, let's look at what it looks like<br><iframe style="background:#fff;border:none" srcdoc='<style>input[type=checkbox].switch {-webkit-appearance: none;-moz-appearance: none;appearance: none;background: #efefef;position: relative;border: 1px black solid;width: 30px;height: 16px;border-radius: 0;}input[type=checkbox].switch::before {top: 0;left: 0.24px;position: relative;background: red;content: ".";color: transparent;width: 20px;height: 14px;}input[type=checkbox].switch:checked::before {left: 25px;}</style><input type="checkbox" class="switch">'></iframe></p>
69
70
</article>
71
</main>
72
<footer role="group">
73
<div class="footer-link-list-holder">
74
<span aria-hidden="true" id="footer-label-site-details" class="footer-link-list-label">Site Meta</span>
75
<ol class="footer-link-list" aria-labelledby="footer-label-site-details">
76
<li><a href="https://github.com/Steve0Greatness/steve0greatness.github.io">Github Repository</a></li>
77
<li><a href="https://steve0greatness.github.io/extras">Extras Archive</a></li>
78
</ol>
79
</div>
80
<div class="footer-link-list-holder">
81
<span aria-hidden="true" id="footer-label-social-accounts" class="footer-link-list-label">Social Accounts</span>
82
<ol class="footer-link-list" aria-labelledby="footer-label-social-accounts">
83
<li><a href="https://mastodon.social/@S0G" rel="me">Mastodon</a></li>
84
<li><a href="https://youtube.com/@s0g">YouTube</a></li>
85
<li><a href="/link-tree.html">More...</a></li>
86
</ol>
87
</div>
88
</footer>
89
</body>
90
91
</html>