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

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>

Related

Custom radio button not getting checked on clicking

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>

Alignment of a radio from with custom radios

I can not find out why I cannot align following in the center. The classes do not include a specified position, except for the .c class and the specialized container div(not in CSS), which only has an influence on the amount part, but is supposed to align everything in the center of this div. I write with AngularJs, so maybe there is another way to align everything better? maybe with a box-align? Or does it have something to do with the labels/spans?
#spread {
background-color: #0BD44A;
padding: 15px;
margin: 15px;
border-radius: 8px;
}
.c {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
}
/* Hide the browser's default radio button */
.c input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #BEBEBE;
border-radius: 8px;
}
/* On mouse-over, add a grey background color */
.c:hover input~.checkmark {
background-color: #F5F5F5;
}
/* When the radio button is checked, add a blue background */
.c input:checked~.checkmark {
background-color: white;
}
<div id="spread">
<h1>Spread</h1>
<div class="container" align=center>
<h2>Answer following questions and find out how to diversify your capital:</h2>
<label for="amount">
<br>
<h4>Amount you plan to invest:</h4>
<input type="number" class="form-control" id="amount" style="text-align:center" placeholder="250.00$">
</label>
<hr>
<h4>Time-period:</h4>
<h5>
<label class="c">short
<input type="radio" name="radiotime" value="1">
<span class="checkmark"></span>
</label>
<label class="c">medium
<input type="radio" name="radiotime" value="2">
<span class="checkmark"></span>
</label>
<label class="c">long
<input type="radio" name="radiotime" value="3">
<span class="checkmark"></span>
</label>
</h5>
<hr>
<h4>Risk-level:</h4>
<h5>
<label class="c">high
<input type="radio" name="radiorisk" value="1">
<span class="checkmark"></span>
</label>
<label class="c">medium
<input type="radio" name="radiorisk" value="2">
<span class="checkmark"></span>
</label>
<label class="c">low
<input type="radio" name="radiorisk" value="3">
<span class="checkmark"></span>
</label>
</h5>
<app-chart></app-chart>
</div>
</div>
**
One way to do it is to add breaks between the radio buttons, and change their display from block to inline.
Added red background color for emphasis.
For example:
#spread {
background-color: #0BD44A;
padding: 15px;
margin: 15px;
border-radius: 8px;
}
.c {
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
}
.inlineRed {
display: inline;
background-color: red;
}
/* Hide the browser's default radio button */
.c input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #BEBEBE;
border-radius: 8px;
}
/* On mouse-over, add a grey background color */
.c:hover input~.checkmark {
background-color: #F5F5F5;
}
/* When the radio button is checked, add a blue background */
.c input:checked~.checkmark {
background-color: white;
}
<div id="spread">
<h1>Spread</h1>
<div class="container" align=center>
<h4>Risk-level:</h4>
<h5>
<label class="c inlineRed">high
<input type="radio" name="radiorisk" value="1">
<span class="checkmark"></span>
</label>
<br>
<label class="c inlineRed">medium
<input type="radio" name="radiorisk" value="2">
<span class="checkmark"></span>
</label>
<br>
<label class="c inlineRed">low
<input type="radio" name="radiorisk" value="3">
<span class="checkmark"></span>
</label>
</h5>
<app-chart></app-chart>
</div>
</div>
Another way to achieve this is once again include br tags between the radio buttons, and add a style to the element that is wrapping the radio buttons, in this case, the h5. Make that style align the text left, then declare its width and add padding-left to the element.
Example, with blue-border for emphasis:
#spread {
background-color: #0BD44A;
padding: 15px;
margin: 15px;
border-radius: 8px;
}
.blue-border {
border: 3px solid blue;
text-align: left;
width: 50%;
padding-left: 25%;
}
.c {
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
}
/* Hide the browser's default radio button */
.c input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #BEBEBE;
border-radius: 8px;
}
/* On mouse-over, add a grey background color */
.c:hover input~.checkmark {
background-color: #F5F5F5;
}
/* When the radio button is checked, add a blue background */
.c input:checked~.checkmark {
background-color: white;
}
<div id="spread">
<h1>Spread</h1>
<div class="container" align=center>
<h4>Risk-level:</h4>
<h5 class="blue-border">
<label class="c inlineRed">high
<input type="radio" name="radiorisk" value="1">
<span class="checkmark"></span>
</label>
<br>
<label class="c inlineRed">medium
<input type="radio" name="radiorisk" value="2">
<span class="checkmark"></span>
</label>
<br>
<label class="c inlineRed">low
<input type="radio" name="radiorisk" value="3">
<span class="checkmark"></span>
</label>
</h5>
<app-chart></app-chart>
</div>
</div>

Styling input radio buttons [duplicate]

This question already has answers here:
CSS - How to Style a Selected Radio Buttons Label?
(8 answers)
Closed 4 years ago.
I have already looked on here at how to style the input buttons I found some good answers but for some reason I can not style the inside of the button when checked. Take a look at my code maybe I did something wrong
.radio input[type='radio'] {
display: none; /*removes original button*/
}
.radio label:before { /*styles outer circle*/
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid orange;
background-color: transparent;
}
.radio label input[type='radio']:checked + label:after { /*styles inside circle*/
border-radius: 11px;
width: 12px;
height: 12px;
position: absolute;
top: 9px;
left: 10px;
content: " ";
display: block;
background-color: blue;
}
<div class="radio">
<label><input type="radio" name="gender" value="male"> Male</label>
<label><input type="radio" name="gender" value="female"> Female</label>
<label><input type="radio" name="gender" value="other"> Other</label>
</div>
You can't go backwards in css, thats why your code doesn't work
You need to add a span inside the label after your input and you can style it when the radio is checked
see code snippet:
.radio input[type='radio'] {
display: none;
/*removes original button*/
}
.radio label:before {
/*styles outer circle*/
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid orange;
background-color: transparent;
}
.radio label {
position: relative;
}
.radio label input[type='radio']:checked+span {
/*styles inside circle*/
border-radius: 11px;
width: 12px;
height: 12px;
position: absolute;
top: 1px;
left: 6px;
display: block;
background-color: blue;
}
<div class="radio">
<label><input type="radio" name="gender" value="male"> Male<span></span></label>
<label><input type="radio" name="gender" value="female"> Female<span></span></label>
<label><input type="radio" name="gender" value="other"> Other<span></span></label>
</div>
Please see below.
I have put the label behind the input field and redefined the CSS for label:before when the button is checked.
You had the input field as a child of the label but in the CSS it was approached as a sibling.
input[type='radio'] {
display: none;
}
label:before {
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid orange;
background-color: transparent;
}
input[type='radio']:checked+label:before {
position: relative;
border: 2px solid orange;
border-radius: 11px;
width: 20px;
height: 20px;
top: 5px;
content: " ";
display: inline-block;
background-color: blue;
}
<div class="radio">
<input id="male" type="radio" name="gender" value="male">
<label for="male">Male</label>
<input id="female" type="radio" name="gender" value="female">
<label for="female">Female</label>
<input id="other" type="radio" name="gender" value="other">
<label for="other">Other</label>
</div>
So your selector is incorrect and so is your html structure. You need to move your labels after your inputs (and target the input using a for attribute on the label and id on the input), then remove the first label from your selector
I have commented the css I have changed below
.radio input[type='radio'] {
display: none; /*removes original button*/
}
.radio label { /* add this so dot is relative to the label */
position: relative;
display: inline-block;
}
.radio label:before { /*styles outer circle*/
content: " ";
display: inline-block;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid orange;
background-color: transparent;
vertical-align:middle; /* add this so text is vertically centred next to ring */
}
/* remove the first label from this selector */
.radio input[type='radio']:checked + label:after { /*styles inside circle*/
border-radius: 50%;
width: 12px;
height: 12px;
position: absolute;
top: 6px; /* I have changed the top and left so this is in the centre */
left: 6px;
content: " ";
display: block;
background-color: blue;
}
<div class="radio">
<input type="radio" name="gender" value="male" id="male"><label for="male">Male</label>
<input type="radio" name="gender" value="female" id="female"><label for="female"> Female</label>
<input type="radio" name="gender" value="other" id="other"><label for="other"> Other</label>
</div>

label:before with content on radio button, weird mobile Safari behaviour

I am using this Codepen to rate a web application. In every Desktop browser it looks the same.
So the CSS looks like this:
body {
padding: 1em;
}
input {
position:absolute;
top: -2em;
clip:rect(0,0,0,0);
}
.score {
unicode-bidi: bidi-override;
direction: rtl;
text-align: center;
border: 0;
font-size: 0;
}
.score legend {
overflow: hidden;
height: 0;
}
.score label {
font-size: 80px; font-size: 9rem;
line-height: 80px; line-height: 9rem;
display: inline-block;
position: relative;
text-align: center;
width: 1.2em;
height: 1em;
overflow: hidden;
text-indent: 100%;
}
.score label:before {
content: "☆";
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
text-indent: 0;
line-height: 1em;
color: #aaa;
}
.score label:hover:before,
.score label:hover ~ label:before,
.score input:checked ~ label:before {
content: "★";
color: #ffbb04;
}
.score label:active {
position: relative;
top: 1px;
}
The HTML:
<fieldset class="score">
<legend>Score:</legend>
<input type="radio" id="score-5" name="score" value="5"/>
<label title="5 stars" for="score-5">5 stars</label>
<input type="radio" id="score-4" name="score" value="4"/>
<label title="4 stars" for="score-4">4 stars</label>
<input type="radio" id="score-3" name="score" value="3"/>
<label title="3 stars" for="score-3">3 stars</label>
<input type="radio" id="score-2" name="score" value="2"/>
<label title="2 stars" for="score-2">2 stars</label>
<input type="radio" id="score-1" name="score" value="1"/>
<label title="1 stars" for="score-1">1 stars</label>
</fieldset>
The problem: When i view this site on mobile Safari, the stars are displayed doubled, with a bit distance in between. Like this Screenshot:

Label still taking up space when absolutely positioned

I have a javascript validation plugin which is used in too many places to replace. It inserts a label after the input it is validating. Current unvalidated mark up & image below:
Would have loved to show images but I guess I can't yet.
<div id="gender-select">
<label id="gender">Gender</label>
<input class="male required radioBtn gender-hide" type="radio" name="gender" value="1" id="male">
<label for="male"></label>
<input class="female required radioBtn gender-hide" type="radio" name="gender" value="2" id="female">
<label for="female"></label>
</div>
This is the code after the validation is run and the input for gender is invalid.
<div id="gender-select">
<label id="gender">Gender</label>
<input class="male required radioBtn gender-hide error" type="radio" name="gender" value="1" id="male">
<label for="gender" generated="true" class="error">Mandatory</label>
<label for="male"></label>
<input class="female required radioBtn gender-hide error" type="radio" name="gender" value="2" id="female">
<label for="female"></label>
</div>
Current CSS
#join-page #form-content input[type="radio"] {
position: absolute;
left: -8000px;
}
#join-page #form-content input {
background: #f4f4f4;
height: 40px;
border: none;
padding: 0 2%;
width: 100%;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
-moz-background-clip: padding;
-webkit-background-clip: padding-box;
background-clip: padding-box;
font-size: 16px;
color: #a2a2a2;
}
#join-page #form-content input[type="radio"] + label {
background: url('/App_Presentation/responsive/OpinionMilesClub/img/omc-genderRadio.png') 0px 0px no-repeat;
cursor: pointer;
height: 40px;
width: 30px;
}
#join-page #form-content label.error[for="gender"] {
position: absolute;
top: -11px;
left: 0px;
}
#join-page #form-content input[type="radio"] + label[for="female"] {
margin-left: -4px;
background: url('/App_Presentation/responsive/OpinionMilesClub/img/omc-genderRadio.png') 31px 0px;
}
I don't understand why the label still takes up space? Anyone have any idea...