HTML Checking sibling radio button is checked [duplicate] - html

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);
}

Related

Animate element with smooth fade-in on appearance, but instantly hide on disappearance [duplicate]

This question already has answers here:
Transition only on hover-in
(2 answers)
Closed 2 years ago.
I've got an element with opacity: 0.
When a certain selector matches, I would like opcacity to smoothly transition to 1.
When that selector no longer matches, I would like opacity to instantly revert to 0.
How do I do that?
PS No JS, of course.
.the {
display: inline-block;
border: 1px solid deepskyblue;
background-color: lightblue;
padding: 2px 5px;
border-radius: 4px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
pointer-events: none;
}
input:checked ~ .the {
opacity: 1;
}
<label>
<input type="checkbox">
Show element
<span class="the">
I'm the element to animate!
</span>
</label>
You can add transition to the :checked rule:
.the {
display: inline-block;
border: 1px solid deepskyblue;
background-color: lightblue;
padding: 2px 5px;
border-radius: 4px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
pointer-events: none;
}
input:checked ~ .the {
transition: opacity .5s;
opacity: 1;
}
<label>
<input type="checkbox">
Show element
<span class="the">
I'm the element to animate!
</span>
</label>

Change position of toggle switch in CSS/HTML

I have a toggle switch below and I'm trying to position it somewhere else on my page but for some reason it doesn't go past this tiny bubble of space. I added the 'top' and left' elements under .switch{ but as I increase the numbers, the button stays put. Does anyone know how to fix this? There are position elements in the other classes and whenever I change the numbers, the toggle switch and text gets all messed up. I want to be able to move the button in its entirety.
html:
<label class="switch">
<input type="checkbox" id="togBtn">
<div class="slider round">
<!--ADDED HTML -->
<span class="on">ON</span>
<span class="off">OFF</span>
<!--END-->
</div>
</label>
css:
.switch {
position: absolute;
display: inline-block;
width: 90px;
height: 34px;
top: 500;
left: 600;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2ab934;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(55px);
-ms-transform: translateX(55px);
transform: translateX(55px);
}
.on
{
display: none;
}
.on, .off
{
color: white;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}
input:checked+ .slider .on
{display: block;}
input:checked + .slider .off
{display: none;}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;}
top & left value need unit eg. 500px, 50rem or 10% but why you added position :absolute in .switch class, if position required for your case then ok, but using absolute positioning the switch is not proper solution

CSS Custom RadioButton not working [duplicate]

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>

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>

How to add label to text field in css AND to give help text also [duplicate]

I am looking to find an example of how to make a label / placeholder transition move up and out of placeholder position into a label position and vice versa..
Example: https://www.xero.com/us/signup/
General sibling selectors & :focus does the trick in a very simple way ;)
input{
position:absolute;
top:20px;
}
input ~ span{
transition:top .7s ease;
position:absolute;
top:20px;
}
input:focus ~ span{
top:0px;
}
<label>
<input>
<span>Text</span>
</label>
here is an example with multiple fields
https://jsfiddle.net/shLe3107/1/
hope this is enough else just ask
I found a good Codepen showing an example of how to do it in CSS.
HTML:
<div class="row">
<input id="name" type="text" name="name">
<label for="name">Full Name</label>
</div>
CSS:
.row {
position: relative;
margin-top: 35px;
}
input {
display: block;
padding: 8px 12px;
width: 100%;
max-width: 300px;
border-radius: 5px;
border: 0;
}
label {
position: absolute;
font-weight: 600;
color: #777777;
top: 50%;
left: 12px;
transform: translateY(-50%);
-ms-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
cursor: text;
user-select: none;
transition: 0.15s ease-in-out;
}
input[data-empty="false"] + label,
input:valid + label,
input:focus + label {
top: -10px;
left: 0px;
font-size: 10px;
color: #ffffff;
}
Example:
https://codepen.io/sivan/pen/alKwf