Multiple Checkboxes, Only One Is Selectable - html

So I have these checkboxes on a page, but no matter which one I click on, only the first one gets the check mark. I have no idea what the problem is. At first I was having the problem that the checkbox wouldn't show up as checked, but now it's just the first one that is getting selected. There is a button on the page that selects all the boxes, and that works, it is just when a user tries to select one that it does't work.
Code
.checkbox {
margin: 3px auto;
width: 25px;
position: relative;
}
.checklabel {
cursor: pointer;
position: absolute;
width: 25px;
height: 25px;
top: 0;
left: 0;
background: #eee;
border:1px solid #ddd;
}
.checkbox .checklabel:after {
opacity: 0.2;
content: '';
position: absolute;
width: 9px;
height: 5px;
background: transparent;
top: 6px;
left: 7px;
border: 3px solid #333;
border-top: none;
border-right: none;
transform: rotate(-45deg);
}
.checklabel:hover::after {
opacity: .5;
}
.checkbox input[type=checkbox]:checked + .checklabel:after {
opacity: 1;
}
<td><div class="checkbox"><input class="check" id="check" type="checkbox"><label class="checklabel" for="check"></label></div></td>
<td><div class="checkbox"><input class="check" id="check" type="checkbox"><label class="checklabel" for="check"></label></div></td>

An id may only be used once per page, and the for attribute of your label needs to reference the unique id for the input you want it to control.
.checkbox {
margin: 3px auto;
width: 25px;
position: relative;
}
.checklabel {
cursor: pointer;
position: absolute;
width: 25px;
height: 25px;
top: 0;
left: 0;
background: #eee;
border: 1px solid #ddd;
}
.checkbox .checklabel:after {
opacity: 0.2;
content: '';
position: absolute;
width: 9px;
height: 5px;
background: transparent;
top: 6px;
left: 7px;
border: 3px solid #333;
border-top: none;
border-right: none;
transform: rotate(-45deg);
}
.checklabel:hover::after {
opacity: .5;
}
.checkbox input[type=checkbox]:checked + .checklabel:after {
opacity: 1;
}
<td>
<div class="checkbox"><input class="check" id="check" type="checkbox"><label class="checklabel" for="check"></label></div>
</td>
<td>
<div class="checkbox"><input class="check" id="check2" type="checkbox"><label class="checklabel" for="check2"></label></div>
</td>

Related

Change radio button outer border color and thickness

I am trying to change 2 radio buttons border color and border thickness but for some reason it is not doing anything.
Here is my code
input[type='radio']:after {
width: 10px;
height: 10px;
border-radius: 15px;
top: -5px;
left: 3px;
position: relative;
background-color: transparent;
content: '';
display: inline-block;
visibility: visible;
border: none;
}
input[type='radio']:checked:after {
width: 10px;
height: 10px;
border-radius: 15px;
top: -5px;
left: 2.7px;
position: relative;
background-color: #f07f09;
content: "";
display: inline-block;
visibility: visible;
border: none;
}
input[type=radio]:checked {
border-color: #bbbbbb;
border: solid 3px;
}
input[type=radio]:checked:before {
border-color: #bbbbbb;
border: solid 3px;
}
<input type="radio" name="contact-option" id="1" class="otp-text-gap" [(ngModel)]="contact" value="cell" (change)="changeOTPChannel()"><span class="otp-channel-text">Cell number</span>
<input type="radio" name="contact-option" id="2" class="otp-text-gap" [(ngModel)]="contact" value="email" (change)="changeOTPChannel()"><span class="otp-channel-text">Email</span>
There are a few things which you should keep in mind, pseudo-classes are not supported by input types
you should not add an id as a number
In this below example I have used the label to trigger onchange for radio
input[type='radio']{
display:none;
}
input[type='radio'] + label{
display:inline-block;
position:relative;
padding-left: 25px;
margin-right:20px;
}
input[type='radio']:checked + label:after {
width: 10px;
height: 10px;
border-radius: 15px;
top: 4px;
left: 4px;
position: absolute;
background-color: #f07f09;
content: "";
}
input[type=radio] + label:before {
content: "";
border-color: #bbbbbb;
border: solid 3px;
width: 13px;
position: absolute;
left: 0;
height: 13px;
border-radius: 50%;
}
<input type="radio" name="contact-option" id="one" class="otp-text-gap" [(ngModel)]="contact" value="cell" (change)="changeOTPChannel()"><label for="one" class="otp-channel-text">Cell number</label>
<input type="radio" name="contact-option" id="two" class="otp-text-gap" [(ngModel)]="contact" value="email" (change)="changeOTPChannel()"><label for="two" class="otp-channel-text">Email</label>

Add CSS to HTML checkbox

I have encountered some problem with my site. I want to add CSS to my button to be something like this which I found on the web. https://codepen.io/AllThingsSmitty/pen/WjZVjo
I can't add an ID to it as I can't change the HTML code as it is auto generated by Wordpress. Any help would be appreciated.
<label class='grunion-field-label checkbox'>
<input type='checkbox' name='g84-iagreetothetermsconditions' value='Yes' class='checkbox' required aria-required='true' />
I agree to the Terms & Conditions. <span>(required)</span>
</label>
With your current DOM structure you go with following snippet:
.grunion-field-label {
position: relative;
display:inline-block;
padding-left:10px;
line-height:30px;
}
.grunion-field-label::before {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 50%;
cursor: pointer;
height: 28px;
left: 0;
position: absolute;
top: 0;
width: 28px;
content:"";
}
.grunion-field-label input[type="checkbox"] + span::after {
border: 2px solid #fff;
border-top: none;
border-right: none;
content: "";
height: 6px;
left: 7px;
opacity: 0;
position: absolute;
top: 8px;
transform: rotate(-45deg);
width: 12px;
}
.grunion-field-label input[type="checkbox"] {
visibility: hidden;
}
.grunion-field-label input[type="checkbox"]:checked + span::after {
border-color: #f00;
opacity: 1;
}
<label class='grunion-field-label checkbox'>
<input type='checkbox' name='g84-iagreetothetermsconditions' value='Yes' class='checkbox' required aria-required='true' />
I agree to the Terms & Conditions. <span>(required)</span>
</label>
You may add external css to re-style the checkbox and also you may need to get some help from javascript.
(function($) {
$('input[type="checkbox"]').on('click', function() {
if ($(this).is(':checked')) {
$(this).closest('label').addClass('checked');
} else {
$(this).closest('label').removeClass('checked');
}
});
})(jQuery);
.grunion-field-label.checkbox {
position: relative;
padding-left: 24px;
}
.grunion-field-label.checkbox input[type="checkbox"] {
display: none;
}
.grunion-field-label.checkbox:before {
content: " ";
position: absolute;
left: 0;
width: 20px;
height: 20px;
border-radius: 10px;
background-color: #fff;
border: 1px solid #ddd;
}
.grunion-field-label.checkbox.checked:after {
content: " ";
position: absolute;
top: 4px;
left: 8px;
border: 2px solid #fff;
border-left: none;
border-top: none;
width: 4px;
height: 8px;
transform: rotate(45deg);
}
.grunion-field-label.checkbox.checked:before {
background-color: #0c0;
border-color: #0c0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class='grunion-field-label checkbox'>
<input type='checkbox' name='g84-iagreetothetermsconditions' value='Yes' class='checkbox' required aria-required='true' />
I agree to the Terms & Conditions. <span>(required)</span>
</label>

Custom checkboxes move slightly when selected

I modified the checkboxes in my project to look more aligned with the rest of the design but I have a problem: whenever selected the label of the checkbox moves slightly.
input[type="checkbox"] {
display: none !important;
}
input[type="checkbox"]+label:before {
position: relative;
content: " ";
display: inline-block;
width: 22px;
height: 22px;
border: 1px solid grey;
top: 4px;
margin: 0 10px 0 0;
}
input[type="checkbox"]:checked+label:before {
position: relative;
content: "✔";
display: inline-block;
width: 22px;
height: 22px;
border: 1px solid grey;
top: 4px;
margin: 0 10px 0 0;
}
<input type="checkbox" />
<label>Click to accept</label>
Here's the result:
And here's what happens if I select it:
What am I doing wrong?
Nowadays and for awhile you can fix this if you set the parent to display:flex
input[type="checkbox"] {
display: none !important;
}
label {
display: flex;
/* just to vertically the text with the box */
align-items: center
}
input[type="checkbox"]+label::before {
content: "";
display: block;
width: 22px;
height: 22px;
border: 1px solid grey;
margin: 0 10px 0 0;
}
input[type="checkbox"]:checked+label::before {
content: "✔";
/* just to center inside the box */
display: grid;
place-content: center;
}
<input id="input" type="checkbox" />
<label for="input">Click to accept</label>
OLD ANSWER
To fix the "moving" you need to:
set vertical-align: some value in label::before I have choose bottom.
And to align the "✔" (in case it doesn't - snippet isn't), you need to:
add text-align:center and line-height:22px (same as the height) in :checked+label::before
input[type="checkbox"] {
display: none !important;
}
input[type="checkbox"]+label::before {
position: relative;
content: " ";
display: inline-block;
vertical-align: bottom;
width: 22px;
height: 22px;
border: 1px solid grey;
top: 4px;
margin: 0 10px 0 0;
}
input[type="checkbox"]:checked+label::before {
content: "✔";
text-align: center;
line-height: 22px;
}
<input id="input" type="checkbox" />
<label for="input">Click to accept</label>
NB: In both answers I removed duplicated properties and you're missing the for attribute in label to match id in input, otherwise you can't click in the pseudo checkbox, only in the label
You can see another way here.
See below working example:
/*--CSS--*/
input[type="checkbox"] {
-webkit-appearance: none;
background-color: transparent;
-webkit-rtl-ordering: logical;
user-select: text;
cursor: pointer;
padding: 1px;
border-width: 0;
border-style: inset;
border-color: initial;
border-image: initial;
float: left;
margin: 0 25px 0 0;
position: relative;
}
input[type="checkbox"]:after {
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
content: "";
position: absolute;
left: 0;
z-index: 1;
width: 16px;
height: 16px;
border: 1px solid #c0c0c0;
top:0;
}
input[type="checkbox"]:before {
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
content: "";
position: absolute;
left: 0;
z-index: 1;
width: 16px;
height: 16px;
border: 1px solid #c0c0c0;
top:0;
opactity:0;
}
input[type="checkbox"]:checked:before {
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
height: 5px;
border-color: #009688;
border-top-style: none;
border-right-style: none;
outline: none;
opacity: 1;
width: 12px;
top: 3px;
left: 3px;
}
input[type="checkbox"]:focus{outline:0;}
<!--HTML-->
<input type="checkbox" />
<label>Click to accept</label>
I have used a flexbox for the label to align the items vertically. Adding text-align:center when the checkbox is checked, ensures the marker is horizontally centered.
label {
display: flex;
align-items: center;
}
input[type="checkbox"] {
display: none;
}
input[type="checkbox"]+label:before {
content: " ";
display: inline-block;
width: 22px;
height: 22px;
border: 1px solid grey;
margin-right: 0.5em;
}
input[type="checkbox"]:checked+label:before {
content: "✔";
display: inline-block;
width: 22px;
height: 22px;
border: 1px solid grey;
margin-right: 0.5em;
text-align: center;
}
<input type="checkbox" id="accept" />
<label for="accept">Click to accept</label>
you can try this, this is working
input[type="checkbox"] {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
}
input[type="checkbox"] + label:before {
position: relative;
content: " ";
display: inline-block;
width: 22px;
height: 22px;
border: 1px solid grey;
top: 4px;
margin: -8px 10px 0 0;
vertical-align: middle;
}
input[type="checkbox"]:checked + label:before {
content: "✔";
text-align: center;
line-height: 22px;
}
<input type="checkbox"/>
<label>Click to accept</label>

Erased border with the scale

There stylized input, but the zoom in / out, they change size, it seems as if the border is lost, I can't understand what went wrong, thanks in advance.
My codepen
For example, Chrome - zoom 75%
<div class="checkbox-remember-me">
<input type="radio" name="gender" id="male" checked/>
<label for="male"></label>
</div>Male
This happens because you use an absolute positioned label to cover its parent partially to make it appear to have a border, and when the page is zoomed, depending on how the browser calculate its position, it jumps a pixel up and down, hence sometimes fully aligns with its parent's edge.
Update your css like this, and use a border instead, and it will work fine.
.checkbox-remember-me {
display: inline-block;
width: 24px;
height: 24px;
position: relative;
border: 1px solid #666;
margin-left: 34px;
margin-right: 10px;
}
.checkbox-remember-me label {
width: 22px;
height: 22px;
cursor: pointer;
position: absolute;
background: #eee;
margin: 1px;
}
.checkbox-remember-me {
display: inline-block;
width: 24px;
height: 24px;
position: relative;
border: 1px solid #666;
margin-left: 34px;
margin-right: 10px;
}
.checkbox-remember-me label {
width: 22px;
height: 22px;
cursor: pointer;
position: absolute;
background: #eee;
margin: 1px;
}
.checkbox-remember-me label:after {
content: '';
width: 15px;
height: 10px;
position: absolute;
top: 4px;
left: 4px;
border: 3px solid red;
border-top: none;
border-right: none;
background: transparent;
opacity: 0;
transform: rotate(-45deg);
}
.checkbox-remember-me label:hover:after {
opacity: 0.3;
}
input[type=radio] {
display: none;
}
input[type=radio]:checked + label:after {
opacity: 1;
}
<h4>Gender</h4>
<div class="checkbox-remember-me">
<input type="radio" name="gender" id="male" checked/>
<label for="male"></label>
</div>Male
<div class="checkbox-remember-me">
<input type="radio" name="gender" id="female" />
<label for="female"></label>
</div>Female
Updated codepen: https://codepen.io/anon/pen/LRLrNO

Custom checkbox using only CSS and HTML

I need to create a custom checkbox using only html and CSS. So far I have:
HTML/CSS:
.checkbox-custom {
opacity: 0;
position: absolute;
}
.checkbox-custom,
.checkbox-custom-label {
display: inline-block;
vertical-align: middle;
margin: 5px;
cursor: pointer;
}
.checkbox-custom + .checkbox-custom-label:before {
content: '';
background: #fff;
border-radius: 5px;
border: 2px solid #ddd;
display: inline-block;
vertical-align: middle;
width: 10px;
height: 10px;
padding: 2px;
margin-right: 10px;
text-align: center;
}
.checkbox-custom:checked + .checkbox-custom-label:before {
content: "";
display: inline-block;
width: 1px;
height: 5px;
border: solid blue;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
border-radius: 0px;
margin: 0px 15px 5px 5px;
}
<div>
<input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
<label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
<input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
<label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>
The checked checkbox should be a checkmark with the square bordered around it instead of just a checkmark. Searching for answers, I have only found cases where the checkmark is displayed using a UTF-8 character or an image. I am not able to use either of these for what I am trying to accomplish. I am only able to use plain CSS and HTML. Any suggestions?
codepen here: http://codepen.io/alisontague/pen/EPXagW?editors=110
The problem is that you are using the same pseudo element for the square border and the checkmark. The simple solution would be to continue using the :before pseudo element for the border, and then use an :after pseudo element for the checkmark.
Updated Example
You would have to absolutely position the :after pseudo element relative to the parent .checkbox-custom label element.
Here is the updated code:
.checkbox-custom {
display: none;
}
.checkbox-custom-label {
display: inline-block;
position: relative;
vertical-align: middle;
margin: 5px;
cursor: pointer;
}
.checkbox-custom + .checkbox-custom-label:before {
content: '';
background: #fff;
border-radius: 5px;
border: 2px solid #ddd;
display: inline-block;
vertical-align: middle;
width: 10px; height: 10px;
padding: 2px; margin-right: 10px;
}
.checkbox-custom:checked + .checkbox-custom-label:after {
content: "";
padding: 2px;
position: absolute;
width: 1px;
height: 5px;
border: solid blue;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
top: 2px; left: 5px;
}
<h3>Checkboxes</h3>
<div>
<input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
<label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
<input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
<label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>
My previous answer is wrong because it uses the ::before pseudo-element selector on an element that isn't a container. Thanks to #BoltClock for pointing out my error. So, I came up with another solution that uses the Checkbox Hack and the CSS3 sibling selector (~).
Demo at CodePen
input[type=checkbox] {
position: absolute;
top: -9999px;
left: -9999px;
}
input[type=checkbox] ~ label::before {
content: '\2713';
display: inline-block;
text-align: center;
color: white;
line-height: 1em;
width: 1em;
height: 1em;
border: 1px inset silver;
border-radius: 0.25em;
margin: 0.25em;
}
input[type=checkbox]:checked ~ label::before {
color: black;
}
<form name="checkbox-form" id="checkbox-form">
<div>
<input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
<label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
<input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
<label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>
</form>
I didn't use StackOverflow's "Run Code-Snippet" functionality because it does something weird when I run it. I think it's because of the checkbox hack.
Custom checkbox using only HTML and CSS along with three checkbox states (checked, unchecked and half checked or unchecked(if it's used in a group of checkboxes))
<label class="checkboxcontainer"> one
<input type="checkbox">
<span class="checkmark"></span>
</label>
.checkboxcontainer input {
display: none;
}
.checkboxcontainer {
display: inlin-block;
padding-left: 30px;
position: relative;
cursor: pointer;
user-select: none;
}
.checkboxcontainer .checkmark {
display: inlin-block;
width: 25px;
height: 25px;
background: #eee;
position: absolute;
left: 0;
top: 0;
}
.checkboxcontainer input:checked+.checkmark {
background-color: #2196fc;
}
.checkboxcontainer input:checked+.checkmark:after {
content: "";
position: absolute;
height: 4px;
width: 9px;
border-left: 3px solid white;
border-bottom: 3px solid white;
top: 45%;
left: 50%;
transform: translate(-50%, -50%) rotate(-45deg);
}
.checkboxcontainer input:indeterminate+.checkmark:after {
content: "";
position: absolute;
height: 0px;
width: 9px;
border-left: 3px solid white;
border-bottom: 3px solid white;
top: 45%;
left: 50%;
transform: translate(-50%, -50%) rotate(180deg);
}
Jsfiddle: Demo
.checkbox-custom {
position: relative;
}
.checkbox-custom input[type=checkbox] {
display: none;
}
.checkbox-custom input[type=checkbox]~b {
cursor: pointer;
outline: 0;
position: relative;
display: inline-block;
margin: 4px 1px 0;
background-color: #ffffff;
border: 2px solid #ad823a;
width: 24px;
height: 24px;
vertical-align: middle;
line-height: 1;
text-align: center;
font-size: 20px;
color: #ad823a;
}
.checkbox-custom input[type=checkbox]:checked~b:after {
content: '\2713';
}
<div class="checkbox-custom">
<label>
<input type="checkbox">
<b></b>
<span>app 1</span>
</label>
</div>
<div class="checkbox-custom">
<label>
<input type="checkbox">
<b></b>
<span>app 1</span>
</label>
</div>