Image instead of radio button [closed] - html

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm probably making a small mistake, but I tried to follow the answers to the previously asked questions on the subject.
I'm trying to replace my radio button with an image. The image shows perfectly and I can click the image to select the radio button. But the mark-up won't hide my radio button. It does when I hardcode it in the HTML code, but well... let's not go there!
.radioImg {
display:inline;
width:30%;
}
.radioImg input[type=radio] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
/* IMAGE STYLES */
.radioImg input[type=radio] + img {
cursor: pointer;
}
/* CHECKED STYLES */
.radioImg input[type=radio]:checked + img {
outline: 2px solid #f00;
}
<form action= "{{ url_for('mechanics') }}" method="POST">
<fieldset>
<legend>What type of system do you want?</legend>
<br />
<label class="radioImg">
<input checked="checked" name="system" type="radio" value="type1">
<img src="static/type1.jpg">
</label>
<label class="radioImg">
<input name="system" type="radio" value="type2">
<img src="static/type2.jpg">
</label>
<label class="radioImg">
<input name="system" type="radio" value="type3">
<img src="static/type3.jpg">
</label>
<br />
<br />
<button type="submit" value="Next">Next</button>
</fieldset>
</form>
I tried to follow: Use images instead of radio buttons,
but the radio buttons still won't hide as you can see:

The weird thing is that when I try to replicate your problem with your code, I don't run into the same problem. I think you have some extra CSS somewhere affecting your HTML.
.radioImg {
display: inline;
width: 30%;
}
.radioImg input[type=radio] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
/* IMAGE STYLES */
.radioImg input[type=radio]+img {
cursor: pointer;
}
/* CHECKED STYLES */
.radioImg input[type=radio]:checked+img {
outline: 2px solid #f00;
}
<form action="{{ url_for('mechanics') }}" method="POST">
<fieldset>
<legend>What type of system do you want?</legend>
<br />
<label class="radioImg">
<input checked="checked" name="system" type="radio" value="type1">
<img src="static/type1.jpg">
</label>
<label class="radioImg">
<input name="system" type="radio" value="type2">
<img src="static/type2.jpg">
</label>
<label class="radioImg">
<input name="system" type="radio" value="type3">
<img src="static/type3.jpg">
</label>
<br />
<br />
<button type="submit" value="Next">Next</button>
</fieldset>
</form>

Apparently, I should clear my cache every time I change the markup of /static/... files.

This is what you were looking for
/* HIDE RADIO */
[type=radio] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
/* IMAGE STYLES */
[type=radio] + img {
cursor: pointer;
}
/* CHECKED STYLES */
[type=radio]:checked + img {
outline: 2px solid red;
}
<label>
<input type="radio" name="test" value="small" checked>
<img src="http://placehold.it/40x60/0bf/fff&text=A">
</label>
<label>
<input type="radio" name="test" value="big">
<img src="http://placehold.it/40x60/b0f/fff&text=B">
</label>

Related

Pure CSS Way to style radio button group when none of the radio buttons are checked?

Is there a pure CSS way to style a group of radio buttons such that they look a certain way when none of them are checked? This would be the default state (for legal reasons) of the radio buttons for a couple questions.
Overall, what I'm trying to do is have two conditions:
If none of the radio buttons are checked, show all three radio buttons in full color/opacity
If one of the radio buttons is checked, show that button in full color/opacity, but the other two slightly dulled/grayed.
It is easy to do condition #2 using the :checked selector. But that by itself will leave all three dulled/grayed in their default state. See the snippet for a very basic example.
I realize this can be done with javascript, and if I need to go that route I will. Just thought I'd see if there was a pure CSS way to accomplish it.
input[type=radio]:not(:checked),
input[type=radio]:not(:checked)+label {
opacity: 0.2;
}
<form>
<input type="radio" name="Question" value="Answer1" /> <label>Answer 1</label>
<input type="radio" name="Question" value="Answer2" /> <label>Answer 2</label>
<input type="radio" name="Question" value="Answer3" /> <label>Answer 3</label>
</form>
I think you could do this in JavaScript. Alone, I don't believe you could do this in CSS because what you need is for it to check if the radio-buttons are selected and you can't have that kind of logic in CSS.
Here is some code I have pulled together in JQuery (the easiest way to do it):
$(function() {
$("#radio1").click(function() {
$("#radio2Text, #radio3Text, #radio2, #radio3").css("opacity", "0.2");
$("#radio1, #radio1Text").css("opacity", "1");
});
$("#radio2").click(function() {
$("#radio1Text, #radio3Text, #radio1, #radio3").css("opacity", "0.2");
$("#radio2, #radio2Text").css("opacity", "1");
});
$("#radio3").click(function() {
$("#radio1Text, #radio2Text, #radio1, #radio2").css("opacity", "0.2");
$("#radio3, #radio3Text").css("opacity", "1");
});
});
#radio1Text, #radio2Text, #radio3Text {
opacity: 1;;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="radio" name="Question" value="Answer1" id = "radio1"> <label id = "radio1Text">Answer 1</label>
<input type="radio" name="Question" value="Answer2" id = "radio2"> <label id = "radio2Text">Answer 2</label>
<input type="radio" name="Question" value="Answer3" id = "radio3"> <label id = "radio3Text">Answer 3</label>
</form>
This isn't quite what you're looking for, because while this styling differentiates between checked and unchecked states, it does so not by opacity but by using different colours. Perhaps you can adapt it to suit your needs.
Hope it helps. here's a fiddle
[name=radio] {
display: none;
}
[for^=radio] {
display: inline-block;
vertical-align: top!important;
position: relative;
margin: 10px 15px 0px 15px;
width: 120px;
}
[for^=radio]:before {
display: inline-block;
content: '';
position: relative;
margin: 0px 5px -10px 5px;
width: 32px;
height: 32px;
background: red;
}
[type=radio]:checked + [for^=radio]:before {
background: green;
}
<input id=radio-1 type=radio name=radio />
<label for=radio-1>Answer 1</label>
<input id=radio-2 type=radio name=radio />
<label for=radio-2>Answer 2</label>
<input id=radio-3 type=radio name=radio />
<label for=radio-3>Answer 3</label>
I gave a try but I was only partially successful. This is not the correct answer, but it is in pure CSS.
input[type='radio']:not(:checked) + label
{
opacity:1;
}
input[type="radio"]:checked + label
{
opacity:1;
}
input[type="radio"]:checked ~ input[type="radio"] + label
{
opacity:0.5;
}
<form>
<div class="input-group">
<input type="radio" name="Question" value="Answer1" id="one" />
<label for="one">Answer 1</label>
<input type="radio" name="Question" value="Answer2" id="two" />
<label for="two">Answer 2</label>
<input type="radio" name="Question" value="Answer3" id="three" />
<label for="three">Answer 3</label>
</div>
</form>
A code pen of it is here.

Radiobutton Style don't show up as expected

I have a very basic form, but I used a third party css which seems to give me a problem. I looked for it pretty long now in this CSS but cannot find anything what cause this problem,
Mevr.<input type="radio" name="gender" value="Mevr.">
Dhr.<input type="radio" name="gender" value="Dhr.">
Fam.<input type="radio" name="gender" value="Fam.">
this is what I expect
With the third party CSS active I get this:
My question is: What inline style I can use to get my radiobuttons next to eachother like in the first picture?
note:
the css what cause the problem is here:
As you have not given the coding.You can try this:
<style>
.some-class {
float: left;
clear: none;
}
label {
float: left;
clear: none;
display: block;
padding: 2px 1em 0 0;
}
input[type=radio],
input.radio {
float: left;
clear: none;
margin: 2px 0 0 2px;
}
</style>
<div class="some-class">
<input type="radio" class="radio" name="x" value="y" id="y" />
<label for="y">Thing 1</label>
<input type="radio" class="radio" name="x" value="z" id="z" />
<label for="z">Thing 2</label>
</div>
Since you didn't include the CSS in your post it's hard to say what the problem is.
A simple guess would be that the inputs have a display property of block. This would cause the line break and possibly other UI changes (as seen in your screenshots).
Using display: inline-block; would solve this.
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th {
margin:0;
padding:0;
}
input[type="radio"] {
display: inline-block;
}
Mevr.<input type="radio" name="gender" value="Mevr.">
Dhr.<input type="radio" name="gender" value="Dhr.">
Fam.<input type="radio" name="gender" value="Fam.">
EDIT As per your updated question, the problems seems to be with the width of you input tags.
You should try setting your width property to auto
input {
border: 1px solid #b0b0b0;
padding: 3px 5px 4px;
color: #979797;
width: auto;
}
Mevr.<input type="radio" name="gender" value="Mevr.">
Dhr.<input type="radio" name="gender" value="Dhr.">
Fam.<input type="radio" name="gender" value="Fam.">
Just replace it with the existing one in your CSS.
On a side note. I see that your CSS has multiple declarations throughout its content. Consider going through it and removing unnecessary declarations.
I have used dummy HTML. Try this:
<div style="float: left">
<input id="RadioButtonList1_0" name="RadioButtonList1" value="One" type="radio"><label for="RadioButtonList1_0">One</label>
<input id="RadioButtonList1_1" name="RadioButtonList1" value="Two" type="radio"><label for="RadioButtonList1_1">Two</label>
<input id="RadioButtonList1_2" name="RadioButtonList1" value="Three" type="radio"><label for="RadioButtonList1_2">Three</label>
</div>

Bootstrap Checkbox is not working properly

I'm using the following mark up and styles (Bootstrap). It shows my checkbox but it is paralysed, that is, it cannot be checked. here is my mark up:
I want something more Bootstrap-ish. I know there are other options to make the checkbox look fancy but that do not solve the problem.
<div class="form-group">
<div class="checkbox">
1.
<input type="checkbox" name="options" id="chk2" />
<label class="checkbox-label">Option 2</label>
</div>
</div>
Here is how it looks.
What exactly is the issue?
If I put the input element inside label I get this ugly thing:
<input type="checkbox" name="options" id="chk2" />
<label class="checkbox-label">Option 2</label>
The problem is with your label. The for attribute must match with the name attribute of your label
Looks need to tweak bootstrap styling for custom checkbox.
Check this
HTML
<div class="form-group">
<div class="checkbox">
<label for="check">
<input type="checkbox" id="check"/>
<span class="fake-input"></span>
<span class="fake-label">Option 2</span>
</label>
</div>
</div
CSS
.fake-input {
float: left;
width: 20px;
height: 20px;
border: 1px solid #9f9f9f;
background: #fff;
vertical-align: middle;
position: relative;
margin-right: 10px;
border-radius: 4px;
}
input[type="checkbox"] {
position: fixed;
top: -9999px;
left: -9999px;
}
input[type="checkbox"]:checked + .fake-input:before {
content:"\2713";
position: absolute;
color: #000;
text-align: center;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
Check in Fiddle
Reading around it looks like you have to style the checked version and the unchecked version.
input[type=checkbox]:checked {
}
Styling with this tag should solve your problems.
Use "for" attribute to solve this issue.
<div class="form-group">
<div class="checkbox">
1.
<input type="checkbox" name="options" id="chk2" />
<label for="chk2" class="checkbox-label">Option 2</label>
</div>
</div>
<div class="col-md-3">
<input class="form-check-input" type="checkbox" id="" asp-for="">
<label class="form-check-label" for="" asp-for="">
</label>
</div>
It's not due to Bootstrap but to Wordpress. The checkboxes became visible after I added "display:block;" to the css of the checkbox input tag.
<input class="form-check-input" type="checkbox" id="">
input.form-check-input {
display:block;
}

this css not working for radio button selection

in following jsfiddle, I am trying to change the color of selected radio button for two (or more) different radio groups, by only using CSS. I am missing something i do not know.
http://jsfiddle.net/2nxnk/1/
CODE:
<input type="radio" name="group0" value="1" /> One
<input type="radio" name="group0" value="2" /> Two
<input type="radio" name="group0" value="3" /> Three
<p>
<input type="radio" name="group1" value="4" /> Four
<input type="radio" name="group1" value="5" /> Five
<input type="radio" name="group1" value="6" /> Six
CSS:
input[type="radio"]:checked{ color: green; }
i do not want to use any jquery unless there is no other way. pl advice.
EDIT:
I decided to edit the question: I want to change the color of 'text of radio button'. secondly i did not want to clutter the html page with ID's if the only use is to change text color. My question now is:
1. is using the label only way?
2. seems using IDs you get capability of coloring different radio group (text again) differently. is that correct?
3. finally maybe a simple question: I though only javascript catches the action on the page, when there is no javascript how does CSS triggers the effect?
thx all for help and I also upped the last example which colors the radio button image itself.
If you want to change the text next to the radio button, you can do this way
Html
<input type="radio" name="group0" value="1" /><label>One</label>
<input type="radio" name="group0" value="2" /><label>Two</label>
<input type="radio" name="group0" value="3" /><label>Three</label>
<p>
<input type="radio" name="group1" value="4" /><label>Four</label>
<input type="radio" name="group1" value="5" /><label>Five</label>
<input type="radio" name="group1" value="6" /><label>Six</label>
Css
input[type="radio"]:checked + label {
color: green;
}
Fiddle
Probably pseudo element can help a bit:-
input[type="radio"]:checked +label {
color: green;
}
input[type="radio"]:checked:before
{
content:'.';
position:relative;
display:inline-block;
background-color:green;
width:12px;
opacity:.3;
top:-1px;
border-radius:10px;
-moz-border-radius:10px;
}
Fiddle
In most (if not all) browsers, you cannot change the colour of a radio button at all.
You can only simulate it by replacing the radio button with a different widget and trying to link it to the real radio button so it continues to work. Most implementations of this depend on JavaScript.
If you are looking to change the color of the text if respective radio button is selected, you can use + adjacent selector and wrap the text inside a label tag
input[type="radio"]:checked + label {
color: green;
}
Demo
I've removed other boxes to decrease the clutter, but logic goes same for else
Did you consider making your own radio boxes? Using labels that is
We can make labels act like radio boxes by using the "for" attribute, which then makes it clickable and react to the :checked selector. The best thing about these is a label is a basic HTML element so you can make it look exactly as you intend. Check this relatively plain example:
DEMO
<input type="radio" name="group0" value="1" id="one"/>
<label for="one"><div class="inner"></div></label><span>One</span>
<input type="radio" name="group0" value="2" id="two"/>
<label for="two"><div class="inner"></div></label><span>Two</span>
input {
display: none;
}
span,
label {
display: block;
margin: 10px 10px 10px 0;
float: left;
}
label {
width: 20px;
height: 20px;
background: #ddd;
border-radius: 10px;
}
input:checked + label{
background: green;
}
.inner {
width: 10px;
height: 10px;
margin-top: 4px;
margin-left: 4px;
background: #eee;
border-radius: 5px;
border: 1px solid #aaa;
}
Update
Here is a new fiddle with the actual buttons like I was referring to. Link: jsfiddle
Others have answered how to change the text, which I believe is what you are looking for based on your attempt. But, in the off chance you are looking to change the appearance of the button itself you could use <label for="#id"> and some CSS styling/opacity tricks to replace the buttons with images. Then, change the background color when checked. My example and fiddle use placekittens, but you could substitute the <img> sources with any image you wanted (a colored radio button with 50% opacity for example would allow you to disregard the opacity section of code.) See my code below and this fiddle. You'll have to play with the margin and padding settings to make the background color canvas the entire image, but this should give you an idea.
HTML
<input id="a" type="radio" name="group0" value="1" />
<label for="a"><img src="http://www.placekitten.com/40/40" /></label>
<input id="b" type="radio" name="group0" value="2" />
<label for="b"><img src="http://www.placekitten.com/40/39" /></label>
<input id="c" type="radio" name="group0" value="3" />
<label for="c"><img src="http://www.placekitten.com/39/40" /></label>
<p>
<input id="d" type="radio" name="group1" value="4" />
<label for="d"><img src="http://www.placekitten.com/38/40" /></label>
<input id="e" type="radio" name="group1" value="5" />
<label for="e"><img src="http://www.placekitten.com/40/38" /></label>
<input id="f" type="radio" name="group1" value="6" />
<label for="f"><img src="http://www.placekitten.com/39/38" /></label>
CSS
input {
display: none;
}
input[type="radio"]:checked + label {
background-color: green;
}
img {
opacity:0.4;
filter:alpha(opacity=40); /* For IE8 and earlier */
}
I'm not sure you can set css like that for a radio button.
BUT! you can set:
-webkit-appearance: none;
appearance: none;
vertical-align: bottom;
background: #fff;
border: 1px solid #dcdcdc;
-webkit-border-radius: 1px;
-moz-border-radius: 1px;
border-radius: 1px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
Maybe change the background color?
But that's about as much as you can change, sorry.
In case that you are already using Bootstrap (both CSS and JavaScript) you could use class selector active to achieve effect like the one you want.
See this fiddle: https://jsfiddle.net/7k0dbgsa/1/
.active
{
color: green;
}
The fiddle has been forked from https://jsfiddle.net/KyleMit/0nevkwyn/

Good HTML and CSS to use with <input type="radio">?

What's the best way to use <input type="radio"> in HTML?
I'm looking for HTML that's semantically good, whose formatting is configurable via CSS.
I want to be able to style/render it to look something like:
Car: (o) Yes
(X) No
(o) Maybe
Train: (o) Yes
(o) No
(X) Maybe
Address: [An input text box ]
Thinking of the CSS, I think that I'd like the labels on the left (e.g. "Car" and "Bus") to be in some kind of text-align: right block?
I don't know about the radio buttons on the right: in some kind of <span> perhaps, with "display: inline-block"? Or "white-space: pre"?
What kind of block-level tags (e.g. <p> or <div>) and/or other tags (e.g. <span> or <br/>) would you recommend?
Edit:
How about the following.
HTML uses <legend>, like HTML is supposed to and as recommended in the alistapart article:
<fieldset>
<legend>Car</legend>
<label><input type="radio" name="car" value="yes"/> Yes</label>
<label><input type="radio" name="car" value="no"/> No</label>
<label><input type="radio" name="car" value="maybe"/> Maybe</label>
</fieldset>
To make it easer for Firefox to access/position the contents of the <legend>, place it within a <span>:
<fieldset>
<legend><span>Car</span></legend>
<label><input type="radio" name="car" value="yes"/> Yes</label>
<label><input type="radio" name="car" value="no"/> No</label>
<label><input type="radio" name="car" value="maybe"/> Maybe</label>
</fieldset>
Then, use the browser-specific CSS described in Legends of Style Revised to position the contents of the span to left of the fieldset.
Does the CSS really have to be so complicated and browser-specific? What's the simplest CSS which ought theoretically to work, instead of the more-complicated CSS required to actually work with those imperfect browsers? If <legend> is hard to position then what's a good (semantic) alternative?
This is what I usually do with my radio buttons and checkboxes. It allows the associated text to be clickable in most browsers without having to do any work, which makes the form a little easier to use. The CSS cursor change helps to alert the user to this feature.
CSS
label { cursor: pointer; }
HTML
<label><input type="radio" name="option" value="yes"> Yes</label>
<label><input type="radio" name="option" value="no"> No</label>
<label><input type="radio" name="option" value="maybe"> Maybe</label>
Alternatively, use a fieldset legend for cars and a ul for the list of radio buttons:
<fieldset>
<legend>Cars</legend>
<ul class="radio-list">
<li><label><input type="radio" name="option" value="yes"> Yes</label></li>
<li><label><input type="radio" name="option" value="no"> No</label></li>
<li><label><input type="radio" name="option" value="maybe"> Maybe</label></li>
</ul>
<fieldset>
CSS
.radio-list li { list-style: none; }
Stylizing a fieldset/legend to be consistent across browsers isn't too difficult; however, it does require one IE conditional if you want a border around the legend. The only extra HTML that is necessary is a wrapper span within the legend.
CSS
<style>
fieldset {
position: relative;
border: 1px solid #000;
background: #f8f8f8;
padding: 1.6em 10px 0px;
margin: 0;
}
legend {
position: absolute;
font-weight: bold;
font-size: 1.2em;
}
legend span {
position: absolute;
top: -1.1em;
white-space: nowrap;
}
/* This isn't necessary, just here for list aesthetics */
ul, li {
margin: 0;
padding: 0;
list-style-type: none;
}
</style>
<!--[if IE]>
<style>
legend {
border-bottom: 1px solid #000;
}
</style>
<![endif]-->
HTML
<fieldset>
<legend><span>Did you enjoy your SO experience?</span></legend>
<form>
<ul>
<li><label><input type="radio" name="option" value="yes"> Yes</label></li>
<li><label><input type="radio" name="option" value="no"> No</label></li>
<li><label><input type="radio" name="option" value="maybe"> Maybe</label></li>
</ul>
</form>
</fieldset>
That's about as simple as I can get it. Live example
Mmmm.... Car and Train should definitely be <label>s. Check out this classic A List Apart article for a really nice example: Prettier Accessible Forms
As for the radio button labels: Good question! I'd say <label> s again, but a <span> would do as well.
css: (ugly class names need to be changed)
p.radio { height: 4em; }
label.top {
display: block;
width: 4em; /* or something else */
float: left;
text-align: right;
padding-right: 1em;
height: 4em;
}
html:
<p class="radio">
<label class="top" for="car">Car:</label>
<input type="radio" value="yes" name="car" id="car_y" />
<label for="car_y">Yes</label><br />
<input type="radio" value="no" name="car" id="car_n" />
<label for="car_n">No</label><br />
<input type="radio" value="maybe" name="car" id="car_m" />
<label for="car_m">Maybe</label><br />
</p>
EDIT: didn't see the other answer. Using a fieldset instead of a paragraph and legend for the "top-level" label seems to be a good idea IMO.
EDIT2: according to comments, and I agree, using a list would be cleaner here. The new version would be :
css:
fieldset {
border: 0;
padding: 0;
}
legend {
padding: 0 0.5em 0 0;
margin: 0;
display: block;
width: 3.5em;
float: left;
text-align: right;
}
ul {
margin: 0;
padding: 0 0 0 4em;
}
ul li {
list-style-type: none;
list-style-position: outer;
}
html:
<fieldset>
<legend>Car:</legend>
<ul>
<li>
<input type="radio" value="yes" name="car" id="car_y" />
<label for="car_y">Yes</label>
</li>
<li>
<input type="radio" value="no" name="car" id="car_n" />
<label for="car_n">No</label>
</li>
<li>
<input type="radio" value="maybe" name="car" id="car_m" />
<label for="car_m">Maybe</label>
</li>
</ul>
</fieldset>
That would be much more elegant. But even with display:block firefox doesn't seem to want to set the width of a legend element. Strange bug.
I'll use <fieldset>.
The difficulty with positioning a <label> in different browsers is described in the "Legends of Style Revised" article; so instead of using a <label> and trying to position it, I might use a <span class="label"> outside the <fieldset>.