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>
Related
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.
I am getting crazy right now since i try to style my radio buttons for hours now and i cant reach my goal (im completely new to the world of css).
What i want to do is simple:
I want three big radiobuttons underneath each other centered in my div with labels vertically aligned to the buttons. something similar to this:
I have the following html:
<div class="answers">
<label>
<input type="radio" name="answers" value="male" /> All
</label>
<label>
<input type="radio" name="answers" value="female" /> Name
</label>
<label>
<input type="radio" name="answers" value="male" /> Vendor No.
</label>
</div>
And the result is this:
I want much bigger buttons and much bigger text. i want the text to be to the right of the buttom with a little padding. i want all radio buttons to be centered. I tried many things but everything was just looking weird. Pls help me... i am beginning to hate css....
You can use this CSS:
.answers label {
display: block;
font-size: 20px;
line-height: 30px;
margin: 0 auto;
width: 150px;
}
.answers {
width: 100%;
}
.answers input[type="radio"] {
height: 30px;
line-height: 30px;
vertical-align: bottom;
width: 30px;
}
JSFiddle: http://jsfiddle.net/ghorg12110/uqyfbjsb/
The only reason to happen this is to have display: block somewhere in your css to radios:
input[type=radio] {
display: block;
}
<div class="answers">
<label>
<input type="radio" name="answers" value="male" />All
</label>
<label>
<input type="radio" name="answers" value="female" />Name
</label>
<label>
<input type="radio" name="answers" value="male" />Vendor No.
</label>
</div>
You can add display: block to second label using nth-child:
label:nth-child(2) {
display: block;
}
<div class="answers">
<label>
<input type="radio" name="answers" value="male" />All
</label>
<label>
<input type="radio" name="answers" value="female" />Name
</label>
<label>
<input type="radio" name="answers" value="male" />Vendor No.
</label>
</div>
References
nth-child
http://jsfiddle.net/6b888vp8/2/
Add display: block to the label in the answers div, and float left to the inputs. HTML has changed too
.answers label {
display: block
}
.answers input[type="radio"] {
float: left;
}
<div class="answers">
<input type="radio" name="answers" value="male" /><label>All</label>
<input type="radio" name="answers" value="female" /><label>Name</label>
<input type="radio" name="answers" value="male" /><label>Vendor No.</label>
</div>
This one worked for me:
input[type="radio"]{
width: 50px !important;
}
Try it out and check if it works for you as well.
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/
I am just wondering how to use the new HTML5 input attribute "required" in the right way on radio buttons. Does every radio button field need the attribute like below or is it sufficient if only one field gets it?
<input type="radio" name="color" value="black" required="required" />
<input type="radio" name="color" value="white" required="required" />
TL;DR: Set the required attribute for at least one input of the radio group.
Setting required for all inputs is more clear, but not necessary (unless dynamically generating radio-buttons).
To group radio buttons they must all have the same name value. This allows only one to be selected at a time and applies required to the whole group.
<form>
Select Gender:<br>
<label>
<input type="radio" name="gender" value="male" required>
Male
</label><br>
<label>
<input type="radio" name="gender" value="female">
Female
</label><br>
<label>
<input type="radio" name="gender" value="other">
Other
</label><br>
<input type="submit">
</form>
Also take note of:
To avoid confusion as to whether a radio button group is required or not, authors are encouraged to specify the attribute on all the radio buttons in a group. Indeed, in general, authors are encouraged to avoid having radio button groups that do not have any initially checked controls in the first place, as this is a state that the user cannot return to, and is therefore generally considered a poor user interface.
Source
I had to use required="required" along with the same name and type, and then validation worked fine.
<input type="radio" name="user-radio" id="" value="User" required="required" />
<input type="radio" name="user-radio" id="" value="Admin" />
<input type="radio" name="user-radio" id="" value="Guest" />
Here is a very basic but modern implementation of required radio buttons with native HTML5 validation:
fieldset {
display: block;
margin-left: 0;
margin-right: 0;
padding-top: 0;
padding-bottom: 0;
padding-left: 0;
padding-right: 0;
border: none;
}
body {font-size: 15px; font-family: serif;}
input {
background: transparent;
border-radius: 0px;
border: 1px solid black;
padding: 5px;
box-shadow: none!important;
font-size: 15px; font-family: serif;
}
input[type="submit"] {padding: 5px 10px; margin-top: 5px;}
label {display: block; padding: 0 0 5px 0;}
form > div {margin-bottom: 1em; overflow: auto;}
.hidden {
opacity: 0;
position: absolute;
pointer-events: none;
}
.checkboxes label {display: block; float: left;}
input[type="radio"] + span {
display: block;
border: 1px solid black;
border-left: 0;
padding: 5px 10px;
}
label:first-child input[type="radio"] + span {border-left: 1px solid black;}
input[type="radio"]:checked + span {background: silver;}
<form>
<div>
<label for="name">Name (optional)</label>
<input id="name" type="text" name="name">
</div>
<fieldset>
<legend>Gender</legend>
<div class="checkboxes">
<label for="male"><input id="male" type="radio" name="gender" value="male" class="hidden" required="required"><span>Male</span></label>
<label for="female"><input id="female" type="radio" name="gender" value="female" class="hidden" required="required"><span>Female </span></label>
<label for="other"><input id="other" type="radio" name="gender" value="other" class="hidden" required="required"><span>Other</span></label>
</div>
</fieldset>
<input type="submit" value="Send" />
</form>
Although I am a big fan of the minimalistic approach of using native HTML5 validation, you might want to replace it with Javascript validation on the long run. Javascript validation gives you far more control over the validation process and it allows you to set real classes (instead of pseudo classes) to improve the styling of the (in)valid fields. This native HTML5 validation can be your fall-back in case of broken (or lack of) Javascript. You can find an example of that here, along with some other suggestions on how to make Better forms, inspired by Andrew Cole.
You can use this code snippet ...
<html>
<body>
<form>
<input type="radio" name="color" value="black" required />
<input type="radio" name="color" value="white" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Specify "required" keyword in one of the select statements. If you want to change the default way of its appearance. You can follow these steps. This is just for extra info if you have any intention to modify the default behavior.
Add the following into you .css file.
/* style all elements with a required attribute */
:required {
background: red;
}
For more information you can refer following URL.
https://css-tricks.com/almanac/selectors/r/required/
<form>
Select Gender:<br>
<label>
<input type="radio" name="gender" value="male" required>
Male
</label><br>
<label>
<input type="radio" name="gender" value="female">
Female
</label><br>
<label>
<input type="radio" name="gender" value="other">
Other
</label><br>
<input type="submit">
</form>
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>.