单选按钮和标签显示在同一行


78

为什么我的标签和单选按钮不会停留在同一行,该怎么办?

这是我的表格:

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

2
您能显示整个页面和相应的CSS吗?如果仅将提供的代码段放在HTML页面的主体中,则所有内容都将在同一行上,因此必须有一些CSS规则来防止这种情况。
Darin Dimitrov

您正在使用哪种XHTML模式?
Emre Yazici 2010年

@Darin Dimitrov我有一个很大的CSS文件要发布在这里不是一个好主意,我应该在(具有多个样式表)中查找什么,或者如何分别为该样式设置样式?
甘道夫(Canda)StormCrow,2010年

@eyazici <!DOCTYPE html PUBLIC“-// W3C // DTD XHTML 1.0 Strict // EN”“ w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd ”> <html xmlns =“ w3.org/1999 / xhtml “>
甘道夫StormCrow 2010年

1
尝试安装Firebug,并使用“检查”功能列出适用于您的元素的CSS规则。
Tobias Cohen

Answers:


95

如果您使用的是我在此问题中提出的HTML结构则只需将标签浮动并输入到左侧,然后调整填充/边距,直到所有内容对齐为止。

是的,您需要使单选按钮具有旧IE的类名。并有所有他们在同一行,据我连接到上面的标记,它会像这样:

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>


2
(我建议实际上切换一下,在单选按钮后加标签,但这是您的电话。)

1
<label for =“ x”>事物1 </ label>应该为<label for =“ y”>事物1 </ label>
podeig 2012年

1
@D_N,能否请您固定属性的标签,我尝试过,但是我的编辑少于6个字符,所以我无法提交。
iancrowther

消失了一段时间。感谢您修复该问题。

@animuson有一个更好的解决方案,可以将输入嵌套在标签内。向下滚动并支持那个! <label for="one"> <input type="radio" id="one" name="first_item" value="1" /> First Item </label>
乔·乔

62

我一直做的就是将单选按钮包裹在标签内...

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

像这样的东西,一直为我工作。


7
这本身不会起作用,没有什么可以阻止标签中的单词在不同的行上分开。您需要将标签设置为display:block或white-space:nowrap。另外,我不确定将输入内容包含在标签内在语义上是否正确。
汤姆(Tom)2010年

11
用标签元素包装输入是有效的。参见w3.org/TR/html5/forms.html#the-label-element
Pisang Gesek 2014年

4
用标签包裹输入的好处是,它使文本可单击以激活单选按钮,而不是用户必须直接单击单选按钮,这对正在使用移动设备但没有移动设备的人很有用。查明您从鼠标指针获得的点击准确性。
MistyDawn

10

您可能在css中某处为输入标签指定了宽度

class="radio"在您的收音机框中添加一个,并input.radio {width: auto;}在您的CSS中添加一个。


1
更好的方法是在CSS中使用输入类型选择器,而不是添加新类。您可以简单地添加input:radio { /* styles here */ }input[type="radio"] { /* styles here */ }到您的CSS。当您将样式规则应用于所有单选输入元素时,无需使用单独的类来指定样式规则。
MistyDawn


5

嗯 默认情况下,<label>isdisplay: inline;<input>is(大约,至少)display: inline-block;,因此它们应该在同一行。参见http://jsfiddle.net/BJU4f/

也许一个样式表设置labelinputdisplay: block


2

我假设问题是,当窗口太窄时,它们将包裹在单独的行上。正如其他人指出的那样,默认情况下,标签和输入应为“ display:inline;”,因此,除非您有其他更改此样式的样式规则,否则如果有空间,它们应在同一行上呈现。

如果不更改标记,将无法仅使用CSS来解决此问题。

修复此问题的最简单方法是将单选按钮和标签包装在块元素(例如ap或a)中div,然后使用阻止包装white-space:nowrap。例如:

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

2

我总是避免使用float:left,而是使用display: inline

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

.wrapper-class label {
  display: inline;
  margin-left: 5px;
  }
<div class="wrapper-class">
  <input type="radio" id="radio1">
  <label for="radio1">Test Radio</label>
</div>


1

我使用此代码,效果很好:

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;
}

您可能需要重新调整最高价值(取决于您的line-height)。如果您不希望IE6兼容,则只需将此代码放入页面中即可。否则,您将必须在输入中添加额外的类(可以为该方法使用jQuery-或任何其他库-))


1

**使用表格将单选和文本对齐在一行中

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

**


1
请添加您所做工作的简要说明,以便作者更好地理解您的解决方案
giuseppedeponte 19-10-30

0

使用该代码,我无法在Google Chrome 4.0,IE8或Firefox 3.5中重现您的问题。标签和单选按钮位于同一行。

尝试将它们都放置在<p>标签中,或将单选按钮设置为嵌入式,如The Elite Gentleman建议的那样。



0

注意:传统上,标签是用于菜单的。例如:

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

1.不,不是-用于标记表单元素,无论是否在菜单中。2.通过将表单元素id作为for属性值放置在元素上,可以指定标签描述的表单<label>元素。您的示例错过了这一点。
Paul D. Waite

0

我遇到了类似的问题,那就是将所有单选按钮保持在同一行。在尝试了所有可能的方法之后,除以下各项外,对我没有任何帮助。我的意思是简单地使用表格解决了允许单选按钮出现在同一行中的问题。

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

0

如果问题是当窗口太窄时标签和输入会换成两行,请删除它们之间的空白;否则,请转至第二行。例如:

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

如果元素之间需要空格,请使用不间断空格(&amp; nbsp;)或CSS。


0
fieldset {
  overflow: hidden
}

.class {
  float: left;
  clear: none;
}

label {
  float: left;
  clear: both;
  display:initial;
}

input[type=radio],input.radio {
  float: left;
  clear: both;     
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.