CSS Custom RadioButton not working [duplicate] - html

This question already has an answer here:
Why are my show/hide styles not working when I click the radio button?
(1 answer)
Closed 4 years ago.
I'm making a simple custom radio button using CSS and I don't know why is not working as like a normal radio button. When I choose one, the other selected itself too (?).
/* Radio Button */
.radioBtn{
float: right;
margin-top: 30px;
height: 35px;
width: 35px;
border: solid 3px #d8aa00;
border-radius: 50%;
background: #fffbd8;
position: relative;
transition: .3s;
}
.radioBtn::after{
position: absolute;
width: 20px;
height: 20px;
background: #d8aa00;
border-radius: 50%;
content: '';
top: 7px;
left: 7px;
opacity: 0;
transition: .3s;
}
input[type=radio]:checked ~ .radioBtn::after {
opacity: 1;
}
input[type=radio]{
display: none;
}
<input id="radioBtn" type="radio" name="test">
<label class="radioBtn" for="radioBtn"></label>
<input id="radioBtn2" type="radio" name="test">
<label class="radioBtn" for="radioBtn2"></label>

Use + instead of ~ to target only the immediate sibling placed after and not all of them.
The + combinator selects adjacent siblings. This means that the second
element directly follows the first, and both share the same parent.ref
The ~ combinator selects siblings. This means that the second element
follows the first (though not necessarily immediately), and both share
the same parentref
/* Radio Button */
.radioBtn{
float: right;
margin-top: 30px;
height: 35px;
width: 35px;
border: solid 3px #d8aa00;
border-radius: 50%;
background: #fffbd8;
position: relative;
transition: .3s;
}
.radioBtn::after{
position: absolute;
width: 20px;
height: 20px;
background: #d8aa00;
border-radius: 50%;
content: '';
top: 7px;
left: 7px;
opacity: 0;
transition: .3s;
}
input[type=radio]:checked + .radioBtn::after {
opacity: 1;
}
input[type=radio]{
display: none;
}
<input id="radioBtn" type="radio" name="test">
<label class="radioBtn" for="radioBtn"></label>
<input id="radioBtn2" type="radio" name="test">
<label class="radioBtn" for="radioBtn2"></label>

Related

HTML Checking sibling radio button is checked [duplicate]

This question already has answers here:
What does the "~" (tilde/squiggle/twiddle) CSS selector mean?
(3 answers)
Is there a "previous sibling" selector?
(30 answers)
Closed 2 years ago.
So i have this assignment where i need to make a custom radio button that if checked then a success logo come out.
i succeed to create the box but not the content i first though its the real radio button not working but after i turn up the opacity(i use opacity to hide the real one in case this didn't clear enough) the real radio button work. i try asking my senior he didn't really answer my question
here's the html
<label for="Viridian">
<span class="checkbox"></span>
Viridian
<input type="radio" id="Viridian" name="color" value="Viridian" checked="checked">
</label>
and here's the css
.checkbox {
display: block;
width: 18px;
height: 18px;
cursor: pointer;
margin-right: 2vh;
border: solid 1px white;
}
input ~ .checkbox::after {
content: '';
vertical-align: middle;
font-size: 15px;
text-align: center;
}
input:checked ~ .checkbox::after {
content: 'a';
}
I think this is what you're looking for.
HTML:
<p>
<input type="radio" id="test3" name="radio-group">
<label for="test3">Orange</label>
</p>
CSS:
[type="radio"]: checked,
[type="radio"]: not(: checked) {
position: absolute;
left: -9999px;
}
[type="radio"]: checked+label,
[type="radio"]: not(: checked)+label {
position: relative;
padding - left: 28px;
cursor: pointer;
line - height: 20px;
display: inline - block;
color: #666;
}
[type="radio"]: checked+label: before,
[type="radio"]: not(: checked)+label: before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 18px;
height: 18px;
border: 1px solid #ddd;
border - radius: 100 %;
background: #fff;
}
[type="radio"]: checked+label: after,
[type="radio"]: not(: checked)+label: after {
content: '';
width: 12px;
height: 12px;
background: #F87DA9;
position: absolute;
top: 4px;
left: 4px;
border - radius: 100 %;
-webkit - transition: all 0.2s ease;
transition: all 0.2s ease;
}
[type="radio"]: not(: checked)+label: after {
opacity: 0;
-webkit - transform: scale(0);
transform: scale(0);
}
[type="radio"]: checked+label: after {
opacity: 1;
-webkit - transform: scale(1);
transform: scale(1);
}

Cannot able to float label to top when focus in textbox

I want to float the label when users focus on textboxes as in this example, but on focus the label does not move upwards. Below is my code.
.input-base-input {
width: 100%;
font-size: 15px;
padding-top: 8px;
border: none;
border-bottom: .5px solid #a9abb3;
background: transparent;
font-weight: 600;
}
.input-base-input:focus ~ .input-base-label,
.input-base-input:not(:focus):valid ~ .input-base-label{
display: block;
position: absolute;
top: 10px;
transition-property: top;
transition-duration: .1s;
}
.input-base-label {
position: absolute;
pointer-events: none;
top:-10;
transition-property: top;
transition-duration: .1s;
}
<label for="pincode" class="input-base-label">Pin Code</label>
<input class="input-base-input" maxlength="6">
First, this CSS:
.input-base-input:focus ~ .input-base-label
will select the label which comes after the input (and not before as shown in your code), so first change the order of input and label.
Second, you have not specified the correct value for the top property of .input-base-label:
.input-base-label {
...
/* Not correct */
top: -10;
/* Correct */
top: -10px;
/* or */
top: 0;
...
}
Third, :valid selector will make your input valid even if it's empty (so your label will be floated on the page load). To resolve this with CSS-only approach, add required attribute to the input.
So, your final result might look like this:
<input class="input-base-input" id="pincode" name="pincode" maxlength="6" required>
<label class="input-base-label" for="pincode">Pin code</label>
.input-base-input {
width: 100%;
font-size: 15px;
padding-top: 8px;
border: none;
border-bottom: .5px solid #a9abb3;
background: transparent;
font-weight: 600;
}
.input-base-label {
position: absolute;
pointer-events: none;
top: 0;
transition-property: top;
transition-duration: .1s;
}
.input-base-input:focus ~ .input-base-label,
.input-base-input:not(:focus):valid ~ .input-base-label {
display: block;
top: -10px;
/* The following properties are not needed as they are specified previously */
/*
position: absolute;
transition-property: top;
transition-duration: .1s;
*/
}
<input class="input-base-input" id="pincode" name="pincode" maxlength="6" required>
<label class="input-base-label" for="pincode">Pin code</label>

Align label with css 3 fancy radio button

I have the following radio button, I need the bigger circle to be 38px
input[type=radio] {
visibility: hidden;
}
.label {
font-weight: normal;
color: #333;
}
.label::before {
content: '';
position: absolute;
left: 0;
width: 38px;
height: 38px;
border: 1px solid #727272;
border-radius: 50%;
}
input[type=radio]:checked+label:after {
content: '';
position: absolute;
width: 38px;
height: 38px;
left: 0;
background: #0065bd;
border: none;
transform: scale(0.5);
border-radius: 50%;
}
<div class="container">
<input type="radio" id="radio1" value="on">
<label for="radio1" class="label">Yes</label>
</div>
Here is a fiddle, how can I align the label so it is aligned to the centered and pushed to the right of the circle?
Add .container{ line-height:38px} to have it centered (it seems that it was to the right already)
https://jsfiddle.net/8gubpzhq/
to move it to the right add this to the
.label {
font-weight: normal;
color: #333;
padding-left:5px;//add this line
}
https://jsfiddle.net/vszuu535/
You can add line-height:40px; to your .label to center it vertically. To move it over to the right more you can add padding-left:20px; (You can change the line-height and padding-left to fit your needs).
input[type=radio] {
visibility: hidden;
}
.label {
font-weight: normal;
color: #333;
line-height:40px;
padding-left:20px;
}
.label::before {
content: '';
position: absolute;
left: 0;
width: 38px;
height: 38px;
border: 1px solid #727272;
border-radius: 50%;
}
input[type=radio]:checked + label:after {
content: '';
position: absolute;
width: 38px;
height: 38px;
left: 0;
background: #0065bd;
border: none;
transform: scale(0.5);
border-radius: 50%;
}
<div class="container">
<input type="radio" id="radio1" value="on">
<label for="radio1" class="label">Yes</label>
</div>
Perhaps your code is over-complicating matters; as you want the input to be bigger, maybe you should focus the sizing etc on the input rather than the label?
See the snippet (the radio turns blue now since edit, adapted from this codepen. It's grey before click, blue after, centered, and indented from the edge).
Just a note: If you are going to use a default value (and only have one option) maybe a custom checkbox would be a more suitable choice? (Radios button are usually used in instances where the user would have 2 or more choices, but can only select one).. just a thought.
input[type="radio"] {
display: none;
width: 38px;
height: 38px;
border: 1px solid #727272;
}
input[type="radio"]+label span {
display: inline-block;
width: 38px;
height: 38px;
margin: 9px;
vertical-align: middle;
cursor: pointer;
border-radius: 50%;
background-color: grey;
}
input[type="radio"]:checked+label span {
content="";
background: #0065bd;
}
input[type="radio"]+label span,
input[type="radio"]:checked+label span {
-webkit-transition: background-color 0.4s linear;
-o-transition: background-color 0.4s linear;
-moz-transition: background-color 0.4s linear;
transition: background-color 0.4s linear;
}
<div class="container">
<input type="radio" id="radio1" name="radio">
<label for="radio1" class="label"><span></span>Yes</label>
<input type="radio" id="radio2" name="radio">
<label for="radio2" class="label"><span></span>No</label>
</div>

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

How to make checkboxes rounded?

Is there any way to make checkboxes with rounded corners using bootstrap or some css property?
Just using css, however, you lose the checkbox tick.
.checkbox-round {
width: 1.3em;
height: 1.3em;
background-color: white;
border-radius: 50%;
vertical-align: middle;
border: 1px solid #ddd;
appearance: none;
-webkit-appearance: none;
outline: none;
cursor: pointer;
}
.checkbox-round:checked {
background-color: gray;
}
<input type="checkbox" class="checkbox-round" />
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 15px;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196F3;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
<label class="container">One
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="checkbox">
<span class="checkmark"></span>
</label>
Try to do
body {
background-color: #f1f2f3;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.container {
margin: 0 auto;
}
.round {
position: relative;
}
.round label {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 50%;
cursor: pointer;
height: 28px;
left: 0;
position: absolute;
top: 0;
width: 28px;
}
.round label: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;
}
.round input[type="checkbox"] {
visibility: hidden;
}
.round input[type="checkbox"]:checked + label {
background-color: #66bb6a;
border-color: #66bb6a;
}
.round input[type="checkbox"]:checked + label:after {
opacity: 1;
}
<div class="container">
<div class="round">
<input type="checkbox" id="checkbox" />
<label for="checkbox"></label>
</div>
</div>
One of the Best And Easiest Method is to use CSS clip path property:
<input type="checkbox" id="checkbox" />
<label for="checkbox" >Option</label>
input[type="checkbox"] {
width: 45px; /* Set width */
height: 45px; /* Set height */
clip-path: circle(46% at 50% 50%); /* Set the clip path of circle*/
}
if you still see some pointed corners, try reducing the first percentage values, (where I have used 46%), play with it a little bit and it will definitely work.
Well, this is the simplest and optimal solution. You set appearance to none and then use clip-path when its checked.
.rounded-checkbox {
width:35px;
height: 35px;
border-radius: 50%;
vertical-align: middle;
border: 1px solid black;
appearance: none;
-webkit-appearance: none;
outline: none;
cursor: pointer;
}
.rounded-checkbox:checked {
appearance: auto;
clip-path: circle(50% at 50% 50%);
background-color: blue;
}
<input
type="checkbox"
class="rounded-checkbox"
id="checkbox"
/> <label for="checkbox">Checkbox</label>
in css you may play with height, width, border-radius. basically height and width should be equal and border-radius should be half of them.
if you are using bootstrap you can use this class and add it where your shape or in this case your checkbox class is: class="rounded-circle"
look: borders bootstrap 5
for example to make a checkbox rounded in bootstrap 5 (also working for v4):
<div class="form-check">
<input
class="form-check-input rounded-circle"
type="checkbox"
/>
<label class="form-check-label" for="flexCheck1">
rounded checkbox
</label>
</div>
CSS
.checkbox {
clip-path: circle(46% at 50% 50%);
}
HTML
<input type="checkbox" class="checkbox" />
No need to hide the default checkbox and create a custom just set the clip-path and you can easily achieve a circular checkbox.
I thing the best way to make a rounded corners is by using the border-radius property. This site has a nice collection of checkboxes.
For example:
cursor: pointer;
position: absolute;
width: 20px;
height: 20px;
top: 0;
border-radius: 10px;
The last line(border-radius: 10px) will give you a checkbox with rounded corners.