Skip to content

Instantly share code, notes, and snippets.

@Integralist
Created November 20, 2012 08:41
Show Gist options
  • Save Integralist/4116778 to your computer and use it in GitHub Desktop.
Save Integralist/4116778 to your computer and use it in GitHub Desktop.
The Checkbox CSS Hack (doesn't work in IE <= 8, but all other browsers fine)
<label for="toggle-1">I'm a toggle</label>
<input type="checkbox" id="toggle-1">
<div>I'm controlled by toggle. No JavaScript!</div>
/* Checkbox Hack */
input[type=checkbox] {
position: absolute;
top: -9999px;
left: -9999px;
}
label {
-webkit-appearance: push-button;
-moz-appearance: button;
display: inline-block;
margin: 60px 0 10px 0;
cursor: pointer;
}
/* Default State */
div {
background: green;
width: 400px;
height: 100px;
line-height: 100px;
color: white;
text-align: center;
}
/* Toggled State */
input[type=checkbox]:checked ~ div {
background: red;
}
@jamesl1001
Copy link

Hey Mark, I've created a Javascript polyfill which adds support for IE <= 8.
Check it out: https://github.com/jamesl1001/checkbox-hack-polyfill

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment