I've created this custom radio selector for my site and the first radio selector is working but the 2nd field isn't selecting. Is there another class that I need to create or have each input named a different ID so they don't conflict with one another? I'm trying to create multiple options for a customer to select an option.
input[type=radio] {
position: absolute;
visibility: hidden;
display: none;
}
label {
display: inline-block;
cursor: pointer;
font-weight: bold;
padding: 5px 20px;
}
input[type=radio]:checked+label {
color: #fff;
background: #444;
border-radius: 5px;
}
label+input[type=radio]+label {
border-left: solid 3px #444;
}
.radio-group {
display: inline-block;
overflow: hidden;
}
<div class="radio-group">
<input type="radio" id="lessthan6months" name="timeline"><label for="lessthan6months">6 mo</label>
<input type="radio" id="6to12months" name="timeline"><label for="6to12months">6-12 mo</label>
<input type="radio" id="12to24months" name="timeline"><label for="12to24months">12-24 mo</label>
<div class="line"></div>
</div>
<div class="radio-group">
<input type="radio" id="tradein_y" name="tradein" value="Yes"><label for="trade in yes">Yes</label>
<input type="radio" id="tradein_n" name="tradein" value="No"><label for="trade in no">No</label>
<div class="line"></div>
</div>
The for of the label content has to match the id of the input.
This will work, like this:
input[type=radio] {
position: absolute;
visibility: hidden;
display: none;
}
label {
display: inline-block;
cursor: pointer;
font-weight: bold;
padding: 5px 20px;
}
input[type=radio]:checked+label {
color: #fff;
background: #444;
border-radius: 5px;
}
label+input[type=radio]+label {
border-left: solid 3px #444;
}
.radio-group {
display: inline-block;
overflow: hidden;
}
<div class="radio-group">
<input type="radio" id="lessthan6months" name="timeline"><label for="lessthan6months">6 mo</label>
<input type="radio" id="6to12months" name="timeline"><label for="6to12months">6-12 mo</label>
<input type="radio" id="12to24months" name="timeline"><label for="12to24months">12-24 mo</label>
<div class="line"></div>
</div>
<div class="radio-group">
<input type="radio" id="tradein_y" name="tradein" value="Yes"><label for="tradein_y">Yes</label>
<input type="radio" id="tradein_n" name="tradein" value="No"><label for="tradein_n">No</label>
<div class="line"></div>
</div>
Related
I am trying to create two radio buttons that change color once the user selects them. However, once I select one of them they don't apply the supposed CSS:
.poller-nature-radio input[type="radio"] {
opacity: 0;
position: fixed;
width: 0;
}
#id_poller_nature label {
display: inline-block;
background-color: #ddd;
padding: 10px 20px;
font-family: sans-serif, Arial;
font-size: 16px;
border: 2px solid #444;
border-radius: 4px;
}
#id_poller_nature {
display: flex;
}
#id_poller_nature li {
list-style-type: none;
}
#id_poller_nature input[type="radio"]:checked + label {
background-color:#bfb;
border-color: #4c4;
}
<div class="fieldWrapper poller-nature-radio">
<label for="id_poller_nature_0">Nothing changes on select:</label>
<ul id="id_poller_nature">
<li><label for="id_poller_nature_0"><input type="radio" name="poller_nature" value="Choice 1" required="" id="id_poller_nature_0">
Question</label>
</li>
<li><label for="id_poller_nature_1"><input type="radio" name="poller_nature" value="Choice 2" required="" id="id_poller_nature_1">
Information</label>
</li>
</ul>
</div>
Since you used + css selector, the label should come as a sibling and come after the input. Check the below snippet.
.poller-nature-radio input {
position: fixed;
visibility: hidden;
}
#id_poller_nature label {
display: inline-block;
background-color: #ddd;
padding: 10px 20px;
font-family: sans-serif, Arial;
font-size: 16px;
border: 2px solid #444;
border-radius: 4px;
}
#id_poller_nature {
display: flex;
}
#id_poller_nature li {
list-style-type: none;
}
#id_poller_nature input[type="radio"]:checked + label {
background-color:#bfb;
border-color: #4c4;
}
<div class="fieldWrapper poller-nature-radio">
<label for="id_poller_nature_0">Click the button to see the change:</label>
<ul id="id_poller_nature">
<li>
<input
type="radio"
name="poller_nature"
value="Choice 1"
required=""
id="id_poller_nature_0"
/>
<label for="id_poller_nature_0"> Question</label>
</li>
<li>
<input
type="radio"
name="poller_nature"
value="Choice 2"
required=""
id="id_poller_nature_1"
/>
<label for="id_poller_nature_1"> Information</label>
</li>
</ul>
</div>
.poller-nature-radio input[type="radio"] {
opacity: 0;
position: fixed;
width: 0;
}
#id_poller_nature label {
display: inline-block;
background-color: #ddd;
padding: 10px 20px;
font-family: sans-serif, Arial;
font-size: 16px;
border: 2px solid #444;
border-radius: 4px;
}
#id_poller_nature {
display: flex;
}
#id_poller_nature li {
list-style-type: none;
}
#id_poller_nature_1:checked {
background-color:#bfb;
border-color: #4c4;
}
<div class="fieldWrapper poller-nature-radio">
<label for="id_poller_nature_0">Nothing changes on select:</label>
<ul id="id_poller_nature">
<li><label for="id_poller_nature_0"><input type="radio" name="poller_nature"
value="Choice 1" required="" id="id_poller_nature_0">
Question</label>
</li>
<li><label for="id_poller_nature_1"><input type="radio" name="poller_nature" value="Choice 2" required="" id="id_poller_nature_1">
Information</label>
</li>
</ul>
</div>
Try to access the input element using the individual id then handle the :check state
Change your HTML like this.
<ul id="id_poller_nature">
<li>
<input type="radio" name="poller_nature" value="Choice 1" required="" id="id_poller_nature_0">
<label for="id_poller_nature_0">`enter code
here`Question</label>
</li>
I have used two radio buttons to select the gender of applicant and gender of applicant's child.But both of them does not works.Can anyone help me with this?
My codes are given below
input[type=radio] {
position: absolute;
visibility: hidden;
display: none;
}
label {
color: #9a929e;
display: inline-block;
cursor: pointer;
font-weight: bold;
padding: 5px 20px;
}
input[type=radio]:checked + label {
color: #ccc8ce;
background: #675f6b;
}
<input type="radio" id="option-one" name="selector" checked>
<label for="option-one">One</label>
<input type="radio" id="option-two" name="selector">
<label for="option-two">Two</label>
<input type="radio" id="option-one" name="selector_one" checked>
<label for="option-one">One</label>
<input type="radio" id="option-two" name="selector_one">
<label for="option-two">Two</label>
An "id" can be used only once in an html page. Each "id" must be unique.
input[type=radio] {
position: absolute;
visibility: hidden;
display: none;
}
label {
color: #9a929e;
display: inline-block;
cursor: pointer;
font-weight: bold;
padding: 5px 20px;
}
input[type=radio]:checked + label {
color: #ccc8ce;
background: #675f6b;
}
<input type="radio" id="option-one" name="selector" checked>
<label for="option-one">One</label>
<input type="radio" id="option-two" name="selector">
<label for="option-two">Two</label>
<input type="radio" id="option-one2" name="selector_one" checked>
<label for="option-one2">One</label>
<input type="radio" id="option-two2" name="selector_one">
<label for="option-two2">Two</label>
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>
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>
I'm making a tab system only with CSS using :target and :checked pseudoclasses, but I have an anchor inside the label, and the label doesn't trigger the :checked.
If you click in the anchor, the :checked doesn't trigger because the click is in the <a> tag, but is inside a <label> that must trigger the radio button. If you click on the border of the tab, you'll see how it triggers the :checked, but not the anchor, so the :target can't be triggered.
Here you are my code, more understandable than the words:
a {
text-decoration: none;
}
.tabs {
position: relative;
}
input {
display: none;
}
.tabs .tab label {
text-decoration: none;
border: 1px solid black;
padding: 2px;
display: inline-block;
top: 2px;
position: relative;
cursor: pointer;
}
.tabs .tab input:checked + label {
background-color: white;
border-bottom: 0;
padding: 4px 2px;
top: 1px;
}
.contents {
border: 1px solid black;
background-color: white;
}
.contents .content {
display: none;
padding: 20px;
}
.contents .content:target {
display: block;
}
<div class="tabs">
<span class="tab">
<input type="radio" name="ch" id="check1">
<label for="check1">
Tab 1
</label>
</span>
<span class="tab">
<input type="radio" name="ch" id="check2">
<label for="check2">
Tab 2
</label>
</span>
<span class="tab">
<input type="radio" name="ch" id="check3">
<label for="check3">
Tab 3
</label>
</span>
</div>
<div class="contents">
<div class="content" id="tab1">Contenido 1</div>
<div class="content" id="tab2"><strong>Contenido 2</strong></div>
<div class="content" id="tab3"><em>Contenido 3</em></div>
</div>
Is there a way to combine :checked and :target pseudoclasses to achieve a complete tab system only with CSS?
Thank you.
EDIT
Here you are the snippet without anchor. Obviously the :target will not be triggered:
a {
text-decoration: none;
}
.tabs {
position: relative;
}
input {
display: none;
}
.tabs .tab label {
text-decoration: none;
border: 1px solid black;
padding: 2px;
display: inline-block;
top: 2px;
position: relative;
cursor: pointer;
}
.tabs .tab input:checked + label {
background-color: white;
border-bottom: 0;
padding: 4px 2px;
top: 1px;
}
.contents {
border: 1px solid black;
background-color: white;
}
.contents .content {
display: none;
padding: 20px;
}
.contents .content:target {
display: block;
}
<div class="tabs">
<span class="tab">
<input type="radio" name="ch" id="check1">
<label for="check1">
Tab 1
</label>
</span>
<span class="tab">
<input type="radio" name="ch" id="check2">
<label for="check2">
Tab 2
</label>
</span>
<span class="tab">
<input type="radio" name="ch" id="check3">
<label for="check3">
Tab 3
</label>
</span>
</div>
<div class="contents">
<div class="content" id="tab1">Contenido 1</div>
<div class="content" id="tab2"><strong>Contenido 2</strong></div>
<div class="content" id="tab3"><em>Contenido 3</em></div>
</div>
When you use input:checked, :target is not efficient cause this event is not triggered at all.
You need to put your input ahead in the flow so you can use the selector ~ to select any sibblings and their children following in the flow of the document:
example
a {
text-decoration: none;
}
.tabs {
position: relative;
}
input {
display: none;
}
.tabs .tab label {
text-decoration: none;
border: 1px solid black;
padding: 2px;
display: inline-block;
top: 2px;
position: relative;
cursor: pointer;
}
#check1:checked ~ .tabs label[for="check1"],
#check2:checked ~ .tabs label[for="check2"],
#check3:checked ~ .tabs label[for="check3"] {
background-color: white;
border-bottom: 0;
padding: 4px 2px;
top: 1px;
}
.contents {
border: 1px solid black;
background-color: white;
}
.contents .content {
display: none;
padding: 20px;
}
#check1:checked ~ .contents #tab1,
#check2:checked ~ .contents #tab2,
#check3:checked ~ .contents #tab3 {
display: block;
}
<!-- begin hidden inputs for CSS tabs purpose -->
<input type="radio" name="ch" id="check1">
<input type="radio" name="ch" id="check2">
<input type="radio" name="ch" id="check3">
<!-- End hidden inputs for CSS tabs purpose -->
<div class="tabs">
<span class="tab">
<label for="check1">
Tab 1
</label>
</span>
<span class="tab">
<label for="check2">
Tab 2
</label>
</span>
<span class="tab">
<label for="check3">
Tab 3
</label>
</span>
</div>
<div class="contents">
<div class="content" id="tab1">Contenido 1</div>
<div class="content" id="tab2"><strong>Contenido 2</strong>
</div>
<div class="content" id="tab3"><em>Contenido 3</em>
</div>
</div>
This behavior is specified in HTML5 (emphasis mine):
The activation behavior of a label element for events
targeted at interactive content descendants of a label
element, and any descendants of those interactive content
descendants, must be to do nothing.
Since the link is interactive content, clicking on it won't check the labeled radio input.