Custom radio button not getting checked on clicking - html

I created a custom radio button using HTML and CSS
.radio {
display: inline-flex;
align-items: center;
cursor: pointer;
margin-left: 10px;
}
.radio__input {
display: none;
}
.radio__radio {
width: 1.25em;
height: 1.25em;
border: 2px solid #87cac3;
border-radius: 50%;
margin-left: 10px;
box-sizing: border-box;
padding: 2px;
}
.radio__radio::after {
content: "";
width: 100%;
height: 100%;
display: block;
background: #87cac3;
border-radius: 50%;
transform: scale(0);
transition: transform 0.15s;
}
.radio__input:checked+.radio__radio::after {
transform: scale(1)
}
<div>
<label for="myRadioId" class="radio">Agree</label>
<input type="radio" name="myRadioField" id="myRadioId" class="radio__input">
<div class="radio__radio"></div>
</label>
</div>
The code doesn't seem to work accurately means, the radio button is not getting checked on clicking. I think the problem is with the .radio__radio::after inside it i have done trnsform: scale(0) so it should default change its scale to zero on refreshing the page and when checked it should change the scale to 1.

Use the following code, hope it will be helpful .
var val = 0;
$('input[name=myRadioField]').on("click", function(){
console.log(val);
if($(this).val()==val)
{$(this).prop('checked', false);
val = -1;
}
else val = $(this).val();
});
.radio {
display: inline-flex;
align-items: center;
cursor: pointer;
margin-left: 10px;
}
.radio__input {
display: none;
}
.radio__radio {
width: 1.25em;
height: 1.25em;
border: 2px solid #87cac3;
border-radius: 50%;
margin-left: 10px;
box-sizing: border-box;
padding: 2px;
}
.radio__radio::after {
content: "";
width: 100%;
height: 100%;
display: block;
background: #87cac3;
border-radius: 50%;
transform: scale(0);
transition: transform 0.15s;
}
.radio__input:checked+.radio__radio::after {
transform: scale(1)
}
.radio__input:unchecked+.radio__radio::after {
transform: scale(0)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div>
<label for="myRadioId" class="radio">Agree
<input type="radio" name="myRadioField" id="myRadioId" class="radio__input">
<div class="radio__radio"></div>
</label>
</div>

Thanks everyone for your inputs.
The problem is solved I just added autocomplete= "off" inside <input> tag and it worked fine.
.container {
padding-top: 10px;
text-align: center;
}
.radio {
display: inline-flex;
align-items: center;
cursor: pointer;
margin-left: 10px;
}
.radio__input {
display: none;
}
.radio__radio {
width: 1.25em;
height: 1.25em;
border: 2px solid #87cac3;
border-radius: 50%;
margin-left: 10px;
box-sizing: border-box;
padding: 2px;
padding-bottom: 2px;
}
.radio__radio::after {
content: "";
width: 100%;
height: 100%;
display: block;
background: #87cac3;
border-radius: 50%;
transform: scale(0);
transition: transform 0.15s;
}
.radio__input:checked+.radio__radio::after {
transform: scale(1)
}
<div class="container">
<h1>Question 1</h1>
<h4>Agree
<label class="radio">
<input type="radio" name="myRadioField" id="myRadioId" class="radio__input" autocomplete="off" value="1">
<div class="radio__radio"></div>
</label>
<label class="radio">
<input type="radio" name="myRadioField" id="myRadioId" class="radio__input" autocomplete="off" value="2">
<div class="radio__radio"></div>
</label>
<label class="radio">
<input type="radio" name="myRadioField" id="myRadioId" class="radio__input" autocomplete="off" value="3">
<div class="radio__radio"></div>
</label>
<label class="radio">
<input type="radio" name="myRadioField" id="myRadioId" class="radio__input" autocomplete="off" value="4">
<div class="radio__radio"></div>
</label>
<label class="radio">
<input type="radio" name="myRadioField" id="myRadioId" class="radio__input" autocomplete="off" value="5">
<div class="radio__radio"></div>
</label> Disagree
</h4>
</div>

Related

CSS: How to set url image properly to checked checkbox

I have this scenario, I have this list of checkbox with corresponding image for it, If the checkbox is checked, I want to append black circle at the back of checkbox image
Sample current output:
Sample expected output:
Code for populating checkbox:
<div
key={item.id}
className="chk-multiple-badge form-check form-check-inline"
>
<input
className="chkbox-badge form-check-input"
type="checkbox"
id={item.id}
/>
<label
htmlFor={item.id}
className="form-check-label form-check-label-badge"
>
<Row>
<Col>
<img
className="chk-badge-img"
src={item.imgBase64}
alt={item.badge_name}
/>
</Col>
</Row>
<Row>
<Col>{item.badge_name}</Col>
</Row>
</label>
</div>
And CSS for checkbox:
:checked + .form-check-label-badge:before {
content: url("../../../../assets/images/checkd-badge.png");
position: absolute;
cursor: pointer;
}
.chkbox-badge {
display: none;
}
Pure semantic HTML/CSS solution
This is easy to implement
This is what you need to do:
Your checkboxes need to have distinct id attributes. This allows you to connect a to it, using the label's for-attribute.
Example:
<input type="checkbox" id="myCheckbox1" />
<label for="myCheckbox1"><img src="http://someurl" /></label>
Here is a working snippet :
ul {
list-style-type: none;
}
li {
display: inline-block;
}
input[type="checkbox"][id^="cb"] {
display: none;
}
label {
border: 4px solid transparent; border-radius: 50%;
display: block;
position: relative;
margin: 10px;
cursor: pointer;
}
label:before {
background-color: white;
color: white;
content: " ";
display: block;
border-radius: 50%;
border: 1px solid grey;
position: absolute;
top: -5px;
left: -5px;
width: 25px;
height: 25px;
text-align: center;
line-height: 28px;
transition-duration: 0.4s;
transform: scale(0);
}
label img {
height: 100px;
width: 100px;
transition-duration: 0.2s;
transform-origin: 50% 50%;
}
:checked + label {
border-color: #000;
}
:checked + label:before {
content: "✓";
background-color: grey;
transform: scale(1);
}
:checked + label img {
transform: scale(0.9);
border-radius: 50%;
z-index: -1;
}
<ul>
<li><input type="checkbox" id="cb1" />
<label for="cb1"><img src="https://m.media-amazon.com/images/I/61Dnvcie7PL._SL1024_.jpg" /></label>
</li>
<li><input type="checkbox" id="cb2" />
<label for="cb2"><img src="https://m.media-amazon.com/images/I/91vx4DQg4jS._AC_SX466_.jpg" /></label>
</li>
<li><input type="checkbox" id="cb3" />
<label for="cb3"><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSZ31Gis0FZirNYl6OGbnHlXWJTtgZXUVB6eNt7DomL5ZnK_v94cJpNLw24jKMcuQW3q7U&usqp=CAU" /></label>
</li>
</ul>

HTML/CSS elements don't looks precisely in needed position

Made custom checkbox styles. But in different viewport widths dots inside of circles looks in a different position.
This is how it looks in one viewport width and another image shows how it looks in another viewport. In all cases, the radius of elements doesn't change. Here you can see the code that I use for the WordPress website.
Is there some way to make dots stable in any viewports?
input {
&[type="radio"] {
&:checked, &:not(:checked) {
position: absolute;
left: -9999px;
& + label {
position: relative;
padding-left: 28px;
cursor: pointer;
line-height: 20px;
display: inline-block;
color: #666;
&::before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 16px;
height: 16px;
border: 1px solid rgba(62, 62, 62, 0.5);
border-radius: 100%;
background: #fff;
}
&::after {
content: '';
width: 10px;
height: 10px;
background: $theme_color;
position: absolute;
top: 3px;
left: 3px;
border-radius: 100%;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
}
}
&:not(:checked) {
& + label {
&::after {
opacity: 0;
-webkit-transform: scale(0);
transform: scale(0);
}
}
}
&:checked {
& + label {
&::after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}
}
}
}
}
<form action="#">
<p>
<input type="radio" id="test1" name="radio-group" checked>
<label for="test1">Apple</label>
</p>
<p>
<input type="radio" id="test2" name="radio-group">
<label for="test2">Peach</label>
</p>
<p>
<input type="radio" id="test3" name="radio-group">
<label for="test3">Orange</label>
</p>
</form>
Add display: flex to the <p> elements to prevent the label text wrapping below the radio button.
p{
display: flex;
}
form{
width: 150px;
background-color: #ddd;
padding: 1rem;
}
<form action="#">
<p>
<input type="radio" id="test1" name="radio-group" checked>
<label for="test1">Apple</label>
</p>
<p>
<input type="radio" id="test2" name="radio-group">
<label for="test2">This is some long piece of text that might wrap on smaller devices</label>
</p>
<p>
<input type="radio" id="test3" name="radio-group">
<label for="test3">Orange</label>
</p>
</form>

How to make radio button like on/off switches using css

I have requirement of radio buttons with on/off method i.e when click on one radio button it should on and when click on second button first button should off but below code is not working as per my expectation.
Here is the snippet for that html and css code:
body {
background: #0288D1;
}
.checkboxes-and-radios {
margin: 80px auto 0;
width: 280px;
padding: 30px;
background: #fafafa;
input {
display: none;
}
label {
cursor: pointer;
padding-right: 35px;
position: relative;
display: block;
font-size: 18px;
padding: 15px 0
}
input[type="checkbox"],
input[type="radio"] {
position: absolute;
visibility: hidden !important;
}
input[type="checkbox"]+label,
input[type="radio"]+label {
&:before,
&:after {
content: '';
position: absolute;
top: 50%;
margin-top: -7.5px;
box-sizing: border-box;
}
&:before {
width: 30px;
height: 15px;
right: 0px;
background: #fff;
border: 1px solid #e4e3e1;
border-radius: 15px;
}
&:after {
width: 15px;
height: 15px;
right: 15px;
background: #BDBDBD;
border-radius: 50%;
transition: all 200ms ease-out;
}
}
input[type="checkbox"]:checked+label,
input[type="radio"]:checked+label {
&:after {
right: 0px;
background: #FF9800;
}
}
}
<div class="checkboxes-and-radios">
<h1>Radios:</h1>
<input type="radio" name="radio-cats" id="radio-1" value="1" checked>
<label for="radio-1">Radio Label 1</label>
<input type="radio" name="radio-cats" id="radio-2" value="2">
<label for="radio-2">Radio Label 2</label>
<input type="radio" name="radio-cats" id="radio-3" value="3" checked>
<label for="radio-3">Radio Label 3</label>
<h1>Checkboxes:</h1>
<input type="checkbox" name="checkbox-cats[]" id="checkbox-1" value="1" checked>
<label for="checkbox-1">Checkbox Label 1</label>
<input type="checkbox" name="checkbox-cats[]" id="checkbox-2" value="2">
<label for="checkbox-2">Checkbox Label 2</label>
<input type="checkbox" name="checkbox-cats[]" id="checkbox-3" value="3" checked>
<label for="checkbox-3">Checkbox Label 3</label>
</div>
expected output:
so how to add css that will look like in image shown in top?
Your code works like a charm for me. In your provided fiddle, the SCSS was not compiled to CSS. Here a compiled version. Radio buttons are switching correctly.
Here also a version on CodePen.
Your CSS is not CSS but SCSS, which has to be compiled to CSS.
You have to use a preprocessor to compile scss to css. You should start reading here:
http://sass-lang.com
body {
background: #0288D1;
}
.checkboxes-and-radios {
margin: 80px auto 0;
width: 280px;
padding: 30px;
background: #fafafa;
}
.checkboxes-and-radios input {
display: none;
}
.checkboxes-and-radios label {
cursor: pointer;
padding-right: 35px;
position: relative;
display: block;
font-size: 18px;
padding: 15px 0;
}
.checkboxes-and-radios input[type="checkbox"],
.checkboxes-and-radios input[type="radio"] {
position: absolute;
visibility: hidden !important;
}
.checkboxes-and-radios input[type="checkbox"]+label:before,
.checkboxes-and-radios input[type="checkbox"]+label:after,
.checkboxes-and-radios input[type="radio"]+label:before,
.checkboxes-and-radios input[type="radio"]+label:after {
content: '';
position: absolute;
top: 50%;
margin-top: -7.5px;
box-sizing: border-box;
}
.checkboxes-and-radios input[type="checkbox"]+label:before,
.checkboxes-and-radios input[type="radio"]+label:before {
width: 30px;
height: 15px;
right: 0px;
background: #fff;
border: 1px solid #e4e3e1;
border-radius: 15px;
}
.checkboxes-and-radios input[type="checkbox"]+label:after,
.checkboxes-and-radios input[type="radio"]+label:after {
width: 15px;
height: 15px;
right: 15px;
background: #BDBDBD;
border-radius: 50%;
transition: all 200ms ease-out;
}
.checkboxes-and-radios input[type="checkbox"]:checked+label:after,
.checkboxes-and-radios input[type="radio"]:checked+label:after {
right: 0px;
background: #FF9800;
}
<div class="checkboxes-and-radios">
<h1>Radios:</h1>
<input type="radio" name="radio-cats" id="radio-1" value="1" checked>
<label for="radio-1">Radio Label 1</label>
<input type="radio" name="radio-cats" id="radio-2" value="2">
<label for="radio-2">Radio Label 2</label>
<input type="radio" name="radio-cats" id="radio-3" value="3" checked>
<label for="radio-3">Radio Label 3</label>
<h1>Checkboxes:</h1>
<input type="checkbox" name="checkbox-cats1" id="checkbox-1" value="1" checked>
<label for="checkbox-1">Checkbox Label 1</label>
<input type="checkbox" name="checkbox-cats2" id="checkbox-2" value="2">
<label for="checkbox-2">Checkbox Label 2</label>
<input type="checkbox" name="checkbox-cats3" id="checkbox-3" value="3" checked>
<label for="checkbox-3">Checkbox Label 3</label>
</div>

Why is my other checkbox panel going missing in this weird custom checkbox issue?

The fiddle is here.
Should be like this, in essence.
But in my custom checkbox, as you'll see in fiddle, it shows like this.
Note there are two inputs in checkgroup before a label
HTML
<div class=" text-left"><span class="font-weight-bold">Include</span> <span class="font-weight-bold">Exclude</span>
<div>
<div>
<div class="checkGroup"><input type="checkbox" class="include" value="Chicken"> <input type="checkbox" class="exclude"> <label>
Chicken
</label></div>
</div>
<div>
<div class="checkGroup"><input type="checkbox" class="include" value="Turkey"> <input type="checkbox" class="exclude"> <label>
Turkey
</label></div>
</div>
<div>
<div class="checkGroup"><input type="checkbox" class="include" value="Beef"> <input type="checkbox" class="exclude"> <label>
Beef
</label></div>
</div>
<div>
<div class="checkGroup"><input type="checkbox" class="include" value="Pork"> <input type="checkbox" class="exclude"> <label>
Pork
</label></div>
</div>
<div>
<div class="checkGroup"><input type="checkbox" class="include" value="Fish"> <input type="checkbox" class="exclude"> <label>
Fish
</label></div>
</div>
<div>
<div class="checkGroup"><input type="checkbox" class="include" value="No Meat - Vegetarian Only"> <input type="checkbox" class="exclude"> <label>
No Meat - Vegetarian Only
</label></div>
</div>
<div>
<div class="checkGroup"><input type="checkbox" class="include" value="No Meat - Vegan Only"> <input type="checkbox" class="exclude"> <label>
No Meat - Vegan Only
</label></div>
</div>
</div>
</div>
CSS
.checkGroup {
display: inline;
.exclude {
margin-left: 2em;
}
.include {
margin-left: 2em;
}
label {
margin-left: 2em;
}
/* 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, 0.1);
}
/* checked mark aspect */
[type="checkbox"]:not(:checked)+label:after,
[type="checkbox"]:checked+label:after {
content: '✔';
position: absolute;
top: .2em;
left: .275em;
font-size: 1.4em;
line-height: 0.8;
color: #09ad7e;
transition: all .2s;
font-family: Helvetica, Arial, sans-serif;
}
/* 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;
}
}
Here's the principle:
.checkGroup {
display: inline-flex;
align-items: center;
cursor: pointer;
}
.checkGroup [type=checkbox] {
visibility: hidden;
position: absolute;
}
cbox-image {
position: relative;
height: 1em;
width: 1em;
margin: 0 .35em;
display: inline-flex;
align-items: center;
justify-content: center;
border: 2px solid #eee;
border-radius: 5px;
}
.checkGroup input:checked + cbox-image:after,
.checkGroup input + cbox-image + cbox-image:after { /* checked state */
content: '✔';
position: absolute;
font-size: 1.4em;
line-height: 0.8;
color: #09ad7e;
font-family: Helvetica, Arial, sans-serif;
}
.checkGroup input:checked + cbox-image + cbox-image:after,
.checkGroup input:not(:checked) + cbox-image:after { /* unchecked state */
content: none;
}
<label class="checkGroup">
<input type="checkbox" value="Turkey">
<cbox-image></cbox-image>
<cbox-image></cbox-image> Turkey
</label>
From your comments and chats, clicking <label> should not trigger any checkbox clicks.
Therefore your original CSS is basically useless because:
You have only 1 label for 2 checkboxes.
All your CSS is styling the 1 label as if there is 2 labels.
For example, this line:
[type="checkbox"]:not(:checked)+label,
[type="checkbox"]:checked+label {
...
}
Does nothing because there isn't enough label available for one of those [type="checkbox"], other than the fact that they are overriding each other.
Besides, your CSS code contains a lot of self-overriding CSS similar to:
[type="checkbox"]:not(:checked), [type="checkbox"]:checked {...}
Where two opposite states use the same styling. It can be simplified to just
[type="checkbox"] {...}
Suggested Solution
You can transfer most styling from label to the [type="checkbox"] and [type="checkbox"]:checked::after.
Check this fiddle.
Not exactly what you asked for, but I'm sure you can tweak it yourself.

Floating list issue

I am creating a list with 5 star rating. I want to put stars in from of list title but my stars are not showing in front of list title.
.cont {
color: #eee;
margin: 0 auto;
max-width: 350px;
overflow: hidden;
padding: 0;
text-align: center;
width: 100%;
}
hr {
margin: 20px;
border: none;
border-bottom: thin solid rgba(255, 255, 255, .1);
}
div.stars {
width: 270px;
display: inline-block;
float: left;
}
input.star {
display: none;
}
label.star {
color: #444;
float: right;
font-size: 25px;
padding: 5px;
transition: all .2s;
}
input.star:checked ~ label.star:before {
content: '\f005';
color: #FD4;
transition: all .25s;
}
input.star-5:checked ~ label.star:before {
color: #FE7;
text-shadow: 0 0 20px #952;
}
input.star-1:checked ~ label.star:before {
color: #F62;
}
label.star:hover {
transform: rotate(-15deg) scale(1.3);
}
label.star:before {
content: '\f006';
font-family: FontAwesome;
}
/* LIST #3 */
#list3 {
float: left;
}
#list3 ul {
list-style-image: url("../images/arrow.png");
color: #4B4B4B;
font-size: 18px;
margin-left: 25px;
}
#list3 ul li {
line-height: 30px;
}
<div id="list3">
<ul>
<li>Estimated time: 43min</li>
<li>Distance in miles: 14 miles</li>
<li>Review Rating:
<div class="cont">
<div class="stars">
<form action="">
<input class="star star-5" id="star-5-2" type="radio" name="star" />
<label class="star star-5" for="star-5-2"></label>
<input class="star star-4" id="star-4-2" type="radio" name="star" />
<label class="star star-4" for="star-4-2"></label>
<input class="star star-3" id="star-3-2" type="radio" name="star" />
<label class="star star-3" for="star-3-2"></label>
<input class="star star-2" id="star-2-2" type="radio" name="star" />
<label class="star star-2" for="star-2-2"></label>
<input class="star star-1" id="star-1-2" type="radio" name="star" />
<label class="star star-1" for="star-1-2"></label>
</form>
</div>
</div>
</li>
</ul>
</div>
Try this: http://codepen.io/g1un/pen/KgvrwZ
.cont {
color: #eee;
margin: 0 auto;
max-width: 350px;
overflow: hidden;
padding: 0;
text-align: center;
/* width: 100%; */
display: inline-block;
vertical-align: top;
}
div.stars {
/* width: 270px; */
display: inline-block;
/* float: left; */
}
Your main mistake causing appearing of your rating icons below the title is that rating block has display: block; property by default as a div and you didn't change it.
It is also no need in width: 100% that causes element jumping to another row to fit this property.