I am working on a small interface, and currently have a simple set of radio buttons inside a fieldset with a legend. It currently looks like this:
But I want to get a border between the legend and the buttons, like so:
The code is as follows:
<fieldset>
<legend>Size:</legend>
<label>
<input type="radio" name="size" value="small" checked="checked" /> Small
</label>
<label>
<input type="radio" name="size" value="medium" /> Medium
</label>
<label>
<input type="radio" name="size" value="big" /> Big
</label>
</fieldset>
Any advice would be appreciated!
Try this:
fieldset {
border: none;
}
legend {
border-bottom: 1px solid black;
padding: 2px 0px;
width: 100%;
}
Demo
Related
How can I put each individual checkbox and label on separate lines? I've tried everything.
Here's my markup:
<label>What would you like to see improved? <label class="optional">(Check all that apply)</label>
<input value="1" type="checkbox">Front-end Projects</input>
<input value="2" type="checkbox">Back-end Projects</input>
<input value="3" type="checkbox">Data Visualization</input>
<input value="4" type="checkbox">Challenges</input>
<input value="5" type="checkbox">Open Source Community</input>
<input value="6" type="checkbox">Gitter help rooms</input>
<input value="7" type="checkbox">Videos</input>
<input value="10" type="checkbox">City Meetups</input>
<input value="11" type="checkbox">Wiki</input>
<input value="12" type="checkbox">Forum</input>
<input value="13" type="checkbox">Additional Courses</input>
You need to adjust your markup a bit. Firstly, the <label> should not be used to group a collection of inputs, it should be used to describe which each input is.
Labels and checkbox input fields are by default, inline, so they would not go on separate lines without either some CSS making it block-level, or placing it inside a block-level element such as a <div>.
As for the checkbox size, this isn't so straightforward. You have to essentially mimic the checkbox by using pseudo-elements (:before or after) inside a label to make it selectable.
I've made one using your content below.
.form-control {
display: grid;
grid-template-columns: 1em auto;
gap: 0.5em;
}
/* Make a gap between each option */
.form-control + .form-control {
margin-top: 1em;
}
input[type="checkbox"] {
appearance: none;
background-color: #fff;
margin: 0;
font: inherit;
color: currentColor;
width: 1.15em;
height: 1.15em;
border: 0.15em solid currentColor;
border-radius: 0.15em;
transform: translateY(-0.075em);
display: grid;
place-content: center;
}
input[type="checkbox"]::before {
content: "";
width: 0.65em;
height: 0.65em;
transform: scale(0);
box-shadow: inset 1em 1em black;
}
input[type="checkbox"]::before {
content: "";
width: 0.65em;
height: 0.65em;
transform: scale(0);
box-shadow: inset 1em 1em black;
}
input[type="checkbox"]:checked::before {
transform: scale(1);
}
<p>What would you like to see improved? <span class="optional">(Check all that apply)</span></p>
<label class="form-control"><input type="checkbox" value="1">Front-end Projects</label>
<label class="form-control"><input type="checkbox" value="2">Back-end Projects</label>
<label class="form-control"><input type="checkbox" value="3">Data Visualization</label>
<label class="form-control"><input type="checkbox" value="4">Challenges</label>
<label class="form-control"><input type="checkbox" value="5">Open Source Community</label>
<label class="form-control"><input type="checkbox" value="6">Gitter help rooms</label>
<label class="form-control"><input type="checkbox" value="7">Videos</label>
<label class="form-control"><input type="checkbox" value="8">City Meetups</label>
<label class="form-control"><input type="checkbox" value="9">Wiki</label>
<label class="form-control"><input type="checkbox" value="10">Forum</label>
<label class="form-control"><input type="checkbox" value="11">Additional Courses</label>
<input> doesn't have an end tag </input>. There's two ways to use a <label> that's valid:
Wrap it around a form control
<label> <input> </label>
Add an #id to the form control and for attribute to the <label>
<input id='data'> <label for="data">Data</label>
In order to change the size of a checkbox or radio button use CSS property transform and function value scale() you'll need translateY() in order to adjust their vertical position as well.
You can make a new line-break by adding a <br> in HTML. See example.
<style>
[type='checkbox'] {
transform: scale(1.25) translateY(0.75px);
}
</style>
<fieldset>
<legend>What would you like to see improved? (Check all that apply)</legend>
<input id="chx1" type="checkbox"> <label for="chx1">Front-end Projects</label><br>
<input id="chx2" type="checkbox"> <label for="chx2">Back-end Projects</label><br>
<input id="chx3" type="checkbox"> <label for="chx3">Data Visualization</label><br>
<input id="chx4" type="checkbox"> <label for="chx4">Challenges</label><br>
<input id="chx5" type="checkbox"> <label for="chx5">Open Source Community</label>
</fieldset>
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>
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>.