这是一个基础的 CSS 问题,我有一个带有小文本标签的单选按钮。我希望文本垂直居中显示,但在我的情况下,文本始终与单选按钮的底部对齐。
<p><input type="radio" id="opt1" name="opt1" value="1" />A label</p>
这是一个 Jsfiddle 的链接:
有什么建议吗?
谢谢,
Alan。
这是一个基础的 CSS 问题,我有一个带有小文本标签的单选按钮。我希望文本垂直居中显示,但在我的情况下,文本始终与单选按钮的底部对齐。
<p><input type="radio" id="opt1" name="opt1" value="1" />A label</p>
这是一个 Jsfiddle 的链接:
有什么建议吗?
谢谢,
Alan。
将其放置在 label
标签内。使用 vertical-align
属性将其设置为不同的值 -- 如 bottom
、baseline
、middle
等。
input[type="radio"] {
vertical-align: baseline;
}
<label>
<input type="radio" id="oddsPref" name="oddsPref" value="decimal" /> Decimal
</label>
<label>
<input type="radio" id="oddsPref" name="oddsPref" value="decimal" /> <img src="https://via.placeholder.com/80x30">
</label>
vertical-align
属性也可以使用数值。例如,vertical-align:-1px;
。 - Slavavertical-align: sub;
使我的对齐了。 - Wartodust我想这可能是你所要求的
label {
font-size: 18px;
vertical-align: middle;
}
input[type="radio"] {
vertical-align: middle;
}
<span>
<input type="radio" id="oddsPref" name="oddsPref" value="decimal" />
<label>Decimal</label>
</span>
input[type="radio"] {
margin-top: 1px;
vertical-align: top;
}
以下是简单易懂的解决方案,只需添加以下样式:
style="vertical-align: text-bottom;"
HTML:
<label><input type="radio" id="opt1" name="opt1" value="1"> A label</label>
CSS:
label input[type="radio"] { vertical-align: text-bottom; }
<p><input type="radio" id="oddsPref" name="oddsPref" value="decimal" /><span>Decimal</span></p>
span{
margin-top: 4px;
position:absolute;
}
here is the fiddle http://jsfiddle.net/UnA6j/11/
input[type="radio"]{
float:left;
}
input[type="radio"] {
margin-top: -1px;
vertical-align: middle;
}
<label class="child"><input id = "warm" type="radio" name="weathertype" value="warm" checked> Warm<br></label>
<label class="child1"><input id = "cold" type="radio" name="weathertype" value="cold" checked> Cold<br></label>
input.radio {vertical-align:middle; margin-top:8px; margin-bottom:12px;}
只需根据需要调整顶部和底部,即可完美对齐单选按钮或复选框
<input type="radio" class="radio">
<label>
元素作为标签而不是段落。 - Musa