简体   繁体   English

如何将 html 单选按钮与 label 垂直对齐?

[英]How to vertically align a html radio button to it's label?

I have a form with radio buttons that are on the same line as their labels.我有一个带有单选按钮的表单,它们与它们的标签在同一行。 The radio buttons are however not aligned vertically with their labels as shown in the screenshot below.然而,单选按钮并未与其标签垂直对齐,如下面的屏幕截图所示。

单选按钮。

How can I vertically align the radio buttons with their labels?如何将单选按钮与其标签垂直对齐?

Edit:编辑:

Here's the html code:这是 html 代码:

<input checked="checked" type="radio" name="user_level" id="rd1" value="1"/>
<label for="rd1">radio 1</label><br/>
<input type="radio" name="user_level" id="rd2" value="2"/>
<label for="rd2">radio 2</label><br/>
<input type="radio" name="user_level" id="rd3" value="3"/>
<label for="rd3">radio 3</label><br/>

And the css code:和 css 代码:

label{
    padding:5px;
    color:#222;
    font-family:corbel,sans-serif;
    font-size: 14px;
    margin: 10px;
}

Try this:试试这个:

input[type="radio"] {
  margin-top: -1px;
  vertical-align: middle;
}
input {
    display: table-cell;
    vertical-align: middle
}

Put class for all radio.为所有电台放课。 This will work for all radio button on the html page.这将适用于 html 页面上的所有单选按钮。

Fiddle小提琴

I know I'd selected the anwer by menuka devinda but looking at the comments below it I concurred and tried to come up with a better solution.我知道我选择了 menuka devinda 的答案,但查看下面的评论我同意并试图提出更好的解决方案。 I managed to come up with this and in my opinion it's a much more elegant solution:我设法想出了这个,在我看来,这是一个更优雅的解决方案:

input[type='radio'], label{   
    vertical-align: baseline;
    padding: 10px;
    margin: 10px;
 }

Thanks to everyone who offered an answer, your answer didn't go unnoticed.感谢所有提供答案的人,您的答案并未被忽视。 If you still got any other ideas feel free to add your own answer to this question.如果您还有任何其他想法,请随时为这个问题添加您自己的答案。

Might as well add a bit of flex to the answers.不妨为答案添加一点弹性。

 .Radio { display: inline-flex; align-items: center; } .Radio--large { font-size: 2rem; } .Radio-Input { margin: 0 0.5rem 0; }
 <div> <label class="Radio" for="sex-female"> <input class="Radio-Input" type="radio" id="sex-female" name="sex" value="female" /> Female </label> <label class="Radio" for="sex-male"> <input class="Radio-Input" type="radio" id="sex-male" name="sex" value="male" /> Male </label> </div> <div> <label class="Radio Radio--large" for="sex-female2"> <input class="Radio-Input" type="radio" id="sex-female2" name="sex" value="female" /> Female </label> <label class="Radio Radio--large" for="sex-male2"> <input class="Radio-Input" type="radio" id="sex-male2" name="sex" value="male" /> Male </label> </div>

try adding vertical-align:top into the css尝试将vertical-align:top添加到 css 中

label{
    padding:5px;
    color:#222;
    font-family:corbel,sans-serif;
    font-size: 14px;
    margin: 10px;
    vertical-align:top;
}​

Test: http://jsfiddle.net/muthkum/heAWP/测试: http : //jsfiddle.net/muthkum/heAWP/

You can do like this :你可以这样做:

input {
    vertical-align:middle;
}

label{
    color:#222;
    font-family:corbel,sans-serif;
    font-size: 14px;
}

I like putting the inputs inside the labels (added bonus: now you don't need the for attribute on the label), and put vertical-align: middle on the input.我喜欢将输入放在标签内(额外的好处:现在你不需要标签上的for属性),并将vertical-align: middle放在输入上。

 label > input[type=radio] { vertical-align: middle; margin-top: -2px; } #d2 { font-size: 30px; }
 <div> <label><input type="radio" name="radio" value="1">Good</label> <label><input type="radio" name="radio" value="2">Excellent</label> <div> <br> <div id="d2"> <label><input type="radio" name="radio2" value="1">Good</label> <label><input type="radio" name="radio2" value="2">Excellent</label> <div>

(The -2px margin-top is a matter of taste.) -2px margin-top是一个品味问题。)

Another option I really like is using a table.我非常喜欢的另一个选择是使用桌子。 (Hold your pitch forks! It's really nice!) It does mean you need to add the for attribute to all your labels and id s to your inputs. (拿着你的音叉!这真的很好!)这确实意味着你需要将for属性添加到所有标签,并将id到输入中。 I'd recommended this option for labels with long text content, over multiple lines.对于多行长文本内容的标签,我建议使用此选项。

 <table><tr><td> <input id="radioOption" name="radioOption" type="radio" /> </td><td> <label for="radioOption"> Really good option </label> </td></tr></table>

Something like this should work这样的事情应该工作

CSS: CSS:

input {
    float: left;
    clear: left;
    width: 50px;
    line-height: 20px;
}

label {
    float: left;
    vertical-align: middle;
}
label, input {
    vertical-align: middle;
}

I'm just align the vertical midpoint of the boxes with the baseline of the parent box plus half the x-height (en.wikipedia.org/wiki/X-height) of the parent.我只是将框的垂直中点与父框的基线加上父框的 x 高度 (en.wikipedia.org/wiki/X-height) 的一半对齐。

A lot of these answers say to use vertical-align: middle;很多这些答案都说使用vertical-align: middle; , which gets the alignment close but for me it is still off by a few pixels. ,这使对齐接近,但对我来说它仍然偏离了几个像素。 The method that I used to get true 1 to 1 alignment between the labels and radio inputs is this, with vertical-align: top;我用来在标签和无线电输入之间实现真正的 1 对 1 对齐的方法是这样的, vertical-align: top; :

 label, label>input{ font-size: 20px; display: inline-block; margin: 0; line-height: 28px; height: 28px; vertical-align: top; }
 <h1>How are you?</h1> <fieldset> <legend>Your response:</legend> <label for="radChoiceGood"> <input type="radio" id="radChoiceGood" name="radChoices" value="Good">Good </label> <label for="radChoiceExcellent"> <input type="radio" id="radChoiceExcellent" name="radChoices" value="Excellent">Excellent </label> <label for="radChoiceOk"> <input type="radio" id="radChoiceOk" name="radChoices" value="OK">OK </label> </fieldset>

While I agree tables shouldn't be used for design layouts contrary to popular belief they do pass validation.虽然我同意表格不应该用于设计布局,但与流行的看法相反,它们确实通过了验证。 ie the table tag is not deprecated.即不推荐使用 table 标记。 The best way to align radio buttons is using the vertical align middle CSS with margins adjusted on the input elements.对齐单选按钮的最佳方法是使用垂直对齐中间 CSS,并在输入元素上调整边距。

Try the below styling for the radio buttons试试下面的单选按钮样式

input { 
  width: unset;
  margin: 0 0.5em 0 0;
  vertical-align: middle;
}

Adding display:inline-block to the labels and giving them padding-top would fix this, I think.我认为,将display:inline-block添加到标签并给它们padding-top可以解决这个问题。 Also, just setting the line-height on the labels would also.此外,只需在标签上设置line-height也可以。

there are several way, one i would prefer is using a table in html.有几种方法,我更喜欢使用 html 中的表格。 you can add two coloum three rows table and place the radio buttons and lable.您可以添加两个 coloum 三行表并放置单选按钮和标签。

<table border="0">

<tr>
  <td><input type="radio" name="sex" value="1"></td>
  <td>radio1</td>

</tr>
<tr>
  <td><input type="radio" name="sex" value="2"></td>
  <td>radio2</td>

</tr>
</table>

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

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