How to make a custom checkbox and radio button in pure css? - html

I am trying to make a custom check box and radio button in pure css with cross browser compatible.
I am lookinga result like mentioned below image:-
I want to this only pure css with ie7

Try This:
DEMO
HTML
<div class="checkbox">
<label class="i-checks">
<input type="checkbox" value=""> <i></i> Option one
</label>
</div>
<div class="radio">
<label class="i-checks">
<input type="radio" checked="" value="option2" name="a"> <i></i> Option two checked
</label>
</div>
CSS
.i-checks {
padding-left: 20px;
cursor: pointer;
}
.i-checks input {
opacity: 0;
position: absolute;
margin-left: -20px;
}
.i-checks input:checked + i {
border-color: #23b7e5;
}
.i-checks input:checked + i:before {
left: 4px;
top: 4px;
width: 10px;
height: 10px;
background-color: #23b7e5;
}
.i-checks input:checked + span .active {
display: inherit;
}
.i-checks input[type="radio"] + i, .i-checks input[type="radio"] + i:before {
border-radius: 50%;
}
.i-checks input[disabled] + i, fieldset[disabled] .i-checks input + i {
border-color: #dee5e7;
}
.i-checks input[disabled] + i:before, fieldset[disabled] .i-checks input + i:before {
background-color: #dee5e7;
}
.i-checks > i {
width: 20px;
height: 20px;
line-height: 1;
border: 1px solid #cfdadd;
background-color: #fff;
margin-left: -20px;
margin-top: -2px;
display: inline-block;
vertical-align: middle;
margin-right: 4px;
position: relative;
}
.i-checks > i:before {
content: "";
position: absolute;
left: 10px;
top: 10px;
width: 0px;
height: 0px;
background-color: transparent;
-webkit-transition: all 0.2s;
transition: all 0.2s;
}
.i-checks > span {
margin-left: -20px;
}
.i-checks > span .active {
display: none;
}
OR - follow the this URL: http://fronteed.com/iCheck/

I have made the css based check box & radio buttons is it near by results ?
http://jsfiddle.net/Vsvg2/78/

Related

How to style checkbox where check box and label in separate div sections

I have created separate div sections for checkbox and label and I want style checkbox. When use only checkbox and label it will work properly, but When add div sections it won't work. I tried solutions in this forum but those solutions are not working please anyone can check and help me on this
How to Checkbox and label style only with CSS
Forms, separate div for labels and inputs
.checkbox input[type=checkbox] + .checkmark label {
display: block;
margin: 0.2em;
cursor: pointer;
padding: 0.2em;
}
.checkbox input[type=checkbox] {
display: none;
}
.checkbox input[type=checkbox] + .checkmark label:before {
content: "\2714";
border: 0.1em solid #000;
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;
}
.checkbox input[type=checkbox] + .checkmark label:active:before {
transform: scale(0);
}
.checkbox input[type=checkbox]:checked + .checkmark label:before {
background-color: MediumSeaGreen;
border-color: MediumSeaGreen;
color: #fff;
}
.checkbox input[type=checkbox]:disabled + .checkmark label:before {
transform: scale(1);
border-color: #aaa;
}
.checkbox input[type=checkbox]:checked:disabled + .checkmark label:before {
transform: scale(1);
background-color: #bfb;
border-color: #bfb;
}
<div class="checkbox">
<input type="checkbox" id="fruit1" name="fruit-1" value="Apple">
</div>
<div>
<label for="fruit1" class="checklabel">Apple</label>
</div>
Typo error checkcon to checbox or Change Some HTML Position
.checkbox input[type=checkbox] + label {
display: block;
margin: 0.2em;
cursor: pointer;
padding: 0.2em;
}
.checkbox input[type=checkbox] {
display: none;
}
.checkbox input[type=checkbox] + label:before {
content: "\2714";
border: 0.1em solid #000;
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;
}
.checkbox input[type=checkbox] + label:active:before {
transform: scale(0);
}
.checkbox input[type=checkbox]:checked + label:before {
background-color: MediumSeaGreen;
border-color: MediumSeaGreen;
color: #fff;
}
.checkbox input[type=checkbox]:disabled + label:before {
transform: scale(1);
border-color: #aaa;
}
.checkbox input[type=checkbox]:checked:disabled + label:before {
transform: scale(1);
background-color: #bfb;
border-color: #bfb;
}
<div class="checkbox">
<input type="checkbox" id="fruit1" name="fruit-1" value="Apple">
<label for="fruit1" class="checklabel">Apple</label>
</div>
Try using the same class for the checkbox input
<input class="checkbox-input" type="checkbox" id="fruit1" name="fruit-1" value="Apple" />
$('input:checkbox').change(function(){
if($(this).is(":checked")) {
$(this).parent().addClass("selected");
} else {
$(this).parent().removeClass("selected");
}
});
.checkbox + .checkmark label {
display: block;
margin: 0.2em;
cursor: pointer;
padding: 0.2em;
}
.checkbox input[type=checkbox] {
display: none;
}
.checkbox + .checkmark label:before {
content: "\2714";
border: 0.1em solid #000;
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;
}
.checkbox + .checkmark label:active:before {
transform: scale(0);
}
.checkbox.selected + .checkmark label:before {
background-color: MediumSeaGreen;
border-color: MediumSeaGreen;
color: #fff;
}
.checkbox input[type=checkbox] + .checkmark label:active:before {
transform: scale(0);
}
.checkbox input[type=checkbox]:checked + .checkmark label:before {
background-color: MediumSeaGreen;
border-color: MediumSeaGreen;
color: #fff;
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<div class="checkbox">
<input type="checkbox" id="fruit1" name="fruit-1" value="Apple"> </div>
<div class="checkmark">
<label for="fruit1" class="checklabel">Apple</label>
</div>

How to make the radio buttons with tabs work in pure css?

I'm working on the radio buttons that display each option once clicked. For example, if I click on "By Location", I want to display a tab with the field in which I entered a field, and if clicked on "By Procedure", it display a different tab with different field.
However, I also wanted to make it responsive and the radio buttons stacked on top of each other in mobile version centered in middle but text-align left.
Here's what I did in my code:
.steps-box {
margin-top: 60px;
background-color: white;
border-radius: 5px;
padding: 30px 30px;
text-align: center; }
.steps-box h2 {
color: #002c4a; }
.steps-box2 {
background-color: white;
border-radius: 5px;
padding: 30px 30px; }
/*Schedule Options on Step #1 */
.schedule_options {
position: relative;
width: 90%;
margin: 0 auto;
padding: 35px 0;
z-index: 10; }
.schedule_tabs {
width: 100%;
margin: 0 auto; }
.schedule_options input[type="radio"] {
position: absolute;
opacity: 0;
z-index: -1; }
.schedule_options label {
position: relative;
display: inline-block;
margin-right: 10px;
margin-bottom: 10px;
padding-left: 33px;
padding-right: 10px;
line-height: 36px;
cursor: pointer;
color: #51a5ba; }
.schedule_options label i {
display: none; }
.schedule_options label::before {
content: " ";
position: absolute;
top: 4px;
left: 0;
display: block;
width: 24px;
height: 24px;
border: 2px solid #51a5ba;
border-radius: 4px;
z-index: -1; }
.schedule_options input[type="radio"] + label::before {
border-radius: 5px; }
/* Checked */
.schedule_options input[type="radio"]:checked + label {
padding-left: 10px;
padding-right: 15px;
color: #fff; }
.schedule_options input[type="radio"]:checked + label i {
display: inline-block;
padding-right: 10px; }
.schedule_options input[type="radio"]:checked + label::before {
top: 0;
width: 100%;
height: 100%;
background: #51a5ba; }
/* Transition */
.schedule_options label,
.schedule_options label::before {
-webkit-transition: .25s all ease;
-o-transition: .25s all ease;
transition: .25s all ease; }
.schedule_options section {
display: none;
padding: 20px 0 0; }
.schedule_tabs #rb1:checked ~ .schedule_options #content1,
.schedule_tabs #rb2:checked ~ .schedule_options #content2,
.schedule_tabs #rb3:checked ~ .schedule_options #content3 {
display: block; }
#media screen and (max-width: 480px) {
.schedule_options {
text-align: left; }
.schedule_tabs {
width: 170px;
margin: 0 auto; } }
<section>
<div class="container">
<div class="steps-box">
<h2>How do you want to schedule your appointment?</h2>
<div class="schedule_options">
<div class="schedule_tabs">
<input type="radio" name="rb" id="rb1">
<label for="rb1"><i class="fas fa-check"></i>By Location</label>
<input type="radio" name="rb" id="rb2">
<label for="rb2"><i class="fas fa-check"></i>By Availability</label>
<input type="radio" name="rb" id="rb3">
<label for="rb3"><i class="fas fa-check"></i>By Procedure</label>
</div>
<section id="content1">
<p>Content 1</p>
</section>
<section id="content2">
<p>Content 2</p>
</section>
<section id="content3">
<p>Content 3</p>
</section>
</div>
</div>
</div>
</section>
As you notice, without the <div class="schedule_tabs">, I can easily display the content of each tab. With this <div class="schedule_tabs"> in place, I am able to center the radio buttons in mobile version, but the content in each tab won't display.
How do I fix it?
This happens because adding the <div class="schedule_tabs"> means that the inputs and labels are no longer siblings.
Taken from Mozilla Developer Network:
The ~ combinator selects siblings. This means that the second element follows the first (though not necessarily immediately), and both share the same parent.
Syntax: A ~ B
Example: p ~ span will match all elements that follow a <p>.
Your only other options are the + which only selects adjacent siblings, > which only selects a direct child, and (space) which selects all children.
Therefore it doesn't seem this is possible in pure CSS. Although with this kind of DOM manipulation I would highly recommend using even very basic JavaScript.
I just finished reformatting my code. Here is my updated code that works
.schedule_options {
position: relative;
width: 99%;
margin: 0 auto;
padding: 35px 0;
z-index: 10;
}
.schedule_options input[type="radio"] {
position: absolute;
opacity: 0;
z-index: -1;
}
.schedule_options label {
position: relative;
display: inline-block;
padding-left: 33px;
/*padding-right: 10px;*/
line-height: 36px;
cursor: pointer;
color: $teal;
text-align: left;
width: /*160px*/ 160px;
margin: 0 auto 10px;
}
.schedule_options label i {
display: none;
}
.schedule_options label::before {
content: " ";
position: absolute;
top: 4px;
left: 0;
display: block;
width: 24px;
height: 24px;
border: 2px solid $teal;
border-radius: 4px;
z-index: -1;
}
.schedule_options input[type="radio"] + label::before {
border-radius: 5px;
}
/* Checked */
.schedule_options input[type="radio"]:checked + label {
color: #fff;
}
.schedule_options input[type="radio"]:checked + label i {
display: block;
position: absolute;
width: 24px;
height: 24px;
top: 10px;
left: 8px;
}
.schedule_options input[type="radio"]:checked + label::before {
top: 0;
width: /*100%*/ 160px;
height: 100%;
background: #51a5ba;
}
/* Transition */
.schedule_options label, .schedule_options label::before {
-webkit-transition: .25s all;
-o-transition: .25s all;
transition: .25s all;
}
.schedule_options section {
display: none;
padding: 20px 0 0;
}
.schedule_panels {
}
#rb1:checked ~ .schedule_panels #content1,
#rb2:checked ~ .schedule_panels #content2,
#rb3:checked ~ .schedule_panels #content3 {
display: block;
}
#media screen and (max-width: 768px) {
.schedule_options {
display: flex;
flex-direction: column;
}
}
<div class="schedule_options">
<input type="radio" name="rb" id="rb1" />
<label for="rb1"><i class="fas fa-check"></i>By Location</label>
<input type="radio" name="rb" id="rb2" />
<label for="rb2"><i class="fas fa-check"></i>By Availability</label>
<input type="radio" name="rb" id="rb3" />
<label for="rb3"><i class="fas fa-check"></i>By Procedure</label>
<div class="schedule_panels">
<section id="content1">
<p>Content 1</p>
</section>
<section id="content2">
<p>Content 2</p>
</section>
<section id="content3">
<p>Content 3</p>
</section>
</div>
</div>

How to center custom checkbox label under checkbox - CSS?

I'm working on modifying an existing project which has some custom CSS styling for checkboxes. Currently the checkbox label is sitting to the right of the checkbox and I would like to move the checkbox to sit above the label. How can I achieve this using the following code?
<input type="checkbox" id="box-<%= tmp %>">
<label for="box-<%= tmp %>"><%= question.choiceAnswer[tmp] %></label>
input[type="checkbox"] { display: none; }
input[type="checkbox"] + label {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 20px;
text-align: center;
font-weight: 500;
font-size: 0.85em;
color: #232A33;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
input[type="checkbox"] + label:last-child { margin-bottom: 0; }
input[type="checkbox"] + label:before {
content: '';
display: block;
width: 20px;
height: 20px;
border: 1px solid #232A33;
position: absolute;
left: 0;
top: 0;
opacity: .6;
-webkit-transition: all .12s, border-color .08s;
transition: all .12s, border-color .08s;
}
input[type="checkbox"]:checked + label:before {
width: 10px;
top: -5px;
left: 5px;
border-radius: 0;
opacity: 1;
border-top-color: transparent;
border-left-color: transparent;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
From my understanding, the typical checkbox is no hidden and new checkbox is added through CSS, but I'm not sure how to move the label to be below the checkbox since the seem to be a single piece. Can I achieve this by modifying the above code or am I better off starting from scratch?
Thanks in advance!
Remove the left padding for the label and the absolute positioning for the pseudo element :before
input[type="checkbox"] { display: none; }
input[type="checkbox"] + label {
position: relative;
margin-bottom: 20px;
text-align: center;
font-weight: 500;
font-size: 0.85em;
color: #232A33;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
input[type="checkbox"] + label:last-child { margin-bottom: 0; }
input[type="checkbox"] + label:before {
content: '';
display: block;
width: 20px;
height: 20px;
border: 1px solid #232A33;
opacity: .6;
-webkit-transition: all .12s, border-color .08s;
transition: all .12s, border-color .08s;
}
input[type="checkbox"]:checked + label:before {
width: 10px;
top: -5px;
left: 5px;
border-radius: 0;
opacity: 1;
border-top-color: transparent;
border-left-color: transparent;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
<input type="checkbox" id="box">
<label for="box">Label</label>

Label inside styled checkbox is moving, Why?

When I'm working on a checkbox, I have it styled the way I need it to be, but the label beside it keeps moving. When unchecked it aligns itself with the bottom of the box, but then when checked it moves to center align it to the box.
.check {
width: 100%;
font-weight: normal;
margin-bottom: 0px;
}
.check label {
content: " ";
width: 18px;
height: 18px;
border: 1px solid #dae2e6;
margin-right: 10px;
display: inline-block;
}
.check label::after {
font-size: 13px;
color: #8e989f;
}
.check input[type="checkbox"]:checked + label::after {
font-family: "Material Icons";
content: "\e5ca";
}
.check input[type="checkbox"] {
left: -9999px;
position: absolute;
}
.check input[type="checkbox"]:checked + label + div {
display: inline-block;
}
<label class="check">
<input type="checkbox" value="mandatory" name="checkbox1" id="check3">
<label for="check3"> </label>
Mark as Mandatory
</label>
Fiddle
.check {
width: 100%;
font-weight: normal;
margin-bottom: 0px;
}
.check label {
content: " ";
width: 18px;
height: 18px;
border: 1px solid #dae2e6;
margin-right: 10px;
display: inline-block;
float: left; /*MODIFICATION */
}
.check label::after {
font-size: 13px;
color: #8e989f;
}
.check input[type="checkbox"]:checked + label::after {
font-family: "Material Icons";
content: "\e5ca";
}
.check input[type="checkbox"] {
left: -9999px;
position: absolute;
}
.check input[type="checkbox"]:checked + label + div {
display: inline-block;
}
<label class="check">
<input type="checkbox" value="mandatory" name="checkbox1" id="check3">
<label for="check3"> </label>
Mark as Mandatory
</label>
Just add this float:left property.
.check label {
content: " ";
width: 18px;
height: 18px;
border: 1px solid #dae2e6;
margin-right: 10px;
display: inline-block;
float: left; /*MODIFICATION*/
}
Working fiddle

Radio button pseudo-element positioning problems in a container with a percentage width

I have the following radio buttons set up with a percentage-width container:
The circle in the middle has to be centred, of course.
I have found this to be quite tricky with pseudo-element positioning.
Depending on the browser's width, the circle is not always horizontally centred. Here is a gif of the radio buttons, while resizing the window:
Is it possible to keep this circle centred while maintaining a percentage-width container?
Here's my code:
HTML:
<div class="form-section gender">
<div class="label-col">
<label>Gender</label>
</div>
<div class="form-section">
<input type="radio" name="gender" id="male" value="male" />
<label for="male"><span>Male</span></label>
</div>
<div class="form-section">
<input type="radio" name="gender" id="female" value="female" />
<label for="female"><span>Female</span></label>
</div>
</div>
CSS (compiled from SCSS):
#charset "UTF-8";
.gender {
width: 80%;
margin: 0 auto;
}
.gender .form-section {
margin-top: 20px;
display: inline-block;
width: 70px;
}
.gender input[type=radio] {
display: none;
width: 30px;
}
.gender .form-section label {
display: inline-block;
cursor: pointer;
position: relative;
padding-left: 25px;
margin-right: 15px;
color: gray;
}
#keyframes fade-in {
from {
opacity: 0.7;
}
to {
opacity: 1;
}
}
.gender .form-section label:before {
content: "";
display: inline-block;
width: 16px;
height: 16px;
margin-right: 10px;
position: absolute;
left: 0;
bottom: 1px;
background-color: transparent;
border: 2px solid gray;
border-radius: 50%;
}
.gender input[type=radio]:checked + label:before {
font-family: Monaco, Menlo, Consolas, "Courier New", monospace;
content: "•";
color: blue;
font-size: 21px;
text-align: center;
line-height: 17px;
border: 2px solid blue;
animation: fade-in 1s;
}
.gender input[type=radio]:checked + label {
color: blue;
}
Codepen link: http://codepen.io/anon/pen/JXxvZW
I have used translate to position the inner dot to center the label and changed the label styling to add outer border
.gender {
width: 80%;
margin: 0 auto;
}
.gender .form-section {
margin-top: 20px;
display: inline-block;
width: 70px;
}
.gender input[type=radio] {
display: none;
width: 30px;
}
.gender .form-section label {
display: inline-block;
cursor: pointer;
position: relative;
width: 16px;
color: gray;
border: 2px solid grey;
border-radius: 50%;
height: 16px;
}
.gender .form-section label span {
margin-left: 25px;
}
#keyframes fade-in {
from {
opacity: 0.7
}
to {
opacity: 1
}
}
.gender .form-section label:before {
content: "";
}
.gender input[type=radio]:checked + label:before {
width: 8px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
height: 8px;
background: blue;
position: absolute;
border-radius: 50%;
animation: fade-in 1s;
}
.gender input[type=radio]:checked + label {
border-color: blue;
}
<div class="form-section gender">
<div class="label-col">
<label>Gender</label>
</div>
<div class="form-section">
<input type="radio" name="gender" id="male" value="male" />
<label for="male"><span>Male</span>
</label>
</div>
<div class="form-section">
<input type="radio" name="gender" id="female" value="female" />
<label for="female"><span>Female</span>
</label>
</div>
</div>
I have used both ::after and ::before to construct this. Kindly check thoroughly and see if it works.
input[type="radio"] {display: none;}
input[type="radio"] + label {
display: inline-block;
padding-left: 25px;
line-height: 25px;;
position: relative;
}
input[type="radio"] + label::after {
content: " ";
display: block;
position: absolute;
width: 16px;
height: 16px;
border: 2px solid #999;
left: 0;
top: 4px;
border-radius: 100%;
}
input[type="radio"]:checked + label::before {
content: " ";
display: block;
position: absolute;
width: 8px;
height: 8px;
border: 2px solid #999;
left: 4px;
background-color: #999;
top: 8px;
border-radius: 100%;
}
<div class="form-section gender">
<div class="label-col">
<label>Gender</label>
</div>
<div class="form-section">
<input type="radio" name="gender" id="male" value="male" />
<label for="male"><span>Male</span></label>
</div>
<div class="form-section">
<input type="radio" name="gender" id="female" value="female" />
<label for="female"><span>Female</span></label>
</div>
</div>
Preview
JSBin: http://jsbin.com/bapepahequ