html 将单选按钮垂直置于标签旁边

lskq00tm  于 2023-04-18  发布在  其他
关注(0)|答案(3)|浏览(103)

我尝试将所有单选按钮与其标签垂直居中,但vertical-align没有帮助:

input[type="radio"]{
    vertical-align: center;
}
<p style="display:inline-block;margin:0 0;"> Option: </p>
<label>
<input type="radio" name="group1"> Label1
</label>
<label>
<input type="radio" name="group1"> Label2
</label>
<label>
<input type="radio" name="group1"> Label3
</label>
mm9b1k5b

mm9b1k5b1#

更正为:垂直对齐:中间;

jtoj6r0c

jtoj6r0c2#

您可以使用以下CSS设置:

input[type="radio"] {
  display: block;
  margin: 0 auto 5px auto;
  
}
label {
  text-align: center;
  display: inline-block;
  width: 80px;
}
<p style="display:inline-block;margin:0 0;"> Option: </p>
<label>
<input type="radio" name="group1"> Label1
</label>
<label>
<input type="radio" name="group1"> Label2
</label>
<label>
<input type="radio" name="group1"> Label3
</label>

并应用到您的jsfiddle:https://jsfiddle.net/p5ts1rco/1/

zwghvu4y

zwghvu4y3#

我编辑了复选框https://stackoverflow.com/a/494922/7767664的示例(更改为单选按钮):

.checkboxes label {
  display: block;
  float: left;
  padding-right: 3px;
  white-space: nowrap;
}
.checkboxes input {
  vertical-align: middle;
}
.checkboxes label span {
  vertical-align: middle;
  font-size:29px;
}
<div class="checkboxes">
    <label> <span>Option: </span></label>
    <label><input type="radio" name="group1" id="x" /> <span>Label text x</span></label>
    <label><input type="radio" name="group1" id="y" /> <span>Label text y</span></label>
    <label><input type="radio" name="group1" id="z" /> <span>Label text z</span></label>
</div>

相关问题