I am using radio buttons, but I am not sure how to align them how I want. I want them to be on the same line like this :
Option 1 o Option 2 o
But they appear like this :
Option 1 o
Option 2 o
Here is my HTML, can anybody advise?
<table>
<tr>
<td>
<label for="lblMeterName" />
</td>
</tr>
<tr>
<td>
Input/Output Group :
</td>
<td>
<input type="radio" name="rdoInput" value="Yes"/> Yes
<input type="radio" name="rdoInput" value="No"/> No
</td>
</tr>
<tr>
<td>
<input type="button" id="dialogButton" name="dialogButton" value="Submit" />
</td>
</tr>
</table>
Give your radio buttons and labels a class as follows:
<input type="radio" id="radioYes" class="inline-radio" name="radioGroup" value="Yes" />
<label for="radioYes" class="inline-radio">Yes</label>
<input type="radio" id="radioNo" class="inline-radio" name="radioGroup" value="No" />
<label for="radioNo" class="inline-radio">No</label>
Then apply the following CSS:
.inline-radio {
display: inline-block;
}
This is an improvement over the float method because float often requires additional semantics to prevent undesired rendering, whereas inline-block does not.
Put them in a table like this:
<td>
<table><tr><td>
<input type="radio" name="rdoInput" value="Yes"/> Yes
</td><td>
<input type="radio" name="rdoInput" value="No"/> No
</td></tr></table>
</td>
This gives you the most flexibility concerning space in between or alignment with parent object.
You can wrap input and text into div and set float css property for div.
There are multiple solutions for this:
You could wrap another table around the radiobuttons and add each one to a table cell.
Add a <br/> after each option.
You could float the radiobuttons.
You could add a display:inline or display:inline-block to the radiobuttons.
I would prefer wrapping both the radiobutton and it's label in a <label> tag and applying float, since this would make the label clickable as well and provide you with more flexibility:
<label class="radio-label"><input type="radio" name="rdoInput" value="Yes"/> Yes</label>
<label class="radio-label"><input type="radio" name="rdoInput" value="No"/> No</label>
And the CSS:
label.radio-label {
float: left;
margin-right: 10px;
}
Add non-breaking spaces between radio buttons;
Use this:
<table>
<tr>
<td><input type="radio" name="rdoInput" value="Yes"/> Yes</td>
<td><input type="radio" name="rdoInput" value="No"/> No</td>
</tr>
<tr>
<td colspan=2><input type="button" id="dialogButton" name="dialogButton" value="Submit" />
</td>
</tr>
</table>
Related
I'm trying to check a checkbox using <label> but by clicking a row / <tr> of <table>. Is this possible??
I have tried to use jQuery, but I'm not quite satisfied with the result, because, I'll eventually select the texts inside the row, which isn't very user friendly
I also have tried to test it in HTML on Chrome
<table border="1">
<label>
<!-- This label is expected to be used to check on the checkbox by
clicking anywhere on the table row -->
<tr>
<td><input type="checkbox" /> Foo</td>
<td>Bar</td>
</tr>
</label>
</table>
I expected when I click bar the checkbox would be checked, but it didn't
Note:
Since this is impossible to be aquired through basic HTML, I'm going to close this question
Please set label for attribute to do this, below code will help you.
<table border="1">
<tr>
<td><input type="checkbox" id="mycheckbox" /> Foo</td>
<td><label for="mycheckbox">Bar</label></td>
</tr>
</table>
$('.checkBoxChecked').on('click', function(){
var checkbox = $(this).find('.Aps_checkbox');
checkbox.prop("checked", !checkbox.prop("checked"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1">
<tr class="checkBoxChecked">
<td><input type="checkbox" id="mycheckbox" class="Aps_checkbox"/> Foo</td>
<td>Bar</td>
</tr>
<tr class="checkBoxChecked">
<td><input type="checkbox" id="mycheckbox2" class="Aps_checkbox"/> Foo2</td>
<td>Bar2</td>
</tr>
</table>
<!-- begin snippet: js hide: false console: true babel: false -->
It's not possible to achieve this by basic HTML as of now...
Based on W3, it said that <label> can only affect on one level of container only
Something like this:
Legal:
<label>
<div> AAAA</div>
<input type="checkbox">
Some text
</label>
Illegal:
<label>
<div> AAAA
<input type="checkbox">
</div>
Some text
</label>
So since my question is involving more than one level of containers, It can't be achieved by normal means
Reference:
Mozila Web Reference
Multiple labels can be associated with the same form control.
Clicking on any of the labels associated with an input element toggles the checked state of that input element.
To check a checkbox by clicking a table row, you can create a table where the first td element of each table row contains an input element, and the subsequent td elements contain labels for that input element, like so:
<table>
<tr>
<td>
<input id="foo" type="checkbox" />
</td>
<td>
<label for="foo">Foo Label 1</label>
</td>
<td>
<label for="foo">Foo Label 2</label>
</td>
</tr>
<tr>
<td>
<input id="bar" type="checkbox" />
</td>
<td>
<label for="bar">Bar Label 1</label>
</td>
<td>
<label for="bar">Bar Label 2</label>
</td>
</tr>
</table>
To maximize the clickable area of each table row, you can make each label element fill the width of its parent td element like so:
table td label {
display: block;
}
I need my input radio elements + associated text to be the same width for alignment purposes. I tried this:
.rad {
width: 80px;
}
...but that only works on the radio element itself (centers them in a sea of blankness).
So I tried this:
.rad text {
width: 160px;
}
...but it does nothing.
The html intended for styling is:
<input type="radio" class="rad" id="visitor" name="travelertype" />Visitor
<input type="radio" class="rad" id="ucstudent" name="travelertype"/>UC Student
<input type="radio" class="rad" id="ucemployee" name="travelertype"/>UC Employee
UPDATE 2
Trying the answer, I get:
You'll need to change the markup to achieve this. There are many ways, here's 2 of them.
Method 1: Use separate table columns for radio buttons and the selects.
<table>
<tr>
<td>
<input type="radio" class="rad" id="visitor" name="travelertype" />Visitor
</td>
<td>
<select ...>
</td>
</tr>
... and so on.
</table>
Method 2: Wrap the text in <label>s and add style to the labels
<input type="radio" class="rad" id="visitor" name="travelertype" /><label for="travelertype" class="label-radio">Visitor</label>
CSS:
label.label-radio {
width: 100px;
}
See this https://jsfiddle.net/byB9d/6228/
<p><input type="radio" class="rad" id="visitor" name="travelertype" /> Value</p>
<p><input type="radio" class="rad" id="ucstudent" name="travelertype"/>
UC Student</p>
<p><input type="radio" class="rad" id="ucemployee" name="travelertype"/>
UC Employee</p>
You should put the text after each radio button in a <label> for screen readers. This will also help with the CSS.
<td><input type="radio" class="rad" id="visitor" name="travelertype" /><label for="Visitor">Visitor</label></td>
<td><input type="radio" class="rad" id="ucstudent" name="travelertype"/><label for="UC Student">UC Student</label></td>
<td><input type="radio" class="rad" id="ucemployee" name="travelertype"/><label for="UC Employee">UC Employee</label></td>
You can style the whole cell with the container <td>.
td {
width: 80px;
}
Have a contact form that I have some options to check for contact times and methods. I had it styled correctly on Chrome, Firefox and Safari. The labels where to the left of the radio option and they were all lined up.
I have since run into the issue where the chrome style has broken and I cannot figure out what is going on as the Firefox and Safari CSS work perfectly. The page is cbasphaltmaintenance.com
<table id="contactOptionTable">
<tr>
<td>
<h1>Prefered contact method:</h1>
</td>
<td>
<input type="radio" value="home" name="contact_Method" class="contactRadioClass"/>
<label for="home">Home</label>
</td>
<td>
<input type="radio" value="mobile" name="contact_Method" class="contactRadioClass" />
<label for="home">Mobile</label>
</td>
<td>
<input type="radio" value="email" name="contact_Method" class="contactRadioClass" />
<label for="home">Email</label>
</td>
</tr>
<tr>
<td>
<h1>Prefered contact time:</h1>
</td>
<td>
<input type="radio" value="morning" name="contact_Time" class="contactRadioClass"/>
<label for="home">Morning</label>
</td>
<td>
<input type="radio" value="afternoon" name="contact_Time" class="contactRadioClass" />
<label for="home">Afternoon</label>
</td>
<td>
<input type="radio" value="night" name="contact_Time" class="contactRadioClass" />
<label for="home">Evening</label>
</td>
</tr>
</table>
Here is the css that works in safari and firefox
#contactOptionTable{
vertical-align: middle;
float: left;
padding: 0;
margin-left: 14%;
}
.contactRadioClass{
margin-top:10px;
}
I don't know what is going on here but it is bugging me. I appreciate all of the help. As you may see I'm a little wet behind the ears.
Add the following class in your CSS.
#contactOptionTable input
{
float:right;
}
You need to reduce margin-left: 5% and add width : 100% to #contactOptionTable.
However I suggest you not to use table structure here, it would be better if you create it using div's
The radio buttons are looking weird, and I don't know why. This is what it looks like:
I want that third one in line but I don't know how.
This is the code for all three buttons. I tried using <pr> tags but this didn't work.
<td><input type="radio"
id="examtype"
name="examtype"
value="GCSE" /> : GCSE<br />
<pr></pr>
<td><input type="radio"
id="examtype"
name="examtype"
value="A2" /> : A2<br />
<pr></pr>
<td><input type="radio"
id="examtype"
name="examtype"
value="AS"/> : AS<br />
<pr></pr>
</tr>
I don't think that pr is a tag... I'm guessing you meant to type tr, or br, or maybe even pre (but that doesn't make sense). Regardless, using the correct table structure, the elements can be neatly positioned inline without any weird jumps like in your picture:
<table>
<tr>
<td>
<input type="radio" id="examtype" name="examtype" value="GCSE" />: GCSE
</td>
<td>
<input type="radio" id="examtype" name="examtype" value="A2" />: A2
</td>
<td>
<input type="radio" id="examtype" name="examtype" value="AS" />: AS
</td>
</tr>
</table>
Example
I have form with 2 radio buttons but both of the radio buttons can be selected,
<form class="descriptions" id="collection" method="post" action="">
<table width="200">
<tr>
<td>
<label>Collection</label>
<input type="radio" value="collection" />
</td>
<td>
<label>Delivery</label>
<input type="radio" value="delivery" />
</td>
</tr>
</table>
</form>
I know this is very easy but I can't seem to find the answer,
any help would be appreciated.
Give them the same name.
Description of the radio button control type in the HTML 4.01 specification:
Radio buttons are like checkboxes except that when several share the same control name, they are mutually exclusive: when one is switched "on", all others with the same name are switched "off".
<form class="descriptions" id="collection" method="post" action="">
<table width="200">
<tr>
<td>
<label>Collection</label>
<input type="radio" name="samename" value="collection" />
</td>
<td>
<label>Delivery</label>
<input type="radio" name="samename" value="delivery" />
</td>
</tr>
</table>
</form>
// this is the correct answer for ur following problem //