如何在同一行强制使用复选框和文本?

And*_*eas 38 html

如何强制复选框和后续文本显示在同一行?在下面的HTML中,我只希望行在标签和输入之间断开,而不是在输入和标签之间断开.

<p><fieldset>
    <input type="checkbox" id="a">
    <label for="a">a</label>
    <input type="checkbox" id="b">
    <!-- depending on width, a linebreak can occur here. -->
    <label for="b">b</label>
    <input type="checkbox" id="c">
    <label for="c">c</label>
</fieldset></p>
Run Code Online (Sandbox Code Playgroud)

澄清一下:如果fieldset/p对于所有元素都不够宽,而不是:

[] a [] b []
c [] d [] e
Run Code Online (Sandbox Code Playgroud)

我想要:

[] a [] b
[] c [] d
[] e
Run Code Online (Sandbox Code Playgroud)

Ada*_*zer 31

如果你在div中包装每个项目它不会破坏.看看我的小提琴下面的链接.我将字段集的宽度设为125px,并使每个项目的宽度为50px.您会看到标签和复选框在新行上并排保留,不会中断.

<fieldset>
<div class="item">
    <input type="checkbox" id="a">
    <label for="a">a</label>
</div>
<div class="item">
   <input type="checkbox" id="b">
<!-- depending on width, a linebreak can occur here. -->
    <label for="b">bgf bh fhg fdg hg dg gfh dfgh</label>
</div>
<div class="item">
    <input type="checkbox" id="c">
    <label for="c">c</label>
</div>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/t5dwp7pg/1/

  • 尝试使您的标签大于一个字母!它不适用于较长的标签. (2认同)

Bre*_*ith 27

试试这个CSS:

label {
  display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)

  • 将每个复选框和标签对包装在一个使用display:inline-block设置的div中 (10认同)
  • @Brent:如果只有你的回答反映了你的评论,我会投票.事实上,答案是孤立的; 但是,如果你按照你在评论中的建议行事,那就有效. (3认同)
  • 仍可以在复选框和标签之间显示换行符.我会编辑我的问题以澄清. (2认同)

Man*_*nde 20

试试这个.以下将checkbox和label视为唯一元素:

<style>
  .item {white-space: nowrap;display:inline  }
</style>
<fieldset>
<div class="item">
    <input type="checkbox" id="a">
    <label for="a">aaaaaaaaaaaa aaaa a a a a a a aaaaaaaaaaaaa</label>
</div>
<div class="item">
   <input type="checkbox" id="b">
<!-- depending on width, a linebreak NEVER occurs here. -->
    <label for="b">bbbbbbbbbbbb bbbbbbbbbbbbbbbbb  b b b b  bb</label>
</div>
<div class="item">
    <input type="checkbox" id="c">
    <label for="c">ccccc c c c c ccccccccccccccc  cccc</label>
</div>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

  • “空白:nowrap”是我一直在寻找的窍门。谢谢。 (3认同)

Ric*_*era 8

另一种方法是单独使用css:

input[type='checkbox'] {
  float: left;
  width: 20px;
}
input[type='checkbox'] + label {
  display: block;
  width: 30px;
}
Run Code Online (Sandbox Code Playgroud)

请注意,这会强制将每个复选框及其标签放在一个单独的行上,而不是仅在出现溢出时才这样做.


小智 7

You can wrap the label around the input:

 **<label for="a"><input type="checkbox" id="a">a</label>**
Run Code Online (Sandbox Code Playgroud)

This worked for me.