Customize Radio and Checkbox with CSS [duplicate] - css

This question already has answers here:
How to style a checkbox using CSS
(43 answers)
Closed 7 years ago.
Is it possible to customize the look of radio and checkboxes only using CSS? I see there is a lot out there regarding this, but most solutions require the use of images and javascript.

Looks like the guys at bootswatch.com do a nice job of customizing inputs with pure CSS. I simplified their approach on the fiddle here:
https://jsfiddle.net/mthomas9261/qujbq4gs/
HTML:
<input type="radio" value="a" name="test">
<input type="radio" value="b" name="test">
<input type="checkbox" name="a" value="a">
<input type="checkbox" name="b" value="b">
CSS:
/*****************************************************
****************** Radio *****************
******************************************************/
/* Hide Original Radio Button */
input[type="radio"] {
position: relative;
margin-top: 6px;
margin-right: 4px;
vertical-align: top;
border: none;
background-color: transparent;
-webkit-appearance: none;
appearance: none;
cursor: pointer;
}
/* Remove standard blue selection outline */
input[type="radio"]:focus {
outline: none;
}
/* New radio button */
input[type="radio"]:before,
input[type="radio"]:after {
content: "";
display: block;
width: 14px;
height: 14px;
border-radius: 50%;
-webkit-transition: 240ms;
-o-transition: 240ms;
transition: 240ms;
}
input[type="radio"]:before {
position: absolute;
left: 1px;
top: -2px;
background-color: red;
-webkit-transform: scale(0);
-ms-transform: scale(0);
-o-transform: scale(0);
transform: scale(0);
}
input[type="radio"]:after {
position: relative;
top: -3px;
border: 1px solid #E0E0E0;
}
/* New radio checked */
input[type="radio"]:checked:before {
-webkit-transform: scale(0.5);
-ms-transform: scale(0.5);
-o-transform: scale(0.5);
transform: scale(0.5);
}
input[type="radio"]:checked:after {
border-color: #E0E0E0;
}
/* New radio disabled */
input[type="radio"]:disabled:after,
input[type="radio"]:disabled:checked:after {
border-color: #F1F1F1;
}
input[type="radio"]:disabled:checked:before {
background-color: #F1F1F1;
}
/*****************************************************
**************** Checkbox ****************
******************************************************/
/* Hide Original Checkbox */
input[type="checkbox"] {
position: relative;
border: none;
margin-bottom: -4px;
-webkit-appearance: none;
appearance: none;
cursor: pointer;
}
/* Display New Checkox */
input[type="checkbox"] {
position: relative;
border: none;
margin-bottom: -4px;
-webkit-appearance: none;
appearance: none;
cursor: pointer;
}
input[type="checkbox"]:focus,
.checkbox input[type="checkbox"]:focus,
.checkbox-inline input[type="checkbox"]:focus {
outline: none;
}
input[type="checkbox"]:focus:after,
.checkbox input[type="checkbox"]:focus:after,
.checkbox-inline input[type="checkbox"]:focus:after {
border-color: #E0E0E0;
}
input[type="checkbox"]:after,
.checkbox input[type="checkbox"]:after,
.checkbox-inline input[type="checkbox"]:after {
content: "";
display: block;
width: 14px;
height: 14px;
margin-top: -2px;
margin-right: 5px;
border: 1px solid #E0E0E0;
border-radius: 1px;
-webkit-transition: 240ms;
-o-transition: 240ms;
transition: 240ms;
}
/* Checkbox Checked */
input[type="checkbox"]:checked:before {
content: "";
position: absolute;
top: -2px;
left: 5px;
display: table;
width: 4px;
height: 9px;
border: 3px solid red;
border-top-width: 0;
border-left-width: 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
}
input[type="checkbox"]:checked:after {
border-color: #E0E0E0;
}
/* Checkbox Disabled */
input[type="checkbox"]:disabled:after {
border-color: #F1F1F1;
}
input[type="checkbox"]:disabled:checked:after {
background-color: #F1F1F1;
border-color: transparent;
}

Related

CSS3 problem with animation when unchecking a checkbox

I'm trying to recreate my form's checkboxes by CSS3.
I animated the mark (checkbox) correctly, but when I uncheck the checkbox I can't start the animation in reverse mode. I reproduced my example.
/*
==================
INPUT CHECKBOX
==================
*/
.checkbox-label>input {
display: none;
outline: none;
}
/*
==============
LABEL CHECKBOX
==============
*/
.checkbox-label {
display: block;
position: relative;
font-size: 0.95em;
margin-bottom: 10px;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
outline: none;
}
.checkbox-label:hover>.checkbox-button {
box-shadow: 0px 0px 0px 5px hsla(255, 100%, 60%, 0.1);
background-color: hsla(255, 100%, 60%, 0.1);
}
.checkbox-label:hover>.checkbox-button {
border: 2px solid #808080;
}
/*
======================
MARK CHECKBOX - AFTER
======================
*/
.checkbox-label>input[type="checkbox"]~.checkbox-button::after {
content: "";
position: absolute;
top: 8px;
left: 3px;
border-right: 2px solid transparent;
border-bottom: 2px solid transparent;
transform: rotate(45deg);
transform-origin: left bottom;
}
/*
================================
MARK CHECKBOX - AFTER - CHECKED
================================
*/
.checkbox-label>input[type="checkbox"]:checked~.checkbox-button::after {
animation: checkbox-check 0.3s cubic-bezier(0.5, 0, 0.23, 1);
animation-fill-mode: forwards;
animation-direction: normal;
}
.checkbox-label:not(:active)>input[type="checkbox"]:not(:checked)~.checkbox-button::after {
animation-direction: reverse;
}
/*
========
CHECKBOX
========
*/
.checkbox-button {
vertical-align: sub;
}
.checkbox-button {
position: relative;
top: 0px;
left: 0px;
color: #808080;
height: 20px;
width: 20px;
background-color: transparent;
border: 2px solid #ccc;
border-radius: 5px;
display: inline-block;
transition: background-color 0.4s ease;
}
.checkbox-label>input:checked~.checkbox-button {
background-color: transparent;
border: 2px solid #6633ff;
background-color: #6633ff;
}
.checkbox-label>input~.checkbox-button::before {
content: "";
position: absolute;
left: -8px;
top: -8px;
width: 35px;
height: 35px;
background-color: #6633ff;
border-radius: 30%;
opacity: 0.6;
transform: scale(0);
}
.checkbox-label>input:checked~.checkbox-button::before {
animation: ripple 0.9s ease-out;
}
/*
==========
ANIMATION
==========
*/
#keyframes ripple {
0% {
transform: scale(0);
}
50% {
transform: scale(1);
}
100% {
opacity: 0;
transform: scale(1);
}
}
#keyframes checkbox-check {
0% {
width: 0px;
height: 0px;
border-color: white;
transform: translate3d(0, 0, 0) rotate(45deg);
}
33% {
width: 5px;
height: 0px;
transform: translate3d(0, 0px, 0) rotate(45deg);
}
100% {
width: 5px;
height: 11px;
border-color: white;
transform: translate3d(0, -11px, 0) rotate(45deg);
}
}
<form action="" method="post" class="form">
<h1>Custom Checkboxe</h1>
<label class="checkbox-label" data-label="One">
<input type="checkbox" checked="checked" />
<span class="checkbox-button"></span>
One
</label>
</form>
Let me know what I'm doing wrong and how to improve to make my animation work properly.

Change the color of the checkbox's check mark in css [duplicate]

This question already has answers here:
How to style a checkbox using CSS
(43 answers)
Closed 5 years ago.
How can I change the color of the check mark using css?
<label style="color: #fff;">
<input type="checkbox" name="remember" value="1" class="remember">
<span></span>
</label>
I think this is what you want (CSS only):
HTML:
<input type="checkbox" name="genres" value="adventure" id="adventure_id">
<label for="adventure_id" style="font-family: 'SExtralight'; font-size:14px;">Adventure</label>
CSS:
input[type="checkbox"]:checked + label::after {
content: '';
position: absolute;
width: 1.2ex;
height: 0.4ex;
background: rgba(0, 0, 0, 0);
top: 0.5ex;
left: 0.6ex;
border: 3px solid red;
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);
}
input[type="checkbox"] {
line-height: 2.1ex;
}
input[type="radio"],
input[type="checkbox"] {
position: absolute;
left: -999em;
}
input[type="checkbox"] + label {
position: relative;
overflow: hidden;
cursor: pointer;
}
input[type="checkbox"] + label::before {
content: "";
display: inline-block;
vertical-align: -25%;
height: 3ex;
width: 3ex;
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;
}

Issue with pure-CSS fancy checkbox

I am trying to come up with a pure-css fancy checkbox. And I have succeeded to achieve my goal by 90%, with the only exception that I see the irritating standard chechbox appear from under my fancy checkbox. And I can't get rid of it. Any help ?
.checkboxFive {
width: 12px;
margin: 12px 100px;
position: relative;
}
.checkboxFive label {
cursor: pointer;
position: absolute;
width: 12px;
height: 12px;
top: 0;
left: 0;
background: #eee;
border: 1px solid #ddd;
}
.checkboxFive label:after {
opacity: 0;
content: '';
position: absolute;
width: 11px;
height: 4px;
background: transparent;
top: 1px;
left: 3px;
border: 2px solid #333;
border-top: none;
border-right: none;
transform: rotate(-45deg);
}
.checkboxFive label:hover::after {
opacity: 0.5;
}
.checkboxFive input[type=checkbox]:checked + label:after {
opacity: 1;
}
<div class="checkboxFive">
<input type="checkbox" value="1" id="checkboxFiveInput" name="" />
<label for="checkboxFiveInput"></label>
</div>
you can remove it from screen with position and coordonates.
(display or visibility might become an accessibility issue)
.checkboxFive {
width: 12px;
margin: 12px 100px;
position: relative;
}
.checkboxFive label {
cursor: pointer;
position: absolute;
width: 12px;
height: 12px;
top: 0;
left: 0;
background: #eee;
border: 1px solid #ddd;
}
.checkboxFive label:after {
opacity: 0;
content: '';
position: absolute;
width: 11px;
height: 4px;
background: transparent;
top: 1px;
left: 3px;
border: 2px solid #333;
border-top: none;
border-right: none;
transform: rotate(-45deg);
}
.checkboxFive label:hover::after {
opacity: 0.5;
}
#checkboxFiveInput {
position:absolute;
left:-9999px;
}
.checkboxFive input[type=checkbox]:checked + label:after {
opacity: 1;
}
<div class="checkboxFive">
<input type="checkbox" value="1" id="checkboxFiveInput" name="" />
<label for="checkboxFiveInput"></label>
</div>
Add a display:none to checkboxFiveInput.
.checkboxFive {
width: 12px;
margin: 12px 100px;
position: relative;
}
.checkboxFive input {
display:none;
}
.checkboxFive label {
cursor: pointer;
position: absolute;
width: 12px;
height: 12px;
top: 0;
left: 0;
background: #eee;
border: 1px solid #ddd;
}
.checkboxFive label:after {
opacity: 0;
content: '';
position: absolute;
width: 11px;
height: 4px;
background: transparent;
top: 1px;
left: 3px;
border: 2px solid #333;
border-top: none;
border-right: none;
transform: rotate(-45deg);
}
.checkboxFive label:hover::after {
opacity: 0.5;
}
.checkboxFive input[type=checkbox]:checked + label:after {
opacity: 1;
}
<div class="checkboxFive">
<input type="checkbox" value="1" id="checkboxFiveInput" name="" />
<label for="checkboxFiveInput"></label>
</div>
The first step in creating a custom checkbox is to add visibility: hidden to your input element. In your case, your css would look like this:
.checkboxFive #checkboxFiveInput {
visibility: hidden;
}
You can see this link for more information about building custom checkboxes.

css checkbox style for border color [duplicate]

This question already has answers here:
How to change checkbox's border style in CSS?
(15 answers)
Closed 6 years ago.
I have a check box in my table.
this is css of that checkbox
input[type="checkbox"] {
width: 20px;
height: 30px;
margin: auto;
display: table-row;
border: 5px solid red;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
margin-left: 4px;
margin-top: 1px;
}
but it shows normal checkbox.
I want to change the border color of that checkbox.
but it doesn't work!!!
You can put only below css for checkbox border and see Fiddle Demo
CSS:
.error input[type=checkbox] {
outline: 2px solid #c00;
}
Try this customized the checkbox code:
/* Remove default checkbox */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
position: relative;
overflow: hidden;
padding-left: 35px;
cursor: pointer;
display: inline-block;
height: 25px;
line-height: 25px;
-webkit-user-select: none; /* webkit (safari, chrome) browsers */
-moz-user-select: none; /* mozilla browsers */
-khtml-user-select: none; /* webkit (konqueror) browsers */
-ms-user-select: none; /* IE10+ */
}
/* checkbox aspect */
[type="checkbox"] + label:before,
[type="checkbox"] + label:after {
content: '';
position: absolute;
left: 0;
z-index: 1;
-webkit-transition: .2s;
transition: .2s;
}
/* Unchecked styles */
[type="checkbox"]:not(:checked) + label:before {
top: 0px;
width: 19px; height: 19px;
border: 1px solid red;
}
[type="checkbox"]:not(:checked) + label:after {
top: 0px;
width: 19px; height: 19px;
border: 1px solid red;
z-index: 0;
}
/* Checked styles */
[type="checkbox"]:checked + label:before {
top: 2px;
width: 6px; height: 12px;
border-top: 1px solid transparent;
border-left: 1px solid transparent;
border-right: 1px solid red;
border-bottom: 1px solid red;
-webkit-transform: rotateZ(37deg);
transform: rotateZ(37deg);
-webkit-transform-origin: 20% 40%;
transform-origin: 100% 100%;
}
[type="checkbox"]:checked + label:after {
top: 0px;
width: 19px; height: 19px;
border: 1px solid red;
z-index: 0;
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before {
top: 0;
box-shadow: none;
background-color: #444;
width: 19px; height: 19px;
border: 3px solid #444;
-webkit-transform: rotateZ(0deg);
transform: rotateZ(0deg);
}
[type="checkbox"]:disabled + label {
color: #555;
}
[type="checkbox"]:disabled:not(:checked) + label:hover:before {
border-color: red;
}
<form action="#">
<p>
<input type="checkbox" id="test1" />
<label for="test1">Red</label>
</p>
<p>
<input type="checkbox" id="test2" checked="checked" />
<label for="test2">Yellow</label>
</p>
</form>
I guess HTML does not allow adding border styles to check boxes. Try using the outline property instead.

Browser Support for Custom CSS3 Radio Buttons

So, I got my custom CSS3 Radio Buttons to work and they look great on my site. They're working in Firefox, and from what I've read, they should work in most modern browsers. However, on my phone (a Galaxy Nexus with Android 4.3), while they work perfectly in Chrome, they do not work on the default vanilla browser (not able to be clicked). I suspect there's also most likely problems in various versions of Internet Explorer (as there always seem to be).
I found a great article about browser support for custom radio buttons, but between all the marked out text and various updates, I can't figure out what exactly the problem is with my buttons & what the simplest solution is.
It seems to me that others might experience the same problem, so I've included my code in hopes that you can help me get to the bottom of this, and that this topic might point others in the right direction in the future. Thank you for your time.
Here's my code:
input[type=radio] {
display:none;
}
input[type=radio] + label:before {
content: "";
display: inline-block;
width: 15px;
height: 15px;
vertical-align:middle;
margin-right: 8px;
background-color: #aaa;
border-radius: 8px;
margin-top: -3.5px;
margin-left: 5%;
}
input[type=radio]:checked + label:before {
content: ""; /* just change color, no bullet */
background-color: #24bbff;
text-align:center;
line-height:14px;
}
I make this pen a while ago and works on android. http://codepen.io/karlprieb/pen/kKjCn
Edit the css for your design and test:
/* RADIO BUTTONS */
[type="radio"] {
visibility: hidden;
}
/* Radio button style */
[type="radio"]:not(:checked) + label,
[type="radio"]:checked + label {
position: relative;
padding-left: 20px;
margin-left: -20px;
cursor: pointer;
}
[type="radio"]:not(:checked) + label:before,
[type="radio"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 0 ;
width: 13px; height: 13px;
border: 2px solid #cacaca;
background: #fff;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
}
/* Checked style */
[type="radio"]:not(:checked) + label:after,
[type="radio"]:checked + label:after {
content: '';
position: absolute;
top: 0; left: 0;
width: 17px;
height: 17px;
background: #82cdf5;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
transition: all .2s;
}
/* checked mark aspect changes */
[type="radio"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
[type="radio"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
/* Label hover style */
label:hover:before {
border: 2px solid #82cdf5!important;
}
Custom check box
// Check box
.check-large[type="checkbox"]:checked::-ms-check {
border: 2px solid #aaa;
color: #aaa;
opacity: 1;
font-size: larger;
}
.check-large[type="checkbox"] {
/* remove standard background appearance */
-webkit-appearance: none;
-moz-appearance: none;
/* create custom checkbox appearance */
display: inline-block;
width: 22px;
height: 22px;
padding: 4px;
/* background-color only for content */
background-clip: content-box;
border: 2px solid #000000;
opacity: 0.4;
vertical-align: bottom;
}
/* appearance for checked Checkbox */
.check-large[type="checkbox"]:checked , .check-large[type="checkbox"]:disabled:checked{
background-color: #aaa;
border: 2px solid #aaa;
opacity: 1;
}
<input type="checkbox" class="check-large">
<input type="checkbox" class="check-large">
// Radio Button
.radio-large[type="radio"]:checked::-ms-check {
border: 2px solid #aaa;
color: #aaa;
opacity: 1;
font-size: larger;
}
.radio-large[type="radio"] {
/* remove standard background appearance */
-webkit-appearance: none;
-moz-appearance: none;
/* create custom radiobutton appearance */
display: inline-block;
width: 22px;
height: 22px;
padding: 4px;
/* background-color only for content */
background-clip: content-box;
border: 2px solid #000000;
opacity: 0.4;
vertical-align: bottom;
border-radius:50%;
}
/* appearance for checked Radio button */
.radio-large[type="radio"]:checked , .radio-large[type="radio"]:disabled:checked{
background-color: #aaa;
border: 2px solid #aaa;
opacity: 1;
}
<input type="radio" name="radio-check" selected='selected' class="radio-large" >
<input type="radio" name="radio-check" class="radio-large" >
This can be used for the custom radio and checkbox creation.

Resources