Radio buttons and label to display in same line

HtmlCss

Html Problem Overview


Why my labels and radio buttons won't stay in the same line, what can I do ?

Here is my form:

<form name="submit" id="submit" action="#" method="post">
    <?php echo form_hidden('what', 'item-'.$identifier);?>
			
    <label for="one">First Item</label>
    <input type="radio" id="one" name="first_item" value="1" />
			
    <label for="two">Second Item</label>
    <input type="radio" id="two" name="first_item" value="2" />
    <input class="submit_form" name="submit" type="submit" value="Choose" tabindex="4" />
</form>

Html Solutions


Solution 1 - Html

If you use the HTML structure I lay out https://stackoverflow.com/questions/2350152/documentation-on-fieldset-label-tags-instead-of-using-tables/2350481#2350481">in this question you can simply float your label and input to the left and adjust padding/margin until things are lined up.

And yes, you'll want to make your radio button have a class name for old IE. And to have all of them on the same line, according to the markup I linked to above, it would be like so:

fieldset {
      overflow: hidden
    }
    
    .some-class {
      float: left;
      clear: none;
    }
    
    label {
      float: left;
      clear: none;
      display: block;
      padding: 0px 1em 0px 8px;
    }
    
    input[type=radio],
    input.radio {
      float: left;
      clear: none;
      margin: 2px 0 0 2px;
    }

<fieldset>
      <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>
    </fieldset>

Solution 2 - Html

What I've always done is just wrap the radio button inside the label...

<label for="one">
<input type="radio" id="one" name="first_item" value="1" />
First Item
</label>

Something like that, has always worked for me.

Solution 3 - Html

you might have a width specified for your input tags somewhere in your css.

add a class="radio" to your radio boxes and an input.radio {width: auto;} to your css.

Solution 4 - Html

Put them both to display:inline.

Solution 5 - Html

Hmm. By default, <label> is display: inline; and <input> is (roughly, at least) display: inline-block;, so they should both be on the same line. See http://jsfiddle.net/BJU4f/

Perhaps a stylesheet is setting label or input to display: block?

Solution 6 - Html

I always avoid using float:left but instead I use display: inline

<div class="wrapper-class">
  <input type="radio" id="radio1">
  <label for="radio1">Test Radio</label>
</div>

.wrapper-class input[type="radio"] {
  width: 15px;
}

.wrapper-class label {
  display: inline;
  margin-left: 5px;
  }

Solution 7 - Html

I'm assuming the problem is that they are wrapping onto separate lines when the window is too narrow. As others have pointed out, by default the label and input should be "display:inline;", so unless you have other style rules that are changing this, they should render on the same line if there is room.

Without changing the markup, there will be no way to fix this using only CSS.

The simplest way to fix it would be to wrap the radio button and label in a block element, such as a p or a div, and then prevent that from wrapping by using white-space:nowrap. For example:

<div style="white-space:nowrap;">
  <label for="one">First Item</label>
  <input type="radio" id="one" name="first_item" value="1" />
</div>

Solution 8 - Html

** Used table to align the radio and text in one line >

<div >
  <table>
    <tbody>
      <tr>
        <td><strong>Do you want to add new server ?</strong></td>
        <td><input type="radio" name="addServer" id="serverYes" value="1"></td>
        <td>Yes</td>
        <td><input type="radio" name="addServer" id="serverNo" value="1"></td>
        <td>No</td>

      </tr>
    </tbody>
  </table>
</div>

**

Solution 9 - Html

I wasn't able to reproduce your problem in Google Chrome 4.0, IE8, or Firefox 3.5 using that code. The label and radio button stayed on the same line.

Try putting them both inside a <p> tag, or set the radio button to be inline like The Elite Gentleman suggested.

Solution 10 - Html

I use this code and works just fine:

input[type="checkbox"], 
input[type="radio"],
input.radio,
input.checkbox {
	vertical-align:text-top;
	width:13px;
	height:13px;
	padding:0;
	margin:0;
	position:relative;
	overflow:hidden;
	top:2px;
}

You may want to readjust top value (depends on your line-height). If you don't want IE6 compatibility, you just need to put this code into your page. Otherwise, you will must add extra class to your inputs (you can use jQuery - or any other library - for that tho ;) )

Solution 11 - Html

If the problem is that the label and input are wrapping to two lines when the window is too narrow, remove the whitespace between them; e.g.:

<label for="one">First Item</label>
<input type="radio" id="one" name="first_item" value="1" />

If you need space between the elements, use non-breaking spaces (&amp; nbsp;) or CSS.

Solution 12 - Html

Solution 13 - Html

Note: Traditionally the use of the label tag is for menus. eg:

<menu>
<label>Option 1</label>
<input type="radio" id="opt1">
	
<label>Option 2</label>
<input type="radio" id="opt2">
	
<label>Option 3</label>
<input type="radio" id="opt3">	
</menu>

Solution 14 - Html

I was having the similar issue of keeping all radio buttons on the same line. After trying all the things I could, nothing worked for me except the following. What I mean is simply using table resolved the issue allowing radio buttons to appear in the same line.

<table>
<tr>
    <td>
        <label>
            @Html.RadioButton("p_sortForPatch", "byName", new { @checked = "checked", @class = "radio" }) By Name
        </label>
    </td>
    <td>
        <label>
            @Html.RadioButton("p_sortForPatch", "byDate", new { @class = "radio" }) By Date
        </label>
    </td>
</tr>
</table>

Solution 15 - Html

fieldset {
  overflow: hidden
}
    
.class {
  float: left;
  clear: none;
}
    
label {
  float: left;
  clear: both;
  display:initial;
}
    
input[type=radio],input.radio {
  float: left;
  clear: both;     
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGandalf StormCrowView Question on Stackoverflow
Solution 1 - Htmluser241244View Answer on Stackoverflow
Solution 2 - HtmlanimusonView Answer on Stackoverflow
Solution 3 - HtmlAlexarView Answer on Stackoverflow
Solution 4 - HtmlcasrafView Answer on Stackoverflow
Solution 5 - HtmlPaul D. WaiteView Answer on Stackoverflow
Solution 6 - HtmlRossView Answer on Stackoverflow
Solution 7 - HtmlTomView Answer on Stackoverflow
Solution 8 - HtmlRaushan KumarView Answer on Stackoverflow
Solution 9 - Htmlsoren121View Answer on Stackoverflow
Solution 10 - HtmlIonuČ› StaicuView Answer on Stackoverflow
Solution 11 - HtmlkevingessnerView Answer on Stackoverflow
Solution 12 - HtmlAdrenalineJunkyView Answer on Stackoverflow
Solution 13 - HtmlsherbView Answer on Stackoverflow
Solution 14 - HtmlDhruv RangunwalaView Answer on Stackoverflow
Solution 15 - HtmlSaurav BishtView Answer on Stackoverflow