I'm using ASP.NET radio button list control.
It generates the following HTML.
<table>
<tr>
<td>
<input type="radio"/>
<label >
label 1</label>
</td>
<td>
<input type="radio"
value="False" checked="checked" />
<label >
label 2</label></td>
</tr>
</table>
Everything looks fine so far(at least to the user), labels are aligned with radio buttons.
However when I resize the font to say 10px, the size of the label obviously looks smaller but the side-effect of that is that the label also looks like it is aligned to the bottom of a radio button. I need a label to be alligned to middle of a radio button.
I was able to do this in IE with the following css:
<style type="text/css">
label
{
font-size:10px;
line-height:12px;
vertical-align:middle;
}
</style>
However this doesn't work in Firefox or Chrome
Any suggestions?
Instead of this:
label {
font-size: 10px;
line-height: 12px;
vertical-align: middle;
}
try this:
label, input[type="radio"] {
font-size: 10px;
vertical-align: middle;
}
That attribute selector won't work in IE6 - the most painless method I've found is to add a class of 'radio' to your radio buttons, so the CSS becomes:
label, input.radio{
font-size:10px;
vertical-align:middle;
}
Of course, a table isn't the correct markup for that kind of form - personally I favour a definition list, with labels inside the DT and inputs in the DD. Semantically this is OK in my opinion - you're showing a list of terms (the labels) which the user needs to define (the inputs).
The markup would look like this:
<dl class="formStructure">
<dt class="radio"><label for="option1">Yes</label></dt>
<dd class="radio"><input type="radio" name="option" id="option1" value="yes"/></dd>
<dt class="radio"><label for="option2">No</label></dt>
<dd class="radio"><input type="radio" name="option" id="option2" value="no"/></dd>
</dl>
Related
I want to disable the label associated with checkbox. The code snippets are below:
code:
<label for="label1" class="form-checkbox-left"><input type="checkbox" name="labelname" id="label1" value="0" style="min-width: 20px;" disabled>Name 1</label>
css :
.form-checkbox-left input[type=checkbox]:disabled {
color:#ccc;
}
Somehow it is not working.
Please help
You can opt to use the element+element Selector. You need to place the input before the label, however
input[type=checkbox]:disabled+label {
color: #ccc;
}
<input type="checkbox" name="labelname" id="label1" value="0" style="min-width: 20px;" disabled>
<label for="label1" class="form-checkbox-left">Name 1</label>
you can try to do with jquery using
if ($('label1'.hasAttribute('disabled')){
$('[for="label1"]').attr('disabled', 'disabled');
}
at [for="label1"] you can put the id of lable ("if you want to")
I have just double-checked with Firefox, and adding any sort of a style definition to a checkbox doesn't change any aspect of its appearance.
It seems that you are out of luck here and would have to take a different approach by hiding the checkbox itself (not with display: none;, but with visibility: hidden; instead) and then add auxiliary CSS to actually customize its appearance.
An example on how to do this can be found on http://cssdeck.com/labs/css-checkbox-styles
Maybe that helps you derive a method that suits you best.
EDIT
If you are attempting to style the parent label of the checkbox, you are out of luck, because parent element selectors are not implemented in CSS so far, although a proposal has been made (see https://shauninman.com/archive/2008/05/05/css_qualified_selectors for details).
EDIT 2
You could try this by moving the label definition behind the checkbox definition:
<input type="checkbox" name="labelname" id="label1" value="0" style="min-width: 20px;" disabled><label for="label1" class="form-checkbox-left">Name 1</label>
Then your CSS should look like this to facilitate the change:
input[type="checkbox"][disabled] + label {
color: #ccc;
}
This in turn modifies the text of your label.
Is it possible to create a radio button group without the round buttons in front of each element?
The reason I would like to implement this is, that in my case the user has to choose between 3 different languages and I would really like to add this selection to a <form> tag, change the color of the selected language and make it required, but in the same time I wanted it to look something like this:
___________________________
| Username | <--Text input
___________________________
___________________________
| Password | <--Text input
___________________________
____________________________
| EN | DE | FR | <--This is what I thought of... Horizontal selection
____________________________ of the language looking like a simple table with
3 rows and the plain text (EN, DE, FR) in it.
____________________________
| Login | <--Submit button
____________________________
I really hope that you're able to get my point :)
If you put the radio buttons inside the labels and then make them invisible the user can click the label to select the radio button that is inside it. Consider the following approach.
HTML:
<div>
<label><input type="radio"/>English</label>
<label><input type="radio"/>French</label>
</div>
CSS:
label > input[type=radio] {
visibility: hidden;
}
JSFiddle here: http://jsfiddle.net/gEXUT/
Note that this is just an example, you'd still need to add the radio group name and perhaps the option for German etc.
Yes and no.
If you build your form with input and labels, it will do, else,
you have to. :)
the idea is :
input[type=radio] {
position:fixed;
left:-9999px;
}
As being fixed and of the screen, your input radio won't be in the flow anymore.
If labels are well formed and link to theme with attribute for, you just need to clikc the label to checked your invisible radio input.
To style your form, don't mind those imputs, style your labels as wished.
<input type="radio" name="r-lang" id="r1"><label for="r1"> EN </label>
Cheers
I've actually written on this before, and made a jsfiddle example:
http://jsfiddle.net/Kyle_Sevenoaks/HzQBE/
I'll explain it though. (I've put the labels an radio buttons into a list for this example)
<li class="cardtype-item">
<input type="radio" name="preferred_color" id="red" value="Red" />
<label for="red"> Red</label>
</li>
The general idea is that you have labels linked to the radio buttons, but the radios are hidden (either by display, position, etc). Then you use CSS to style the labels exactly as you like, and because they're linked to the radio buttons (via "name" on the input and "for" on the label) you can have much more control over how they look.
li
{
background: #333;
color: #eee;
padding: 10px;
list-style-type: none;
float: left;
width: 100px;
}
li.selected
{
background: #eee;
color: #333;
box-shadow:inset 0px 0px 15px #999;
}
input[type=radio]
{
display: none;
}
The next part of the trick is to use Javascript (I've use jQuery) to add and remove the selected or active class on the label itself.
$('li.cardtype-item label, li.cardtype-item input').click( function() {
$(this).parents('li').addClass('selected');
$(this).parents().siblings('li').removeClass('selected');
});
var ident = $('input[type=radio]').attr("id");
if($('input[type=radio]').is('checked')) {
$('form').append(ident);
};
I hope this gives you pretty much what you're after.
try this
radio button html
<div class="buttonSlider">
<input type="radio" value=".." name="radio1" />
<input type="radio" value=".." name="radio1" />
<input type="radio" value=".." name="radio1" />
</div>
javascript
$(document).ready(function () {
$('.buttonSlider input').replaceWith('<div class="radiobox"> <input type="radio" name="radio1" value=".."/></div>');
$('.buttonSlider input').prop('checked', false);
$('.radiobox').click(function () {
var this_div = $(this);
if (this_div.find('input').is(':checked')) {
this_div.find('input').prop('checked', false);
this_div.css({ 'background-color': '#800001' });
}
else {
this_div.find('input').prop('checked', true);
this_div.css({ 'background-color': '#808080' });
}
})
})
css
.buttonSlider
{
background-color: #800001;
}
.buttonSlider .radiobox
{
height: 20px;
width: 100px;
border: 1px solid black;
background-color: #800001;
float: left;
}
.buttonSlider input
{
display: none;
}
Thanks to the help of everyone of you (and this awesome answer). I could finally implement it in my website.
This is my code:
HTML:
<div id="language">
<table id="languagetable" border="0px" cellspacing="0px">
<tr>
<td width="33.33333%">
<input type="radio" id="fr" name="languageselection" value="en">
<label for="en">FR</label>
</td>
<td width="33.33333%">
<input type="radio" id="en" name="languageselection" value="de" checked>
<label for="de">EN</label>
</td>
<td width="33.33333%">
<input type="radio" id="it" name="languageselection" value="it">
<label for="de">DE</label>
</td>
</tr>
</table>
</div>
CSS:
#languagetable input[type="radio"] {
display:none;
}
#languagetable label {
display:inline-block;
margin: 0 auto;
font-family: Arial;
font-size: 20px;
}
#languagetable input[type="radio"]:checked + label {
color: #99CC00;
}
I have the following html.
<td>
<div id="form_comparison" class="field radio_field">
<input type="radio" id="form_comparison_0" name="form[comparison]" value="1"/>
<label for="form_comparison_0">Increased</label>
<input type="radio" id="form_comparison_1" name="form[comparison]" value="2" />
<label for="form_comparison_1">About the same</label>
<input type="radio" id="form_comparison_2" name="form[comparison]" value="3" />
<label for="form_comparison_2">Decreased</label>
</div>
</td>
Using css, how can I position radio buttons vertically, so that labels are displayed just after their respective radio buttons in the same line?
I don't know why you've wrapped this inside a td element, if you are designing a form layout, than ignore tables and use div for designing your form. Coming to your question, you can wrap the labels around input tag and use display: block; for label
#form_comparison label {
display: block;
}
Wrap each input using label like this
<label for="form_comparison_0">
<input type="radio" id="form_comparison_0" name="form[comparison]" value="1"/>
Increased
</label>
Demo
If you don't have any permissions to change the markup, you can use CSS content property with white-space: pre; and that will give you the desired output
label:after {
content: "\A";
white-space: pre;
margin-bottom: 10px;
}
Demo (No Changes In The Markup)
Note: Use #form_comparison label instead of only label as it will
select and apply all label element in your website where
#form_comparison label will only select label elements inside
#form_comparison
Working FIDDLE Demo
Float your input and label, and for second row, clear the float to make a new row:
#form_comparison input {
float: left;
}
#form_comparison label {
float: left;
}
#form_comparison label + input {
clear: both;
}
For the CSS gurus out there, this markup outputs a checkbox with a label Value1 to its right, but Value1 is too close to the checkbox.
<dd id="rr-element">
<label for="rr-1">
<input type="checkbox" value="1" id="rr-1" name="rr[]">
Value 1
</label>
</dd>
So I'm trying to create a padding-right effect to the right of the checkbox, but it's not working. The checkbox and label move together. How can I target the checkbox only or its text only so I create a padding gap?
dd label input {
padding-right:100px;
}
Use margin-right. padding is inside the element, and often acts funny with input elements because they are rendered using the OS's native input components, and are a mess to customize in general.
JSFiddle here
No unclickable gap between checkbox and label.
No line wrap disconnection of the checkbox and label.
No spaces added by code indentations.
More readable.
<dd id="rr-element">
<input type="checkbox" value="1" id="rr-1" name="rr[]">
<label for="rr-1">Value 1</label>
</dd>
<style>
#rr-element {
white-space: nowrap;
}
#rr-element label {
padding-left: 0.4em;
}
</style>
ASP.Net has a tag called CheckboxList. The output of this tag looks like this:
<table class="checkbox">
<tbody>
<tr>
<td>
<input id="/*longdynamicstring1*/" type="checkbox" name="/*longdynamicstring2*/" />
<label for="/*longdynamicstring1*/">Label Text</label>
</td>
</tr>
</tbody>
</table>
I want to position the label and the input but I cannot find out how. Tried the following:
.checkbox input{
padding-right: 5px;
}
and
.checkbox input[type='checkbox']
{
padding-right: 5px;
}
but neither of them had any effect. Because it's ASP I cannot set a class for the input elements and I cannot reference the id because it's dynamic.
Your selector works well, it's just that padding has no effect on the check box. Margin will work, for example:
.checkbox input {
margin-right: 50px;
}
See it in action: http://jsbin.com/irari
In addition, take a look at the RepeatLayout property - it can make the generated HTML more CSS friendly.