简体   繁体   English

如何垂直对齐多组单选按钮?

[英]How do I vertically align sets of radio buttons?

I am new to html and css. 我是html和css的新手。

I have 3 sets of radio buttons. 我有3套单选按钮。 Each set of buttons is displayed in a singe line. 每组按钮都以单线显示。 I would like the buttons to align vertically between sets of buttons. 我希望按钮在各组按钮之间垂直对齐。 I am having trouble accomplishing this with css. 我无法用css完成这个任务。 I would like to avoid using tables to solve this problem. 我想避免使用表来解决这个问题。 I figured I could get the radio buttons aligned by setting the width of their labels, but it doesn't work. 我想我可以通过设置标签的宽度来对齐单选按钮,但它不起作用。

My radio buttons display like this: 我的单选按钮显示如下:

未对齐的单选按钮

I want them to display like this: 我希望它们显示如下:

对齐的单选按钮

Here's my css: 这是我的css:

form.remit_change_form label.fixedwidth {
    display: block;
    width: 180px;
    float: left;
}

form.remit_change_form input.radiolabel {
    width: 35px /* doesn't make any visual change! */
}

Here's my html: 这是我的HTML:

<div>
  <label for="tran_code" class="fixedwidth">Tran Code</label>
  <input type="radio" class="radioinput" name="tran_code" id="tran_code_22" 
    value="22"/>
  <label for="tran_code_22" class="radiolabel">22</label>
  <input type="radio" class="radioinput" name="tran_code" id="tran_code_42" 
    value="42"/>
  <label for="tran_code_42" class="radiolabel">42</label>
  <input type="radio" class="radioinput" name="tran_code" id="tran_code_52" 
    value="52"/>
  <label for="tran_code_52" class="radiolabel">52</label>
  <input type="radio" class="radioinput" name="tran_code" id="tran_code_na" 
    value="NA"/>
  <label for="tran_code_na" class="radiolabel">NA</label>
</div>
<div>
  <label for="cut_off_time" class="fixedwidth">Cut-off Time</label>
  <input type="radio" class="radioinput" name="cut_off_time" id="cut_off_time_18.00" 
    value="18.00"/>
  <label for="cut_off_time_18.00" class="radiolabel">18.00</label>
  <input type="radio" class="radioinput" name="cut_off_time" id="cut_off_time_19.00" 
    value="19.00"/>
  <label for="cut_off_time_19.00" class="radiolabel">19.00</label>
  <input type="radio" class="radioinput" name="cut_off_time" id="cut_off_time_na" 
    value="NA"/>
  <label for="cut_off_time_na" class="radiolabel">NA</label>
</div>
<div>
  <label for="remit_notify_method" class="fixedwidth">Remit Notify Method</label>
  <input type="radio" class="radioinput" name="remit_notify_method" 
    id="remit_notify_method_n" value="N"/>
  <label for="remit_notify_method_n" class="radiolabel">N</label>
  <input type="radio" class="radioinput" name="remit_notify_method" 
    id="remit_notify_method_f" value="F"/>
  <label for="remit_notify_method_f" class="radiolabel">F</label>
  <input type="radio" class="radioinput" name="remit_notify_method" 
    id="remit_notify_method_e" value="E"/>
  <label for="remit_notify_method_e" class="radiolabel">E</label>
</div>

You don't have to use classes, if not otherwise needed ( edit : they were needed, so answer has been updated). 如果不需要,您不必使用类( 编辑 :需要它们,因此答案已更新)。

form.remit_change_form label.radiolabel
{
    display:inline-block; /* added this */
    width: 50px;
}

form.remit_change_form label.fixedwidth {
    width: 180px;
}

Why not give yourself some more HTML elements to work with? 为什么不给自己一些HTML元素来使用? If you're opposed to using tables, how about using an unordered list? 如果您反对使用表格,那么如何使用无序列表?

http://jsfiddle.net/YYjsT/ http://jsfiddle.net/YYjsT/

Putting it in a table works nicely 把它放在一张桌子里工作得很好

http://jsfiddle.net/3QJg8/ http://jsfiddle.net/3QJg8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM