I have styled the following radio buttons so that the actual radio is display:none and the label is an inline-block with a background-color.
I am struggling to find how to show a different background colour on the checked radio button.
Obviously its a simple thing that I am missing:
input[type="radio"] {
display: none;
}
label + checked {
background-color: #ffcb00;
display: inline-block;
width: 8vw;
height: 4vw;
line-height: 4vw;
border-radius: 0.5vw;
}
label {
background-color: #f2f2f2;
display: inline-block;
width: 8vw;
height: 4vw;
line-height: 4vw;
border-radius: 0.5vw;
}
<label>
<input type="radio" name="amount" value="100" checked />
<b>£100</b>
</label>
<label>
<input type="radio" name="amount" value="250" />
<b>£250</b>
</label>
<label>
<input type="radio" name="amount" value="500" />
<b>£500</b>
</label>
<label>
<input type="radio" name="amount" value="1000" />
<b>£1,000</b>
</label>
<label>
<input type="radio" name="amount" value="1500" />
<b>£1,500</b>
</label>
<br>
<br>
<label>
<input type="radio" name="term" value="30" checked />
<b>30 days</b>
</label>
<label>
<input type="radio" name="term" value="60" />
<b>60 days</b>
</label>
<label>
<input type="radio" name="term" value="90" />
<b>90 days</b>
</label>
<label>
<input type="radio" name="term" value="180" />
<b>180 days</b>
</label>
<label>
<input type="radio" name="term" value="360" />
<b>360 days</b>
</label>
Since the label is the parent of the input[type="checkbox"] you can't select it directly in CSS. One option of fixing this would be the following. Add an extra div to the label (.background in my example) and use that to change the background color. Of course there are many more options to fix this, but this is probably the simplest one.
input[type="radio"] {
display: none;
}
.background{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: auto;
height: auto;
background-color: #f2f2f2;
}
input[type="radio"]:checked + .background{
background-color: #ffcb00;
}
label {
position: relative;
display: inline-block;
width: 8vw;
height: 4vw;
line-height: 4vw;
border-radius: 0.5vw;
}
label *{
position: relative;
}
<label>
<input type="radio" name="term" value="30" />
<div class="background"></div>
<b>30 days</b>
</label>
<label>
<input type="radio" name="term" value="30" checked />
<div class="background"></div>
<b>30 days</b>
</label>
You're using the + selector incorrectly. a + b means select any element b immediately following an a, for example:
<a></a>
<b></b>
By changing your HTML so that labels immediately follow radio buttons, we're able to use the pseudo-selector :checked to detect when a radio button is checked and style the label appropriately.
Note that I also added for attributes to your labels and ids to the radio buttons to create an association between the two.
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
background-color: #ffcb00;
display: inline-block;
width: 8vw;
height: 4vw;
line-height: 4vw;
border-radius: 0.5vw;
}
label {
background-color: #f2f2f2;
display: inline-block;
width: 8vw;
height: 4vw;
line-height: 4vw;
border-radius: 0.5vw;
}
<input type="radio" name="amount" id="amount1" value="100" />
<label for="amount1">£100</label>
<input type="radio" name="amount" id="amount2" value="250" />
<label for="amount2">£250</label>
Related
My checkboxes are autogenerated as a list so I can not change that and I have tried do modify css code I have found while searching but I can not get it to work properly.
I want to make my checkboxes look like this but instead they look like this
.chk-btn>li {
margin: 4px;
background-color: #EFEFEF;
border-radius: 4px;
border: 1px solid #D0D0D0;
overflow: auto;
float: left;
}
.chk-btn>li label {
float: left;
width: 4.0em;
}
.chk-btn>li label {
text-align: center;
padding: 3px 0px;
display: block;
}
.chk-btn>li label input {
position: absolute;
top: -20px;
}
.chk-btn>li input:checked {
background-color: #911;
color: #fff;
}
<div class="green-content">
<form>
<div>
<ul class="chk-btn">
<li>
<label for="con-1">
choice1
</label>
<input id="con-1" name="con" type="checkbox" value="1">
</li>
<li>
<label for="con-2">
choice2
</label>
<input id="con-2" name="con" type="checkbox" value="2">
</li>
<li>
<label for="con-3">
choice3
</label>
<input id="con-3" name="con" type="checkbox" value="3">
</li>
<li>
<label for="con-4">
choice4
</label>
<input id="con-4" name="con" type="checkbox" value="4">
</li>
</ul>
</div>
</form>
</div>
How do I get it right?
And also have a them change to darker color when hover?
Hiding the input and usage of label and + selector based on input checked or hover state is really helpful for such a scenario. I have removed css which was not needed and also switched position of input and label tags with each other for + to work like so :-
.chk-btn>li {
display:inline-flex;
margin: 4px;
background-color: #EFEFEF;
border-radius: 4px;
border: 1px solid #D0D0D0;
overflow: auto;
}
.chk-btn>li label {
text-align: center;
padding: 3px 0px;
width: 4.0em;
}
.chk-btn>li input {
display: none;
}
.chk-btn>li input:checked + label {
background-color: #911;
color: #fff;
}
.chk-btn>li input:hover + label {
background-color: #580716;
color: #fff;
}
<div class="green-content">
<form>
<div>
<ul class="chk-btn">
<li>
<input id="con-1" name="con" type="checkbox" value="1">
<label for="con-1">
choice1
</label>
</li>
<li>
<input id="con-2" name="con" type="checkbox" value="2">
<label for="con-2">
choice2
</label>
</li>
<li>
<input id="con-3" name="con" type="checkbox" value="3">
<label for="con-3">
choice3
</label>
</li>
<li>
<input id="con-4" name="con" type="checkbox" value="4">
<label for="con-4">
choice4
</label>
</li>
</ul>
</div>
</form>
</div>
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>
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>
.big{ width: 2.5em; height: 1.5em; }
<p>
<input type="radio" name="r1" value="a" checked="checked" class="big" />
<span>Closed</span>
<input type="radio" name="r1" value="b" class="big" />
<span>Pending</span>
</p>
Now the text is coming down near the Radio button but it want the text should be in the center.
check this css
.big{ width: 2.5em;
display: inline-block;
vertical-align: top;
}
.align{height: 1.5em;}
.text-big{ font-size:1.2em}
.span{
display: inline-block;
vertical-align: top;
margin-right: 3%;
}
https://jsfiddle.net/tkp2007p/1/
You can use vertical align for this.
.big{ width: 2.5em; height: 1.5em; }
p span,
p .big{vertical-align: middle}
<p>
<input type="radio" name="r1" value="a" checked="checked" class="big" />
<span>Closed</span>
<input type="radio" name="r1" value="b" class="big" />
<span>Pending</span>
</p>
.align{line-height: 1.5em;}
.big{ width: 2.5em; }
.text-big{ font-size:1.2em}
<p>
<span class="align">
<input type="radio" name="r1" value="a" checked="checked" class="big" />
<span class="text-big">Closed</span>
<input type="radio" name="r1" value="b" class="big" />
<span class="text-big">Pending</span>
</span>
</p>
Here I wrapped the entire radio and its text in a span with class align
And added line-height to that class.
try this
<p>
<input type="radio" name="r1" value="a" checked="checked" class="big" />
<span>Closed</span>
<input type="radio" name="r1" value="b" class="big" />
<span>Pending</span>
</p>
css:
.big{ width: 2.5em; height: 1.5em; }
.big+span{
font-size:15px;
}
.big ,.big+span{
vertical-align:middle;
}
https://jsfiddle.net
updated as per your request:
<p>
<input type="radio" name="r1" value="a" checked="checked" class="big" id="r1" />
<label for="r1">Closed</label>
<input type="radio" name="r1" value="b" class="big" id="r2"/>
<label for="r2">Pending</label>
</p>
css:
/* COMMON RADIO STYLES */
input[type=radio]{
/* hide original inputs */
visibility: hidden;
position: absolute;
}
input[type=radio] + label{
cursor:pointer;
}
input[type=radio] + label:before{
height:16px;
margin-right: 4px;
content: " ";
display:inline-block;
vertical-align: baseline;
transition: 0.3s;
border:1px solid #ccc;
border-radius:10px;
box-shadow: inset 0 -3px 6px #e4e4e4;
transition: 0.3s;
}
/* CUSTOM RADIO STYLES */
input[type=radio] + label:before{
border-radius:50%;
width:16px;
}
/* CHECKED */
input[type=radio]:checked + label:before{
box-shadow: inset 0 -1px 3px #e4e4e4, inset 0 0 1px #222, inset 0 0 0 3px yellow;
background:#000;
}
output:
https://jsfiddle.net
I am trying to align the button radios on my page and it seems that one of the buttons (Gender) is highly indented. The radio for the M (Gender) is at the center and the other button is at the far right. I've been changing the values in css but it doesn't align. How do I align them? Anyone help? My css is combined and I got most of help here from SO.
form {
width: 80%;
margin: 0 auto;
}
label,
input {
display: inline-block;
}
label {
width: 30%;
}
label + input {
width: 30%;
margin: 0 10% 0 1%;
}
.label-align input[type="checkbox"] {
cursor: pointer;
margin-top: 6px;
}
.label-align span {
margin: 0 0 10px 10px;
display: block;
}
.left {
float: left;
}
<div id="register" class="container">
<form action="" method="post">
<label>*First Name:</label>
<input type="text">
<br>
<label>*Last Name:</label>
<input type="text">
<br>
<label>*Birth Date:</label>
<input type="text">
<br>
<label>*Gender:</label>
<input type="radio" name="gender" value="male" checked>M
<input type="radio" name="gender" value="female" checked>F
<br>
<label>*Email:</label>
<input type="text">
<br>
<label>*Password:</label>
<input type="text">
<br>
<label>*Re-enter Password:</label>
<input type="text">
<br>
<button class="button" type="submit" value="Submit" onclick="" style="vertical-align:middle"><span>Submit</span>
</button>
</form>
</div>
This is happening because of this the following style.
label + input {
width: 30%;
margin: 0 10% 0 1%;
}
All inputs following a label will gain a width of 30%.
The reason only the first radio-button is affected by this is because it's the one that's adjacent to the gender label.
You could change this rule so it only affects input boxes for example.
label + input[type='text'] {
width: 30%;
margin: 0 10% 0 1%;
}
Snippet example
form {
width: 80%;
margin: 0 auto;
}
label,
input {
display: inline-block;
}
label {
width: 30%;
}
label + input[type='text'] {
width: 30%;
margin: 0 10% 0 1%;
}
.label-align input[type="checkbox"] {
cursor: pointer;
margin-top: 6px;
}
.label-align span {
margin: 0 0 10px 10px;
display: block;
}
.left {
float: left;
}
<div id="register" class="container">
<form action="" method="post">
<label>*First Name:</label>
<input type="text">
<br>
<label>*Last Name:</label>
<input type="text">
<br>
<label>*Birth Date:</label>
<input type="text">
<br>
<label>*Gender:</label>
<input type="radio" name="gender" value="male" checked>M
<input type="radio" name="gender" value="female" checked>F
<br>
<label>*Email:</label>
<input type="text">
<br>
<label>*Password:</label>
<input type="text">
<br>
<label>*Re-enter Password:</label>
<input type="text">
<br>
<button class="button" type="submit" value="Submit" onclick="" style="vertical-align:middle"><span>Submit</span>
</button>
</form>
</div>
label + input {
width: 30%;
margin: 0 10% 0 1%;
}
Remove width :30%;
Please add following css,
input[type="radio"] {
margin: 4px 4px 4px 4px;
width: 2%;
}
In your code add one class like this
<input type="radio" name="gender" class="gender" value="male" checked>M
<input type="radio" name="gender" value="female" checked>F<br>
in your css style property add this class gender property like this.
.gender{
width: 0px !important;
margin: 0px 0px 0px 4px !important;
}