This question already has answers here:
Can I target a :before or :after pseudo-element with a sibling combinator?
(2 answers)
Closed 6 years ago.
I wanted to create a custom radio button using ::after. My HTML structure looks like this:
<div class='radio'>
<input type='radio' class='radio-input'>
</div>
and what I want is to add an ::after to the radio element and apply some style when the input is checked. What I wrote (and is not working) is this:
.radio-input:checked ~ .radio::after {
...
}
If I use an element instead of the ::after it works but I wanted to know why this doesn't.
The reason the selector .radio-input:checked ~ .radio::after doesn't work with your markup is because you can't target ancestor elements, with any selector. Yours starts with the child (.radio-input), goes up to the parent (.radio) and then back down to the child pseudo. CSS cascades down, it doesn't ascend.
Instead give the input a true sibling. Here I use a label because it conforms to accessibility standards which make sure screen readers can interpret your form:
input:checked ~ label::before
with markup like:
<input type="checkbox" name="dubstep" id="dubstep" value="dubstep" tabindex="0">
<label for="dubstep">dubstep</label>
See example:
ul.check-or-radio-set {
list-style-type: none;
padding: 0;
margin: 0;
position:relative;
}
ul.check-or-radio-set li {
padding:0;
margin:0 0 10px 0;
height:1.6em;
}
ul.check-or-radio-set input {
position: absolute;
left: -9999em;
}
ul.check-or-radio-set input ~ label::before {
background: white;
border: 2px white solid;
box-shadow: 0 0 0 1px darkGray;
content: '\a0';
display: inline-block;
line-height: 1;
text-indent: .15em;
}
ul.check-or-radio-set input[type=checkbox] ~ label {
position: relative;
margin-left: 32px;
line-height: 1.4em;
}
ul.check-or-radio-set input[type=checkbox] ~ label::before {
position: absolute;
margin-left: -32px;
height: 1.4em;
width: 1.4em;
border-radius: 4px;
transition: all .3s;
background-image: url(http://imgh.us/checkmark-white.svg);
background-position: 50%;
background-repeat: no-repeat;
margin-right: 10px;
}
/* checked */
ul.check-or-radio-set input:checked ~ label::before {
background-color: orange;
}
ul.check-or-radio-set input:focus ~ label::before {
box-shadow: 0 0 5px 1px #007fea;
}
ul.check-or-radio-set input:disabled ~ label {
color: lightGray;
cursor: not-allowed;
}
ul.check-or-radio-set input:disabled ~ label::before {
background-color: gray;
box-shadow: 0 0 0 1px darkGray;
}
<ul class="check-or-radio-set">
<li>
<input type="checkbox" name="rock" id="rock" value="rock" tabindex="0">
<label for="rock">rock</label>
</li>
<li>
<input type="checkbox" name="pop" id="pop" value="pop" tabindex="0" checked="checked">
<label for="pop">pop</label>
</li>
<li>
<input type="checkbox" name="dubstep" id="dubstep" value="dubstep" tabindex="0">
<label for="dubstep">dubstep</label>
</li>
</ul>
Note: I got this pattern from here: https://standards.usa.gov/form-controls/ - it's a great resource for making great looking, accessible form controls.
Related
<label>
<input type="checkbox"> todo
</label>
'display:none;' property is given to the input, and the label is styled to look like a picture.
What if I want to change the background color of the label when the checkbox is pressed?
advice please 🥲
I think this example do the thing.
.checkbox {
position: realtive;
}
.checkbox input {
display: none;
}
.checkbox-condition {
display: inline-block;
border: 1px solid gray;
border-radius: 5px;
padding: 30px 50px;
cursor: pointer;
transition: background .4s;
}
.checkbox-condition:hover {
background: lightgray;
}
.checkbox input:checked + .checkbox-condition {
background: green;
}
<label class="checkbox">
<input type="checkbox" />
<span class="checkbox-condition">TODO</span>
</label>
I am trying to do simple checkbox styling.
The color change is not working.
How to change the color and border?
input[type=checkbox]{
height: 25px;
width: 25px;
background-color: #eee;
}
input[type=checkbox]:checked {
background-color: #3ee738;
}
<p>
<input type="checkbox" id="showall" name="showall" value=0>
<label for="showall">Show All</label>
</p>
<p>
<input type="checkbox" id="showfl" name="showfl" value=0>
<label for="showfl">Filtered</label>
</p>
<p>
input[type=checkbox] + label {
display: block;
margin: 0.2em;
cursor: pointer;
padding: 0.2em;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox] + label:before {
content: "\2714";
border: 0.1em solid MediumSeaGreen;
border-radius: 0.2em;
display: inline-block;
width: 1em;
height: 1em;
padding-left: 0.2em;
padding-bottom: 0.3em;
margin-right: 0.2em;
vertical-align: bottom;
color: transparent;
transition: .2s;
}
input[type=checkbox] + label:active:before {
transform: scale(0);
}
input[type=checkbox]:checked + label:before {
background-color: MediumSeaGreen;
border-color: MediumSeaGreen;
color: #fff;
}
input[type=checkbox]:disabled + label:before {
transform: scale(1);
border-color: #aaa;
}
input[type=checkbox]:checked:disabled + label:before {
transform: scale(1);
background-color: #bfb;
border-color: #bfb;
}
<p>
<input type="checkbox" id="showall" name="showall" value=0>
<label for="showall">Show All</label>
</p>
<p>
<input type="checkbox" id="showfl" name="showfl" value=0>
<label for="showfl">Filtered</label>
</p>
<p>
Checkboxes are not currently able to be styled with CSS, as they are typically based on the user's operating system's default styling. The way to get around this with CSS is to style another element, in your case, the <label> tag to serve as the visual indicator of the checkbox, and then some pseudo selector and pseudo element CSS to trigger the change when the input is checked.
Because the input and the label are connected via the id and for attributes, shrinking the input hide it works effectively and still maintains accessibility.
li {
position:relative;
}
input {
height:1px;
width:1px;
position:absolute;
opacity:0;
top:0;
left:0;
}
label {
cursor:pointer;
padding-left:25px;
}
label::before {
content:"";
position:absolute;
left:0;
top:0;
height:15px;
width:15px;
border:1px solid blue;
}
label::after {
content:"";
color:white;
position:absolute;
left:2px;
top:0;
}
input[type=checkbox]:checked + label::before {
background:blue;
}
input[type=checkbox]:checked + label::after {
content:"✓";
}
<ul>
<li><input id="cat" type="checkbox"> <label for="cat">Cat</label></li>
<li><input id="dog" type="checkbox"> <label for="dog">Dog</label></li>
</ul>
What you need to do is to hide the actual html element of the checkbox and create a new one that can be styled easily.
So the logic of this approach is to have a span for the box and a span for the icon and style each accordingly - showing the icon when the input is checked using the :checked pseudoselector.
Note that the label wraps around the whole group so that clicking anywhere on the label will toggle the checkbox - doing it this way removeds the need for the for attribute.
I also added a checked function that will toggle the other checkbox whebn the showAll check is checked.
Edit - note that you can use other methods of getting the icon in there- you may prefer the font awesome fa-check icon for example.
const showAll = document.querySelector("#showAll");
showAll.addEventListener('click', () => toggleAll());
function toggleAll() {
document.querySelector("#showfl").checked = showAll.checked;
}
.checkbox-wrapper {
margin-bottom: 16px;
}
input[type=checkbox]{
display: none
}
.checkbox-box{
display: inline-block;
height: 25px;
width: 25px;
background-color: #eee;
text-align: center
}
.checkbox-icon {
visibility: hidden
}
input[type=checkbox]:checked + .checkbox-box{
background-color: #3ee738;
}
input[type=checkbox]:checked + .checkbox-box .checkbox-icon {
visibility: visible
}
<div class="checkbox-wrapper">
<label>
<input type="checkbox" id="showAll" name="showAll" />
<span class="checkbox-box">
<span class="checkbox-icon">✓</span>
</span> Show All
</label>
</div>
<div class="checkbox-wrapper">
<label>
<input type="checkbox" id="showfl" name="showfl" />
<span class="checkbox-box">
<span class="checkbox-icon">✓</span>
</span> Filtered
</label>
</div>
How do I style a checkbox in firefox, and have the checkmark and border disappear?
http://jsfiddle.net/moneylotion/qZvtY/
CSS:
body { background: black; }
#conditions-form { color: white; }
#conditions-form input[type=checkbox] {
display:inline-block;
-webkit-appearance: none;
-moz-appearance: none;
-o-appearance:none;
appearance: none;
width:19px;
height:19px;
background: url('http://demo.somedomain.com/wp-content/themes/themename/images/care-plan-checkbox.gif') no-repeat top left;
cursor:pointer;
}
#conditions-form input[type=checkbox]:checked {
background:url('http://demo.somedomain.com/wp-content/themes/themename/images/care-plan-checkbox-checked.gif') no-repeat top left;
}
HTML:
<form id="conditions-form">
<ul>
<li>
<input id="condition3" type="checkbox" name="conditions[condition3]"></input>
<label class="checkbox" for="condition3">Conditions 3</label>
</li>
</ul>
</form>
There's a quite easy way you can do this via <label> tags. Just place a label around the checkbox, and insert a dummy element that will be used for the custom styled checkbox. For example:
label.checkbox input[type="checkbox"] {display:none;}
label.checkbox span {
display:inline-block;
border:2px solid #BBB;
border-radius:10px;
width:25px;
height:25px;
background:#C33;
vertical-align:middle;
margin:3px;
position: relative;
transition:width 0.1s, height 0.1s, margin 0.1s;
}
label.checkbox :checked + span {
background:#6F6;
width:27px;
height:27px;
margin: 2px;
}
label.checkbox :checked + span:after {
content: '\2714';
font-size: 20px;
position: absolute;
top: -2px;
left: 5px;
color: #99a1a7;
}
<label class="checkbox">
<input type="checkbox"/>
<span></span>
I like cake
</label>
EDIT: Note that some choices of colours might render the state of your checkbox invisible for colourblind people. When making this code I didn't think of that, but the above demo might be invisible for R/G colourblind people. When implementing this, please do keep that in mind (pick bright/dark colours for example, or show some difference in shape)
The styles I used are just arbitrary, and you can change that to anything you want. You can even toggle certain text inside it via the ::before pseudo-element, such as what I've done here.
I wasn't able to open the image url you provided to use in your question, but I think you'll be able to include whatever image you want by simply modifying this code a little. Just change the current background color to the image URL you want to use.
Note: This won't work in some older browsers.
The accepted answer above is great but this slight tweak to the fiddle from Joeytje50 allows the check-boxes to be tabbed to.
Using opacity 0 instead of display none means the checkbox is still tabbable and hence accessible by keyboard.
Position absolute places the input checkbox top left of the drawn box meaning your formatting stays neat.
input[type="checkbox"] {
opacity: 0;
position: absolute;
}
input[type="checkbox"] + label {
text-align:center;
cursor:pointer;
}
input[type="checkbox"]:focus + label {
background-color: #ddd;
}
input[type="checkbox"] + label div {
display:inline-block;
line-height: 16px;
font-size: 12px;
height: 16px;
width: 16px;
margin:-0px 4px 0 0;
border: 1px solid black;
color: transparent;
}
input[type="checkbox"]:checked + label div {
color: black;
}
<input type="checkbox" id="c1" name="cc" />
<label for="c1">
<div>✔</div>Check Box 1<br />
</label>
<input type="checkbox" id="c12" name="cc" />
<label for="c12">
<div>✔</div>Check Box 2<br />
</label>
This tutsplus tutorial solved my question.
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label span {
display:inline-block;
width:19px;
height:19px;
margin:-1px 4px 0 0;
vertical-align:middle;
background:url(https://cdn.tutsplus.com/webdesign/uploads/legacy/tuts/391_checkboxes/check_radio_sheet.png) left top no-repeat;
cursor:pointer;
}
input[type="checkbox"]:checked + label span {
background:url(https://cdn.tutsplus.com/webdesign/uploads/legacy/tuts/391_checkboxes/check_radio_sheet.png) -19px top no-repeat;
}
<input type="checkbox" id="c1" name="cc" />
<label for="c1"><span></span>Check Box 1</label>
A cleaner solution IMHO that uses pure css to redraw the elements.
Codepen
input[type="checkbox"] {
opacity: 0;
position: absolute;
}
input[type="checkbox"] ~ label:before {
content: '';
display: inline-block;
cursor: pointer;
...
border: 3px solid #999;
border-radius: 2px;
transition: .3s;
}
input[type="checkbox"]:checked ~ label:before {
background: #333;
}
This question already has answers here:
How to style a checkbox using CSS
(43 answers)
Closed 1 year ago.
How do I change the color of the check mark within an HTML checkbox input?
Here's a pure CSS solution that shouldn't break screen readers or default user agent actions. Additionally, this is supported in the latest versions of the big 4 browsers (and a few others if you add some additional hacks, but I'll leave that to you to figure out; probably won't get more than IE8+ support since it uses pseudo elements).
The idea is to hide the actual form element (because browsers do a hard replace with internal styles and don't expose all style-ability to css yet) and replace it with one we like. One side effect is that you will want to track change events rather than click events in your JS if you need it (but you were doing that anyway right?).
Because the label is tied to the form element clicking it works like one would expect, so the new, awesome, checkbox (::before) abuses attribute selectors ([checked]) on the original to check if it is checked. When it is checked it will display our awesomer checkmark (::after).
The checkmark (::after) abuses border width for thickness and height/width for making a checkmark like item. Finally, we transform the box 45deg to match the angle up properly.
To change the color of the checkmark, change the border color on the ::after style. Additionally, if you wanted it to always match your text color remove the border color on it altogether. To change the radio, change the radial gradient start color (the one that isn't white).
Also awesome is that its tied to font size, so if your text is bigger, it should shim right in (though rounding errors can happen when using relative font sizes, so be careful)
I've included basic styles for both check-able types (checkbox and radio).
HTML:
<fieldset>
<legend>Checkbox example</legend>
<input id="checkbox" type="checkbox"/>
<label for="checkbox">Some awesome checkbox label</label>
</fieldset>
<fieldset>
<legend>Radio example</legend>
<div>
<input id="radio1" type="radio" name="radio"/>
<label for="radio1">Some awesome radio option #1</label>
<div>
</div>
<input id="radio2" type="radio" name="radio"/>
<label for="radio2">Some awesome radio option #2</label>
</div>
</fieldset>
CSS:
label, input[type="radio"], input[type="checkbox"] {
line-height: 2.1ex;
}
input[type="radio"],
input[type="checkbox"] {
position: absolute;
left: -999em;
}
input[type="radio"] + label,
input[type="checkbox"] + label {
position: relative;
overflow: hidden;
cursor: pointer;
}
input[type="radio"] + label::before,
input[type="checkbox"] + label::before {
content: "";
display: inline-block;
vertical-align: -25%;
height: 2ex;
width: 2ex;
background-color: white;
border: 1px solid rgb(166, 166, 166);
border-radius: 4px;
box-shadow: inset 0 2px 5px rgba(0,0,0,0.25);
margin-right: 0.5em;
}
input[type="radio"]:checked + label::before {
background: radial-gradient(circle at center, #1062a4 .6ex, white .7ex);
}
input[type="radio"] + label::before {
border-radius: 50%;
}
input[type="checkbox"]:checked + label::after {
content: '';
position: absolute;
width: 1.2ex;
height: 0.4ex;
background: rgba(0, 0, 0, 0);
top: 0.9ex;
left: 0.4ex;
border: 3px solid #1062a4;
border-top: none;
border-right: none;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
transform: rotate(-45deg);
}
Side note: necropost because this was the first question that popped up when I was trying to remember how I pulled this off in the past. ;)
You could create a checkbox image and use that as your checkbox
The following post discusses custom input controls...
http://www.thecssninja.com/css/custom-inputs-using-css
If you need to change tick color from black to white, just try applying filter: invert(1) to the checkbox.
Check this It will Show you how to style a checkbox
How to create a custom checkbox You can do it without JS
/* Customize the label (the container) */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196F3;
}
.container input.white:checked ~ .checkmark:after {
border: solid white;
border-width: 0 3px 3px 0;
}
.container input.black:checked ~ .checkmark:after {
border: solid #000;
border-width: 0 3px 3px 0;
}
.container input.red:checked ~ .checkmark:after {
border: solid #cb1a1a;
border-width: 0 3px 3px 0;
}
.container input.green:checked ~ .checkmark:after {
border: solid #1f4f12;
border-width: 0 3px 3px 0;
}
.container input.yellow:checked ~ .checkmark:after {
border: solid #c6c253;
border-width: 0 3px 3px 0;
}
<label class="container">Black
<input type="checkbox" class="black" checked="checked">
<span class="checkmark"></span>
</label>
<label class="container">White
<input type="checkbox" class="white">
<span class="checkmark"></span>
</label>
<label class="container">Yellow
<input type="checkbox" class="yellow">
<span class="checkmark"></span>
</label>
<label class="container">Red
<input type="checkbox" class="red">
<span class="checkmark"></span>
</label>
<label class="container">Green
<input type="checkbox" class="green">
<span class="checkmark"></span>
</label>
You can do like this.
input[type='checkbox']:checked {
background-color: #023047;
}
input[type='checkbox']:checked:after {
content: '\2713';
color:white;
}
input[type='checkbox']{
text-align: center;
display: table-cell;
vertical-align: middle;
width: 20px !important;
height: 20px !important;
appearance:none;
border-radius:10%;
border: 1px solid rgb(191 35 42 / 90%);
box-shadow: none;
font-size: 1em;
}
<input type="checkbox" > checkbox 1
<input type="checkbox" > checkbox 2
You can imitate a check box with another element and set the background color as desired.
<span onclick="this.innerHTML = (this.innerHTML ? '' : 'X')"></span>
<style>
span{
display:inline-block;
width:10px; height:10px;
font:10px/10px 'Sans Serif'; color: green;
border:solid 1px black;
vertical-align:middle;
cursor:pointer;
}
</style>
You can get a little fancier by using ::before or after
<span class='checked' onclick="this.classList.toggle('checked')"></span>
<style>
span{
display:inline-block;
height: 10px; width:10px;
border:solid 1px black;
vertical-align:middle;
cursor:pointer;
}
span.checked::before{
content:'×';
display:block; height: 10px;
font:10px/10px 'Sans Serif';
color:green;
}
</style>
You can extend this, by using background image or a svg sprite in the ::after tag (see https://stackoverflow.com/a/19255455/87520)
I haven't tried to make it perfect, just to demonstrate the concept.
As you can see, the background color is green, no images, no libraries involved; minimal js.
If you use b-form-checkbox and you will find css of mark is svg like that...
.custom-checkbox
.custom-control-input:checked
~ .custom-control-label::after {
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath **fill='%23000'** d='M6.564.75l-3.59 3.612-1.538-1.55L0 4.26 2.974 7.25 8 2.193z'/%3e%3c/svg%3e");
It's drawn by svg, so you can change coordinate to modify mark or change fill to change mark color.
How can I change checkbox (input) border's style? I've put border:1px solid #1e5180 upon it, but in FireFox 3.5, nothing happens!
I suggest using "outline" instead of "border". For example: outline: 1px solid #1e5180.
You should use
-moz-appearance:none;
-webkit-appearance:none;
-o-appearance:none;
Then you get rid of the default checkbox image/style and can style it. Anyway a border will still be there in Firefox
If something happens in any browser I'd be surprised. This is one of those outstanding form elements that browsers tend not to let you style that much, and that people usually try to replace with javascript so they can style/code something to look and act like a checkbox.
You can use box shadows to fake a border:
-webkit-box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
-moz-box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
Here's my version that uses FontAwesome for checkbox ticker, I think FontAwesome is used by almost everybody so it's safe to assume you have it too. Not tested in IE/Edge and I don't think anyone cares.
input[type=checkbox] {
-moz-appearance:none;
-webkit-appearance:none;
-o-appearance:none;
outline: none;
content: none;
}
input[type=checkbox]:before {
font-family: "FontAwesome";
content: "\f00c";
font-size: 15px;
color: transparent !important;
background: #fef2e0;
display: block;
width: 15px;
height: 15px;
border: 1px solid black;
margin-right: 7px;
}
input[type=checkbox]:checked:before {
color: black !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<input type="checkbox">
Here is a pure CSS (no images) cross-browser solution based on Martin's Custom Checkboxes and Radio Buttons with CSS3 LINK: http://martinivanov.net/2012/12/21/imageless-custom-checkboxes-and-radio-buttons-with-css3-revisited/
Here is a jsFiddle: http://jsfiddle.net/DJRavine/od26wL6n/
I have tested this on the following browsers:
FireFox (41.0.2) (42)
Google Chrome (46.0.2490.80 m)
Opera (33.0.1990.43)
Internet Explorer (11.0.10240.16431 [Update Versions: 11.0.22])
Microsoft Edge (20.10240.16384.0)
Safari Mobile iPhone iOS9 (601.1.46)
label,
input[type="radio"] + span,
input[type="radio"] + span::before,
label,
input[type="checkbox"] + span,
input[type="checkbox"] + span::before
{
display: inline-block;
vertical-align: middle;
}
label *,
label *
{
cursor: pointer;
}
input[type="radio"],
input[type="checkbox"]
{
opacity: 0;
position: absolute;
}
input[type="radio"] + span,
input[type="checkbox"] + span
{
font: normal 11px/14px Arial, Sans-serif;
color: #333;
}
label:hover span::before,
label:hover span::before
{
-moz-box-shadow: 0 0 2px #ccc;
-webkit-box-shadow: 0 0 2px #ccc;
box-shadow: 0 0 2px #ccc;
}
label:hover span,
label:hover span
{
color: #000;
}
input[type="radio"] + span::before,
input[type="checkbox"] + span::before
{
content: "";
width: 12px;
height: 12px;
margin: 0 4px 0 0;
border: solid 1px #a8a8a8;
line-height: 14px;
text-align: center;
-moz-border-radius: 100%;
-webkit-border-radius: 100%;
border-radius: 100%;
background: #f6f6f6;
background: -moz-radial-gradient(#f6f6f6, #dfdfdf);
background: -webkit-radial-gradient(#f6f6f6, #dfdfdf);
background: -ms-radial-gradient(#f6f6f6, #dfdfdf);
background: -o-radial-gradient(#f6f6f6, #dfdfdf);
background: radial-gradient(#f6f6f6, #dfdfdf);
}
input[type="radio"]:checked + span::before,
input[type="checkbox"]:checked + span::before
{
color: #666;
}
input[type="radio"]:disabled + span,
input[type="checkbox"]:disabled + span
{
cursor: default;
-moz-opacity: .4;
-webkit-opacity: .4;
opacity: .4;
}
input[type="checkbox"] + span::before
{
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
}
input[type="radio"]:checked + span::before
{
content: "\2022";
font-size: 30px;
margin-top: -1px;
}
input[type="checkbox"]:checked + span::before
{
content: "\2714";
font-size: 12px;
}
input[class="blue"] + span::before
{
border: solid 1px blue;
background: #B2DBFF;
background: -moz-radial-gradient(#B2DBFF, #dfdfdf);
background: -webkit-radial-gradient(#B2DBFF, #dfdfdf);
background: -ms-radial-gradient(#B2DBFF, #dfdfdf);
background: -o-radial-gradient(#B2DBFF, #dfdfdf);
background: radial-gradient(#B2DBFF, #dfdfdf);
}
input[class="blue"]:checked + span::before
{
color: darkblue;
}
input[class="red"] + span::before
{
border: solid 1px red;
background: #FF9593;
background: -moz-radial-gradient(#FF9593, #dfdfdf);
background: -webkit-radial-gradient(#FF9593, #dfdfdf);
background: -ms-radial-gradient(#FF9593, #dfdfdf);
background: -o-radial-gradient(#FF9593, #dfdfdf);
background: radial-gradient(#FF9593, #dfdfdf);
}
input[class="red"]:checked + span::before
{
color: darkred;
}
<label><input type="radio" checked="checked" name="radios-01" /><span>checked radio button</span></label>
<label><input type="radio" name="radios-01" /><span>unchecked radio button</span></label>
<label><input type="radio" name="radios-01" disabled="disabled" /><span>disabled radio button</span></label>
<br/>
<label><input type="radio" checked="checked" name="radios-02" class="blue" /><span>checked radio button</span></label>
<label><input type="radio" name="radios-02" class="blue" /><span>unchecked radio button</span></label>
<label><input type="radio" name="radios-02" disabled="disabled" class="blue" /><span>disabled radio button</span></label>
<br/>
<label><input type="radio" checked="checked" name="radios-03" class="red" /><span>checked radio button</span></label>
<label><input type="radio" name="radios-03" class="red" /><span>unchecked radio button</span></label>
<label><input type="radio" name="radios-03" disabled="disabled" class="red" /><span>disabled radio button</span></label>
<br/>
<label><input type="checkbox" checked="checked" name="checkbox-01" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" /><span>disabled checkbox</span></label>
<br/>
<label><input type="checkbox" checked="checked" name="checkbox-01" class="blue" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" class="blue" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" class="blue" /><span>disabled checkbox</span></label>
<br/>
<label><input type="checkbox" checked="checked" name="checkbox-01" class="red" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" class="red" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" class="red" /><span>disabled checkbox</span></label>
For Firefox, Chrome and Safari, nothing happens.
For IE the border is applied outside the checkbox (not as part of the checkbox), and the "fancy" shading effect in the checkbox is gone (displayed as an oldfashioned checkbox).
For Opera the border style is actually applying the border on the checkbox element.
Opera also handles other stylings on the checkbox better than other browsers: color is applied as the color of the tick, background-color is applied as background color inside the checkbox (IE applies the background as if the checkbox was inside a <div> with background)).
Conclusion
The easiest solution is to wrap the checkbox inside a <div> like others have suggested.
If you want to completely control the appearance you will have to go with the advanced image/javascript approach, also meantiond by others.
I'm outdated I know.. But a little workaround would be to put your checkbox inside a label tag, then style the label with a border:
<label class='hasborder'><input type='checkbox' /></label>
then style the label:
.hasborder { border:1px solid #F00; }
Styling checkboxes (and many other input elements for that mater) is not really possible with pure css if you want to drastically change the visual appearance.
Your best bet is to implement something like jqTransform does which actually replaces you inputs with images and applies javascript behaviour to it to mimic a checkbox (or other element for that matter)
No, you still can't style the checkbox itself, but I (finally) figured out how to style an illusion while keeping the functionality of clicking a checkbox. It means that you can toggle it even if the cursor isn't perfectly still without risking selecting text or triggering drag-and-drop!
The example is using a span "button" as well as some text in a label, but it gives you the idea of how you can make the checkbox invisible and draw anything behind it.
This solution probably also fits radio buttons.
The following works in IE9, FF30.0 and Chrome 40.0.2214.91 and is just a basic example. You can still use it in combination with background images and pseudo-elements.
http://jsfiddle.net/o0xo13yL/1/
label {
display: inline-block;
position: relative; /* needed for checkbox absolute positioning */
background-color: #eee;
padding: .5rem;
border: 1px solid #000;
border-radius: .375rem;
font-family: "Courier New";
font-size: 1rem;
line-height: 1rem;
}
label > input[type="checkbox"] {
display: block;
position: absolute; /* remove it from the flow */
width: 100%;
height: 100%;
margin: -.5rem; /* negative the padding of label to cover the "button" */
cursor: pointer;
opacity: 0; /* make it transparent */
z-index: 666; /* place it on top of everything else */
}
label > input[type="checkbox"] + span {
display: inline-block;
width: 1rem;
height: 1rem;
border: 1px solid #000;
margin-right: .5rem;
}
label > input[type="checkbox"]:checked + span {
background-color: #666;
}
<label>
<input type="checkbox" />
<span> </span>Label text
</label>
<!DOCTYPE html>
<html>
<head>
<style>
.abc123
{
-webkit-appearance:none;
width: 14px;
height: 14px;
display: inline-block;
background: #FFFFFF;
border: 1px solid rgba(220,220,225,1);
}
.abc123:after {
content: "";
display: inline-block;
position: relative;
top: -3px;
left: 4px;
width: 3px;
height: 5px;
border-bottom: 1px solid #fff;
border-right: 1px solid #fff;
-webkit-transform: rotate(45deg);
}
input[type=checkbox]:checked {
background: #327DFF;
outline: none;
border: 1px solid rgba(50,125,255,1);
}
input:focus,input:active {
outline: none;
}
input:hover {
border: 1px solid rgba(50,125,255,1);
}
</style>
</head>
<body>
<input class="abc123" type="checkbox"></input>
</body>
</html>
Here is a simple way (to use before or after pseudo elements / classes):
input[type=checkbox] {
position: relative;
}
input[type=checkbox]:after {
position: absolute;
top: 0;
left: 0;
/* Above three lines allow the checkbox:after position at checkbox's position */
content: '';
width: 32px;
height: 32px;
z-index: 1; /* This allows the after overlap the checkbox */
/* Anything you want */
}
It's actually just two things you have to do
outline: 1px solid #63DDCF
border: none !important;
put it in a div and add border to the div
<div style="border-style: solid;width:13px">
<input type="checkbox" name="mycheck" style="margin:0;padding:0;">
</input>
</div>