标签下方的中心单选按钮

时间:2012-06-06 04:44:53

标签: html css css3 radio-button

假设我有一些单选按钮,标签看起来像这样:

<label for="my_radio_button_id">My Label</label>
<input type="radio" name="radio" id="my_radio_button_id" />

如何将每个单选按钮置于相应标签下方并将其水平对齐?

3 个答案:

答案 0 :(得分:25)

FIDDLE

.checkboxgroup {
  display: inline-block;
  text-align: center;
}
.checkboxgroup label {
  display: block;
}
<div id="checkboxes">
  <div class="checkboxgroup">
    <label for="my_radio_button_id1">My Label1</label>
    <input type="radio" name="radio" id="my_radio_button_id1" />
  </div>
  <div class="checkboxgroup">
    <label for="my_radio_button_id2">My Label2</label>
    <input type="radio" name="radio" id="my_radio_button_id2" />
  </div>
  <div class="checkboxgroup">
    <label for="my_radio_button_id3">My Label3</label>
    <input type="radio" name="radio" id="my_radio_button_id3" />
  </div>
</div>

答案 1 :(得分:2)

这会有用吗? http://jsfiddle.net/fFEwh/2/

答案 2 :(得分:1)

JSFIDDLE

preview

这个替代方案不使用div作为包装器,我使用它来获得一个简短的DOM树。

/* center radio below label */

.radioGroupBelow label {
  display: inline-block;
  text-align: center;
  margin: 0 0.2em;
}
.radioGroupBelow label input[type="radio"] {
  display: block;
  margin: 0.5em auto;
}
​
<div class="radioGroupBelow">
  Fruits:

  <label for="fruit1">Orange
    <input type="radio" name="fruits" id="fruit1">
  </label>

  <label for="fruit2">Apple
    <input type="radio" name="fruits" id="fruit2">
  </label>

  <label for="fruit3">Grape
    <input type="radio" name="fruits" id="fruit3">
  </label>

  <label for="fruit4">Lemon
    <input type="radio" name="fruits" id="fruit4">
  </label>
</div>