Skip to content

Instantly share code, notes, and snippets.

@SamantazFox
Created April 18, 2021 17:21
Show Gist options
  • Save SamantazFox/c87a7c4930f8ae59050a84a8b4e044c3 to your computer and use it in GitHub Desktop.
Save SamantazFox/c87a7c4930f8ae59050a84a8b4e044c3 to your computer and use it in GitHub Desktop.
Tiny CSS-only on/off button
<div>
<!-- This can be placed in any structure element, like <div>, <td>, etc... -->
<input id="my_checkbox1" name="input1" type="checkbox" hidden="">
<label for="my_checkbox1"><span></span></label>
</div>
/* On-Off Indicator */
label {
--color-enabled: #18984a;
--color-disabled: #7d7d7d;
width: 2.8em; height: 1.4em;
margin: 0; padding: 0;
display: inline-block;
background-color: var(--color-disabled);
border-top-left-radius: 25% 50%;
border-bottom-left-radius: 25% 50%;
border-top-right-radius: 25% 50%;
border-bottom-right-radius: 25% 50%;
}
label > span {
display: block;
background-color: #fff;
width: 1em;
height: 1em;
border-radius: 50%;
margin: 0.2em;
padding: 0;
float: left;
}
/* Modfify nearby indicator when checkbox is checked */
input[type="checkbox"]:checked ~ label { background-color: var(--color-enabled); }
input[type="checkbox"]:checked ~ label > span { float: right; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment