How to overlap the input with div? - html

I am trying to make custom radio button and I want to overlap the <input type="radio" into div.
input[type="radio"]:checked+div {
color: #fff;
background-color: rgb(13, 50, 218);
}
<input type="radio" name="favorite_pet" value="Parrot">
<div>Parrot</div><br>
<input type="radio" name="favorite_pet" value="Dog">
<div>Dog</div><br>
The main goal is to select by text div instead of radio button.
Current Layout:
Expected Layout:
How can I select the div instead of radio button with overlapping?

You should use a label with for and hide the radio button
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
color: #fff;
background-color: rgb(13, 50, 218);
}
<input type="radio" name="favorite_pet" value="Parrot" id="rb1">
<label for="rb1">Parrot</label><br>
<input type="radio" name="favorite_pet" value="Dog" id="rb2">
<label for="rb2">Dog</label><br>
If you have issues giving them ids, you can wrap the whole thing in a label and use another element for the text like a span.
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + span {
color: #fff;
background-color: rgb(13, 50, 218);
}
<label>
<input type="radio" name="favorite_pet" value="Parrot">
<span>Parrot</span>
</label><br>
<label>
<input type="radio" name="favorite_pet" value="Dog">
<span>Dog</span>
</label><br>

Wrap the text in label elements:
input[type="radio"]:checked+span {
color: #fff;
background-color: rgb(13, 50, 218);
}
input {
display: none;
}
span {
display: block;
}
<label><input type="radio" name="favorite_pet" value="Parrot">
<span>Parrot</span></label><br>
<label><input type="radio" name="favorite_pet" value="Dog">
<span>Dog</span></label><br>
Then change your divs to spans since the divs can't exist in labels.

Wrap input inside label.
label, span{
font-family: sans-serif;
display: block;
margin-bottom: 10px;
font-size: 18px;
}
input[type="radio"] {
position: absolute;
visibility: hidden;
}
input[type="radio"]:checked + span{
background: blue;
color: #fff;
}
input[type="radio"] + span:before {
content: '\2610';
margin-right: 10px;
font-size: 20px;
}
input[type="radio"]:checked + span:before {
content: '\2611';
}
<label>
<input type="radio" name="group">
<span>Parrot</span>
</label>
<label>
<input type="radio" name="group">
<span>Dog</span>
</label>

Related

Custom radio selector not functioning

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>

cannot select option when multiple radio buttons are used

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>

Use images instead of radio buttons (HTML/CSS)

I have a div of radio buttons:
<div class="radio-line">
<label>
<img src="img/black.jpeg" />
<input type="radio" name="choice1" value="1"/>
</label>
<label>
<img src="img/blue.jpg" />
<input type="radio" name="choice1" value="2"/>
</label>
</div>
And here's my CSS:
img {
width: 20vw;
height: 20vw;
padding: 2vw;
}
input[type=radio] {
display: none;
}
img:hover {
opacity: 0.4;
cursor: pointer;
}
img:active {
opacity:0.4;
cursor: pointer;
}
input[type=radio]:checked + img {
border: 20px solid rgb(228, 207, 94);
}
When I hover over a square, the square opacity will change, but nothing happens when I click on the square (when it should be "checked", a border should surround it). I've been playing around with different divs and labels, but nothing is working.
img {
width: 20vw;
height: 20vw;
padding: 2vw;
}
input[type=radio] {
display: none;
}
img:hover {
opacity:0.6;
cursor: pointer;
}
img:active {
opacity:0.4;
cursor: pointer;
}
input[type=radio]:checked + img {
border: 20px solid rgb(228, 207, 94);
}
<label>
<img src="img/black.jpeg" />
<input type="radio" name="choice-1" value="1"/>
</label>
<label>
<img src="img/blue.jpg" />
<input type="radio" name="choice-1" value="1"/>
</label>
You can used the :checked pseudo-class and sibling selectors to achieve the effect.
So first thing is to move into a structure where inputs are siblings of the labels instead of descendants, and include a "for" attribute that links them
img {
width: 20vw;
height: 20vw;
padding: 2vw;
}
input[type=radio] {
display: none;
}
img:hover {
opacity:0.6;
cursor: pointer;
}
img:active {
opacity:0.4;
cursor: pointer;
}
input[type=radio]:checked + label > img {
border: 20px solid rgb(228, 207, 94);
}
<input type="radio" name="choice" id="choose-1" value="1"/>
<label for="choose-1">
<img src="https://placehold.it/200/200/" />
</label>
<input type="radio" name="choice" id="choose-2" value="2"/>
<label for="choose-2">
<img src="https://placehold.it/200/200/" />
</label>
You can use jQuery to set an onClick function on the images. Assign the images a custom class e.g: radioImage and in jQuery:
$( ".radioImage" ).click(function() {
$( this ).css("border", "20px solid rgb(228, 207, 94)");
});
You should set for property of label to the name of input you want.
In this case, it maybe looks like this:
<input type="radio" name="choice-1" id="choice-1" value="1"/>
<label for="choice-1">
<img src="img/black.jpeg" />
</label>
<input type="radio" name="choice-1" id="choice-2" value="1"/>
<label for="choice-2">
<img src="img/blue.jpg" />
</label>

Radio button group checked property css error

I made the tabs using radio button trick like this
Html
<div class="tabs">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>
<input type="radio" id="tab-2" name="tab-group-1">
<label for="tab-2">Tab Two</label>
</div>
then css:
.tabs label {
margin-left: -1px;
border: 1px solid;
padding: 0px 80px 0px 80px;
background: #ddd;
}
.tabs input[type=radio]:checked ~ .tabs label {
background: #fff;
border-bottom: 1px solid #fff;
}
The problem here is the style of label does not change when radio button is chosen. Can anyone help me to explain it, thanks
btw: I run code using IE 8. Does it support checked properties
Check following is the solution. Replace ~ with +.
.tabs label {
margin-left: -1px;
border: 1px solid;
padding: 0px 80px 0px 80px;
background: #ddd;
}
input[type="radio"]:checked + label {
background: #fff;
border-bottom: 1px solid #fff;
}
<div class="tabs">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>
<input type="radio" id="tab-2" name="tab-group-1"/>
<label for="tab-2">Tab Two</label>
</div>
Remove the tilde (~) sign and replace with + and the .tabs
.tabs input[type=radio]:checked + label {
You could also make a jQuery approach:
$('.tabs input[type="radio"]').click(function() {
if($('#tab-1').is(':checked')) {
$('#tab-1').addClass("someClass")
}
});

Input with both Radio and checkbox characteristics

I am looking to design a input field such that it will obey radio buttons characteristics as well as checkbox characteristics.
The requirement is that there are some bars, to which when I click, a text "hello" will be shown adjacent to that bar. and when I click on another bar, the "hello" text from the previous bar should hide and the clicked bar's "hello" text should be visible.
This is happening in this fiddle
label {
display: block;
width: 100px;
height: 20px;
background-color: cornflowerblue;
position: relative;
margin-top: 10px;
cursor: pointer;
}
span {
display: none;
}
input[type="radio"] {
visibility: hidden;
position: absolute;
pointer-events: none;
}
:checked + span {
display: block;
position: absolute;
left: 100%;
}
But what I want is each bar should act as toggle button to show/hide the "hello" text. I can achieve this if I use checkboxes instead of radio button but I will lose the behavior which I achieved above using radio buttons.
I am looking for a pure css solution.
Thanks in advance.
EDIT:
This is what I want. Fiddle
But using only css.
If you can afford to edit html and really can't afford to use any javascript at all you can do it by using <input type='reset' />, but this is a hacky way to handle such functionality - javascript should always be your first choice for such tasks.
label {
display: block;
width: 100px;
height: 20px;
background-color: cornflowerblue;
position: relative;
margin-top: 10px;
cursor: pointer;
}
span {
display: none;
}
input[type="radio"] {
visibility: hidden;
position: absolute;
pointer-events: none;
}
input[type="reset"] {
position: absolute;
border:0;padding:0;margin:0;background:transparent;
width:100%;
height:100%;
display: none;
}
:checked + span {
display: block;
position: absolute;
left: 100%;
}
:checked + span + input[type="reset"] {
display: block;
opacity:0;
}
<form>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
</form>