Skip to content

Instantly share code, notes, and snippets.

@MathRivest
Last active November 1, 2019 08:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MathRivest/5413438 to your computer and use it in GitHub Desktop.
Save MathRivest/5413438 to your computer and use it in GitHub Desktop.
Custom SCSS radio/checkbox. No javascript. IE9+ TODO: Disabled states, Hover, focus etc, Fallback to real input for <IE9
/*
* Hide only visually, but have it available for screenreaders: h5bp.com/v
*/
.visuallyhidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
input[type="radio"],
input[type="checkbox"]{
@extend .visuallyhidden;
}
input[type="radio"] + label,
input[type="checkbox"] + label{
position:relative;
padding-left:25px;
&:before{
position:absolute;
left:0;
top:0;
content:" ";
display:block;
width:20px;
height:20px;
background:red;
}
&:hover:before{
//background:blue;
}
}
input[type="radio"] + label{
&:before{}
}
input[type="checkbox"] + label{
&:before{}
}
input[type="radio"]:focus + label,
input[type="checkbox"]:focus + label{
text-decoration: underline;
}
input[type="radio"]:checked + label{
&:before{
background:blue;
}
}
input[type="checkbox"]:checked + label{
&:before{
background:blue;
}
}
<input type="radio" id="credit" name="method" value="credit">
<label for="credit">Credit</label>
<input type="checkbox" id="giftcard" name="method" value="giftcard">
<label for="giftcard">Gift Card</label>
@Casao
Copy link

Casao commented Oct 15, 2014

Thanks so much for posting this! I've been looking for something compatible that didn't depend on spans or javascript. This did the trick 👍

@rttmax
Copy link

rttmax commented Jan 28, 2016

Thank you!

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