Skip to content

Instantly share code, notes, and snippets.

@pcalves
Created March 28, 2014 17:21
Show Gist options
  • Save pcalves/9838037 to your computer and use it in GitHub Desktop.
Save pcalves/9838037 to your computer and use it in GitHub Desktop.
Custom radio buttons & checkboxes with CSS
/**
* Custom radio buttons & checkboxes with CSS
* http://lea.verou.me/css3-secrets/#slide31
*/
/**
* These styles are for this dabblet only
*/
body {
color: #111;
font: 18px sans-serif;
}
/**
* Hide default checkbox and radio button elements
* https://developer.mozilla.org/en-US/docs/Web/CSS/clip
*/
[type=radio],
[type=checkbox] {
position: absolute;
clip: rect(0 0 0 0);
}
label {
position: relative;
cursor: pointer;
}
[disabled] + label {
cursor: default;
}
/**
* Custom radio button as pseudo-element;
* display: inline-block makes it flow with label text
* set width, height, line-height and font-size
*/
[type=radio] + label:before {
vertical-align: middle;
text-align:center;
content: '';
width: 18px;
height: 18px;
line-height:18px;
font-size: 18px;
margin-right: 5px;
background-color: grey;
display:inline-block;
border-radius: 50%;
box-shadow: inset 0 2px 2px 0 black;
}
/**
* Insert ASCII content on checked elements
*/
[type=radio]:checked + label:before {
content: "\25CF";
}
/**
* Styling disabled inputs
*/
[type=radio][disabled] + label:before {
box-shadow: none;
background-color: lightgrey;
}
/**
* Checkboxes are styled pretty much like radio buttons!
*/
[type=checkbox] + label:before {
vertical-align: middle;
text-align:center;
content: '';
width: 18px;
height: 18px;
line-height:18px;
font-size: 18px;
margin-right: 5px;
background-color: lightgrey;
display:inline-block;
border-radius: 3px;
border: 2px solid black;
}
[type=checkbox]:checked + label:before {
content: "\2717";
}
[type=checkbox][disabled] + label:before {
border-color: lightgrey;
background-color: white;
}
<p>Custom radio buttons</p>
<input type="radio" id="ropt1" name="radio">
<label for="ropt1">Option 1</label>
<input type="radio" id="ropt2" name="radio">
<label for="ropt2">Option 2</label>
<input type="radio" id="ropt3" name="radio" disabled>
<label for="ropt3">Option 3 (disabled)</label>
<p>Custom checkboxes</p>
<input type="checkbox" id="copt1" name="checkbox">
<label for="copt1">Option 1</label>
<input type="checkbox" id="copt2" name="checkbox">
<label for="copt2">Option 2</label>
<input type="checkbox" id="copt2" name="checkbox" disabled>
<label for="copt2">Option 3 (disabled)</label>
// alert('Hello world!');
{"view":"split-vertical","fontsize":"100","seethrough":"","prefixfree":"1","page":"all"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment