Custom style checkbox using CSS (no bootstrap) [duplicate] - css

This question already has answers here:
How to style a checkbox using CSS
(43 answers)
Closed 7 months ago.
How do you style checkbox using only CSS? I want checkboxes with a background color of my choice, and appear a cross mark when they are checked, instead of a checkmark.

/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
position: relative;
padding-left: 1.95em;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left: 0; top: 0;
width: 1.25em; height: 1.25em;
border: 2px solid #ccc;
background: #fff;
border-radius: 4px;
box-shadow: inset 0 1px 3px rgba(0,0,0,.1);
}
/* checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: .1em; left: .3em;
font-size: 1.3em;
line-height: 0.8;
color: #09ad7e;
transition: all .2s;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before {
box-shadow: none;
border-color: #bbb;
background-color: #ddd;
}
[type="checkbox"]:disabled:checked + label:after {
color: #999;
}
[type="checkbox"]:disabled + label {
color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus + label:before,
[type="checkbox"]:not(:checked):focus + label:before {
border: 2px dotted blue;
}
/* hover style just for information */
label:hover:before {
border: 2px solid #4778d9!important;
}
body {
font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
color: #777;
}
h1, h2 {
margin-bottom: .25em;
font-weight: normal;
text-align: center;
}
h2 {
margin: .25em 0 2em;
color: #aaa;
}
form {
width: 7em;
margin: 0 auto;
}
.txtcenter {
margin-top: 4em;
font-size: .9em;
text-align: center;
color: #aaa;
}
.copy {
margin-top: 2em;
}
.copy a {
text-decoration: none;
color: #4778d9;
}
<form action="#">
<p>
<input type="checkbox" id="test1" />
<label for="test1">India</label>
</p>
<p>
<input type="checkbox" id="test2" checked="checked" />
<label for="test2">USA</label>
</p>
<p>
<input type="checkbox" id="test3" checked="checked" />
<label for="test3">Japan</label>
</p>
</form>

<div class="checkbox">
<input name="filter_price0" id="offer_407" type="checkbox" value="407" hidden >
<label for="offer_407">
Below 500 Rs
</label>
</div>
<style>
.checkbox input[type="checkbox"]:checked + label:before {
background-color: #424242;
border-color: #424242;
content: "\2713";
display: inline-block;
text-align: center;
color:#fff;
}
li{
list-style-type:none;
margin-top:10px;
}
.checkbox label{
cursor:pointer;
}
.checkbox label:before {
content: "";
cursor:pointer;
display: inline-block;
position: absolute;
width: 20px;
height: 20px;
left: 0;
border: 1px solid #cccccc;
background-color: #fff;
-webkit-transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
-o-transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
}
</style>
for demo you can click here fiddle demo, and next time post some code which you tryed

Had the same question - found a nice solution - check out answers in Stackoverflow - no one has this one nice trick.
Here it is
input[type=checkbox].agb {
all: unset;
/* rest of styles here */
}
It sets all Browser generated css styles to none and you are able to style the checkbox from scratch like you want to have it without any label hacks or something.
Cool?
I'll give you an example of a simple custom checkbox of mine.
input[type=checkbox].agb {
all: unset;
width: 32px;
height: 32px;
margin-right: 5px;
display: block;
background: var(--color-2);
float: left;
}
input[type=checkbox].agb:checked {
background: var(--color-4);
color: var(--color-2)
}
input[type=checkbox].agb:checked::after {
content: "✔";
display: block;
font-size: 26px;
padding-left: 5px;
}

Related

Any workaround for the CSS var?

This is a followup for a solved question :
transition background of pseudo element
The idea is to add a transition to the background color which appears when we do a hover, this following code works on IE11, but without the transition:
input[type=checkbox] {
appearance: none;
}
input[type=checkbox] + label {
margin: 0;
padding: 0;
user-select: none;
}
input[type=checkbox] + label::before {
content: url("https://svgshare.com/i/Qo7.svg");
cursor: pointer;
color: transparent;
background: none;
border: none;
border-radius: 50%;
height: 40px;
width: 40px;
display: flex;
justify-content: center;
align-items: center;
}
input[type=checkbox]:checked + label:before {
content: url("https://svgshare.com/i/Qmp.svg");
background: none;
border: none;
}
input[type=checkbox]:hover + label:before {
background: #D9DEEA;
}
<input type="checkbox" id="checkbox1"/>
<label for="checkbox1"></label>
However, this code doesn't work on IE11, since it doesn't support var:
input[type=checkbox] {
appearance: none;
}
input[type=checkbox] + label {
margin: 0;
padding: 0;
user-select: none;
}
input[type=checkbox] + label::before {
content: url("https://svgshare.com/i/Qo7.svg");
cursor: pointer;
color: transparent;
background: none;
border: none;
border-radius: 50%;
height: 40px;
width: 40px;
display: flex;
justify-content: center;
align-items: center;
background: radial-gradient(farthest-side,#D9DEEA 99%,transparent) center/ var(--s,0px 0px);
background-repeat:no-repeat;
transition:0.5s;
}
input[type=checkbox]:checked + label:before {
content: url("https://svgshare.com/i/Qmp.svg");
background: none;
border: none;
}
input[type=checkbox]:hover + label:before {
--s:100% 100%;
}
<input type="checkbox" id="checkbox1"/>
<label for="checkbox1"></label>
I have updated the code from the solution of my previous question, so it can work with IE11, but I couldn't figure out how to do a workaround for var in IE11.
Edit
Thanks to #Temani Afif answer, I managed to do the transition without the var and using checkbox with input (since pseudo element are not meant to be used on replaced elements such as form elements (inputs)):
input[type=checkbox] {
appearance: none;
}
input[type=checkbox] + label {
margin: 0;
padding: 0;
user-select: none;
}
input[type=checkbox] + label::before {
content: url("https://svgshare.com/i/Qo7.svg");
cursor: pointer;
color: transparent;
background: none;
border: none;
border-radius: 50%;
height: 40px;
width: 40px;
display: flex;
justify-content: center;
align-items: center;
background: radial-gradient(farthest-side,#D9DEEA 99%,transparent) center/ 0px 0px;
background-repeat:no-repeat;
transition:0.5s;
}
input[type=checkbox]:checked + label:before {
content: url("https://svgshare.com/i/Qmp.svg");
border: none;
}
input[type=checkbox]:hover + label:before {
background-size: 40px 40px;
}
<input type="checkbox" id="checkbox1"/>
<label for="checkbox1"></label>
But in IE11 the background just appears instantly without the transition.
No CSS variables solution
.container {
position: relative;
height: 100px;
width: 300px;
padding: 24px;
box-sizing: border-box;
color: black;
border: 2px solid black;
}
input[type="checkbox"] {
height: 40px;
width: 40px;
position: absolute;
top: 12px;
right: 12px;
appearance: none;
outline: none;
cursor: pointer;
border: none;
background:
url("https://svgshare.com/i/Qo7.svg") calc(50% + 1px) 50% /60% 60%,
radial-gradient(farthest-side,#D9DEEA 99%,transparent) center/ 0px 0px;
background-repeat:no-repeat;
transition:0.5s;
}
input[type="checkbox"]:hover {
background-size:
60% 60%,
100% 100%;
}
input[type="checkbox"]:checked {
background-image:
url("https://svgshare.com/i/Qmp.svg"),
radial-gradient(farthest-side,#D9DEEA 99%,transparent);
}
<div class="container">
<input type="checkbox" >
</div>

Styling checkbox in contact-form-7 wordpress

Im styling 3 checkboxes in contact-form-7
this is the code of contact
<div class="form-group checkbox">
[checkbox participated_2016 default:1 "I Participated at Mercy Ships Cargo Day 2016"]
</div>
<div class="form-group checkbox">
[checkbox want_to_participate default:1 "I Want to participate to Mercy Ships Cargo Day 2017"]
</div>
<div class="form-group checkbox">
[checkbox join_mailing default:1 "Join Cargo Day mailing list"]
</div>
and i putted this css to style it
.checkbox .wpcf7-list-item span {
display: inline-block;
position: relative;
padding-left: 15px;
}
.checkbox .wpcf7-list-item span::before {
content: "";
display: inline-block;
position: absolute;
width: 17px;
height: 17px;
left: 0;
margin-left: -20px;
border: 1px solid #cccccc;
border-radius: 3px;
background-color: #fff;
-webkit-transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
-o-transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
}
.checkbox .wpcf7-list-item span::after {
display: inline-block;
position: absolute;
width: 16px;
height: 16px;
left: 0;
top: 0;
margin-left: -20px;
padding-left: 3px;
padding-top: 1px;
font-size: 11px;
color: #0cf !important;
}
.checkbox .wpcf7-list-item input[type="checkbox"] {
opacity: 0;
}
.checkbox .wpcf7-list-item input[type="checkbox"]:focus + span::before {
outline: thin dotted;
outline: 5px auto -webkit-focus-ring-color;
outline-offset: -2px;
}
.checkbox .wpcf7-list-item input[type="checkbox"]:checked + span::after {
font-family: 'FontAwesome';
content: "\f00c";
color: #0cf;
}
.checkbox .wpcf7-list-item input[type="checkbox"]:disabled + span {
opacity: 0.65;
}
.checkbox .wpcf7-list-item input[type="checkbox"]:disabled + span::before {
background-color: #eeeeee;
cursor: not-allowed;
}
.checkbox.checkbox-circle span::before {
border-radius: 50%;
}
.checkbox.checkbox-inline {
margin-top: 0;
}
.checkbox-primary input[type="checkbox"]:checked + span::before {
background-color: #428bca;
border-color: #428bca;
}
.checkbox-primary input[type="checkbox"]:checked + span::after {
color: #fff;
}
.checkbox .wpcf7-list-item input:focus, .checkbox input:active:focus, .checkbox input.active:focus {
outline: none;
box-shadow: none;
}
but when i do this styling whatever checkbox i click on, all three are selected and deselected in same time.
Does anyone know why?

How to color the border of checkbox in css [duplicate]

This question already has answers here:
How to style a checkbox using CSS
(43 answers)
Closed 6 years ago.
I want to color the border of checkbox. I have written the below css -
input[type=checkbox] {
height: 15px;
width: 15px;
border: 1px solid #007dc6;
-webkit-appearance: none;
}
This works for chrome only. I don't want to write browser specific code in css. The css should apply to all latest browsers.
Manipulating checkbox appearance is possible by hiding the default checkbox and styling our own checkbox using the :before element and :checked , not(:checked) selectors
An Example:
body {
font-family: 'segoe ui';
background-color: white;
padding: 10px;
}
.space {
height: 10px;
}
.checkbox * {
box-sizing: border-box;
position: relative;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.checkbox {
display: inline-block;
}
.checkbox > input {
display: none;
}
.checkbox > label {
vertical-align: top;
font-size: 18px;
padding-left: 30px;
}
.checkbox > [type="checkbox"] + label:before {
color: #777;
content: '';
position: absolute;
left: 0px;
display: inline-block;
min-height: 20px;
height: 20px;
width: 20px;
border: 2px solid #777;
font-size: 15px;
vertical-align: top;
text-align: center;
transition: all 0.2s ease-in;
content: '';
}
.checkbox.radio-square > [type="checkbox"] + label:before {
border-radius: 0px;
}
.checkbox.radio-rounded > [type="checkbox"] + label:before {
border-radius: 25%;
}
.checkbox.radio-blue > [type="checkbox"] + label:before {
border: 2px solid #ccc;
}
.checkbox > [type="checkbox"] + label:hover:before {
border-color: lightgreen;
}
.checkbox > [type="checkbox"]:checked + label:before {
width: 8px;
height: 16px;
border-top: transparent;
border-left: transparent;
border-color: green;
border-width: 4px;
transform: rotate(45deg);
top: -4px;
left: 4px;
}
<div class="checkbox">
<input id="chkTest" type="checkbox" />
<label for="chkTest">Task one</label>
<div class="space">
</div>
<input id="chkTest1" type="checkbox" />
<label for="chkTest1">Task two</label>
</div>
Fiddle: https://jsfiddle.net/jo_Geek/jc81o4x0/

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.

How to override BootStrap CSS checkboxs and input checkboxs

I am having a problem over-riding the bootstrap css for checkboxes and input checkboxes. I have a checkboxlist and the bootstraps styling is over-riding my own css file. I have doubled checked and made sure that it wasn't bootstraps themes causing the issue but it is the bootstraps css.
I have also tried using !important
in my css and it still gets over-ridden, I even placed my css file after the bootstraps css, tried putting my css before it, even tried
input[type=checkbox]
and that did nothing as well.
Here is the css I'm trying to use to override the bootstrap
.CheckBoxSubjects td {
border-style: solid;
border: 1px solid #78E389;
width: 122px;
height: 27px;
border-radius: 5px;
background-color:#78E389;
}
Here's the fiddle of the code I used. Hope this helps for you.
http://jsfiddle.net/cLaty/
HTML
<div class="checkbox pull-right">
<input type="checkbox" id="remember" /><label for="remember">Remember me</label>
</div>
CSS
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label {
position: relative;
padding-left: 25px;
cursor: pointer;
}
/* checkbox aspect */
input[type="checkbox"]:not(:checked) + label:before,
input[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 2px;
width: 17px; height: 17px;
border: 1px solid #aaa;
background: #f8f8f8;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
input[type="checkbox"]:not(:checked) + label:after,
input[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 0; left: 4px;
font-size: 14px;
color: #4cc0c1;
transition: all .2s;
-webkit-transition: all .2s;
-moz-transition: all .2s;
-ms-transition: all .2s;
-o-transition: all .2s;
}
/* checked mark aspect changes */
input[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
input[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
/* disabled checkbox */
input[type="checkbox"]:disabled:not(:checked) + label:before,
input[type="checkbox"]:disabled:checked + label:before {
box-shadow: none;
border-color: #999999;
background-color: #ddd;
}
input[type="checkbox"]:disabled:checked + label:after {
color: #999;
}
input[type="checkbox"]:disabled + label {
color: #aaa;
}
/* accessibility */
input[type="checkbox"]:checked:focus + label:before,
input[type="checkbox"]:not(:checked):focus + label:before {
border: inherit;
}

Resources