center radio button below label

无人久伴 提交于 2019-12-21 09:24:36

问题


Let's say I have some radio buttons with their labels looking like this:

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

How do I center each radio button below its corresponding label and align it horizontally?


回答1:


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>



回答2:


Would this work? http://jsfiddle.net/fFEwh/2/




回答3:


JSFIDDLE

This alternative does not use div as wrappers, I use this to get a short DOM tree.

/* 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>


来源:https://stackoverflow.com/questions/10908281/center-radio-button-below-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!