当内容包含在流布局中时,如何将单选按钮或复选框及其标签保持在一起

Tim*_*Tim 14 html css checkbox radio-button flowlayout

当浏览器窗口变窄时,如何在文本换行时将单选按钮()或复选框[]及其标签保持在一起,这样我们就不会得到这样的结果:

          [ ] pepperoni   [ ] anchovies   [ ] mushrooms    [ ]
          olives
Run Code Online (Sandbox Code Playgroud)

编辑以回应nowrap建议.谢谢你的建议.差不多了.它适用于Chrome,FF和Opera,但不适用于IE9.当页面CSS label {white-space: nowrap}和标记是:

          <td>
          <div>
          <label>
          <input type="radio" name="pizza" checked="true" 
           id="pepperoni" value="pepperoni"  />pepperoni  </label>
          <label>
          <input type="radio" name="pizza"  
           id="anchovies" value="anchovies"  />anchovies  </label>
           <label>
          <input type="radio" name="pizza" 
           id="mushrooms" value="mushrooms"  />mushrooms  </label>
          <label>
          <input type="radio" name="pizza" 
           id="olives" value="olives"  />olives           </label>
          </div>
          </td>
Run Code Online (Sandbox Code Playgroud)

在IE9中 TD变为固定宽度; 当浏览器窗口变窄时,TD不会收缩,我明白了:

         ( ] pepperoni   ( ) anchovies   ( ) mushrooms   ( )  olives
Run Code Online (Sandbox Code Playgroud)

当我需要这个时:

          ( ) pepperoni   ( ) anchovies   
          ( ) mushrooms   ( ) olives
Run Code Online (Sandbox Code Playgroud)

IE9还有其他技巧吗?我尝试在标签之间放置一个包含单个空格的范围: ...</label><span> </span><label>...但这不起作用.

Sir*_*rko 12

<span>容器包围并white-space: nowrap;放在上面.

<span style="white-space: nowrap;"> 
  <input type="checkbox" id="in1" /> 
  <label for="in1">pepperoni</label>
</span>
<span style="white-space: nowrap;"> 
  <input type="checkbox" id="in2" /> 
  <label for="in2">anchovies</label>
</span>
<span style="white-space: nowrap;"> 
  <input type="checkbox" id="in3" /> 
  <label for="in3">mushrooms</label>
</span>
<span style="white-space: nowrap;"> 
  <input type="checkbox" id="in4" /> 
  <label for="in4">olives</label>
</span>
Run Code Online (Sandbox Code Playgroud)

编辑

如@xiaoyi所述,您也可以将<label>自身用作容器:

<label style="white-space: nowrap;"> <input type="checkbox" />  pepperoni</label>
<!-- etc -->
Run Code Online (Sandbox Code Playgroud)

  • 现在它根本没有制动线!OP希望打破这条线,但不是在收音机和它的标签之间! (4认同)
  • 怎么样`<label> <input type ="textbox"> xxx </ label>`?更干净 (3认同)
  • 在PHP中:echo"<label style ='white-space:nowrap'> <input type ='radio'name ='radName'value ='x'> xxx </ label>"; 注意到最后的空间.只有空间在那里才有效,有些奇怪(因为空间在标签标签之外). (2认同)

Eli*_*ato 9

您可以将输入和标签<span>包装在一个或将输入包装在标签内.无论哪种方式,外部容器(跨度或标签)应该具有样式white-space:nowrap; display:inline-block.这适用于IE9以及其他浏览器.所以你的最终标记应该是:

<span style="white-space:nowrap; display:inline-block"> 
  <input type="checkbox" name="pizza" checked="true"
   id="pepperoni" value="pepperoni" /> 
  <label for="pepperoni">pepperoni</label>
</span>
Run Code Online (Sandbox Code Playgroud)

要么

<label style="white-space:nowrap; display:inline-block">
  <input type="checkbox" name="pizza" checked="true" 
   id="pepperoni" value="pepperoni"/>
  pepperoni
</label>
Run Code Online (Sandbox Code Playgroud)